Is it possible to declare a large array as static and use it as extern in other files in c?

By making it static you're avoiding overflowing the stack (the heap isn't involved), but by placing it inside main no other parts of your program can access it directly.

Up vote 2 down vote favorite share g+ share fb share tw.

I have a very big array which is shared among many functions in many files in a vc project. My problem is, I have to declare it in main() and use extern in the header files. Since the array is too large for the stack I have to use static which makes it impossible to have the extern declaration in the header files.

How can I solve this problem? EDIT: What I did was as you said but I get error LNK2001: unresolved external symbol Here is my global declaration and the extern declaration: main. C static unsigned char bit_table_ROWSCOLUMNS; hdr.h extern unsigned char bit_table_ROWSCOLUMNS; ROWS and COLUMNS could grow as large as 1024 and 1048576 respectively c arrays static extern link|improve this question edited Mar 18 at 0:20 asked Mar 17 at 23:57John25017 87% accept rate.

1 I doubt "too large for the heap". Anyway, alloc/initialize the data at program start (static or whatever works) and set up an "extern" pointer provide access to said data. Might also want to look at mmap to keep the data separate... – pst Mar 18 at 0:01 1 Show what you've tried so far.

– Jim Rhodes Mar 18 at 0:03 @pst: don't you think 24MB of space is "too large"? – John Mar 18 at 0:03 2 @John - no. I'd imagine pretty much any machine you're using Visual Studio to compiler for can allocate 24 MB without too much effort.

– Carl Norum Mar 18 at 0:05 2 Wait - are you mixing up "heap" and "stack"? – Carl Norum Mar 18 at 0:06.

By making it static, you're avoiding overflowing the stack (the heap isn't involved), but by placing it inside main no other parts of your program can access it directly. To share between functions and files in the same program, you must define it outside of main, and put an extern declaration for it in a header that you'll include in the other files that need to access it: big_array. C: #include "big_array.

H" int my_big_arraybig_size; in big_array. H: #define big_size 1234567 extern int my_big_array; Then any other file that needs access to it just: #include "big_array. H" // ... my_big_array1234 = new_value.

I just have done like this except that I have put the array size in the extern declaration. – John Mar 18 at 0:07 1 @John: That doesn't hurt anything, but the size in the declaration is ignored. I did just edit -- you normally want to define the size in the header.

– Jerry Coffin Mar 18 at 0:09 @John: Looking at your edited question, however, there is one thing you really need to change: remove the static from the definition. The static keeps that definition from being visible in other translation units, even with an extern declaration. – Jerry Coffin Mar 18 at 0:52.

Declare a global pointer and share it among all of your source files (via extern in a header). Then populate that global pointer in main(). Edit: Your comments on your question seem to indicate that you're confusing the heap with the stack.

Just make your array global and share access to it with an extern declaration in a header. Problem solved, and no funny tricks to be pulled.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions