Sharat Chandra
Sharat Chandra

Reputation: 4554

where is a static variable inside a struct located?

in C, if I declare a static variable inside a local strucutre, where does the static variable get placed ? Since the struct is in the stack, will the static variable also be in the stack ?

Upvotes: 2

Views: 2573

Answers (2)

MrTJ
MrTJ

Reputation: 13202

In C++ static variables will be initialized when you first time use their class. In C they are not allowed since C compiler needs to store the whole struct in the same type of memory. About their storage see Where are static variables stored (in C/C++)?

Upvotes: 3

cnicutar
cnicutar

Reputation: 182774

If I declare a static variable inside a local strucutre

In current C the keyword static is meaningless inside a structure. You should get an error from the compiler.


If by "static" you mean "not allocated using malloc": the member of a structure is always stored in the same place as the rest of the structure. If said member is a pointer, it can point to memory in the same region or not.

Upvotes: 10

Related Questions