LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How to define global static struct in heap?

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.

0 Kudos
Message 1 of 3
(5,245 Views)

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

0 Kudos
Message 2 of 3
(5,238 Views)

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.

0 Kudos
Message 3 of 3
(5,212 Views)