11-12-2008 10:44 AM
I have a large global structure that is causing stack overflows. The structure is defined as extern in a global.h file and defined in global.c.
The static keyword causes an error if i use it in the header to extern define the structure.
What is the proper way to define a global structure as static so that it will be allocated in the heap.
11-12-2008 11:57 AM
The heap is where dynamically allocated (e.g. by malloc) memory resides. If you declare a variable (without extern) at file scope in your .c file, that causes the compiler to allocate storage for that object in the data segment of the compiled binary. The data segment is separate from the heap and stack, so if your large global structure is declared at file scope -- that is, not inside a function -- it should not be causing any stack overflows. You may be missing the real culprit. On the other hand, if you're talking about a global typedef for a structure which you later declare instances of within functions, then it's possible to run into a stack overflow.
If you need more help, please try to figure out exactly at what point you get your stack overflow, and present portions of the offending code.
Mert A.
National Instruments
11-13-2008 10:29 AM
in C, the 'static' keyword used on data declaration does not have the same meaning wether it is used for global (file-scope) variables or local (function-scope) variables.
'static' used on global variables tells the compiler that this data is NOT visible from any other compilation unit (a compilation unit is a C source file). as such, 'extern' and 'static' are opposed and the two cannot be used together on the same variable: either it should be accessed from outside the file, in which case it is extern, or it should not in which case it is static. by the way, the extern keyword is not mandatory in included header files.
anyway, this is not related at all to a stack overflow... in which case are you having a stack overflow ? if you pass a large structure as a parameter to a function, this is likely to happen. in this case just pass a pointer to the structure.