简体   繁体   中英

Why can a static class variable not be allocated to the stack?

Normally, a local variable declared in a function is allocated on the stack, not on the heap, but this is not the case if the variable is static. Why can a static class variable not be allocated on the stack?

Because then it wouldn't be static. Your question embodies a contradiction in terms.

Variables allocated on the stack are automatically deleted. You don't want a static variable to be deleted.

The lifetime of a local static variable is the whole of the program, but the stack is used, reused and reused again over multiple function calls, by all functions. Therefore static local variables can't be stored on the stack together with the other local variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM