I would like to make an array of integers via the malloc method. I want this array to be global and be used anywhere in my program. I put code in a header file that looked like this:
static int *pieces;
Then I have a function that fills it with numbers that I want in there. The function is in a namespace and the namespace is implemented in its own .cpp file. However, I import the header file into main.c and call the function from the namespace that creates the array like:
pieces = malloc(sizeof(int) * 128);
But when I try to access numbers in the array in main (after calling the function that creates my array), it crashes and says that pieces wasn't initialized. But in the function I have I can create it and manipulate the numbers in it just fine. I was under the impression that by making pieces a static variable, whenever some function anywhere changes (or sets it) then that will affect the usage of the variable anywhere. Basically what I'm trying to say is why does pieces appear unset in main, even though I set it in a function that I called?
For high performance code on various architectures, you may want a malloc-y allocation rather than generic new. That is because you would wrap it with something like mymalloc() and then use architecture dependent functions, such as ones that implement the proper alignment to avoid cache misses and do other nifty things provided by the hardware manufacturer, such as IBM (Bluegene) or Intel (MIC). All of these optimized allocation routines have the malloc type framework.
In C++17 standard, you can use
inline
specifier instead of static. For variables this means every object unit will have a copy of the variable, but linker will choose only one of them. Or, as stated on cppreference:Supported in (source):
In this case, it means you can replace
with
Static
is a keyword with many meanings, and in this particular case, it means not global (paraphrasing)It means that each
.cpp
file has its own copy of the variable. Thus, when you initialize inmain.cpp
, it is initialized ONLY inmain.cpp
. The other files have it still uninitialized.First thing to fix this would be to remove the keyword
static
. That would cause the "Multiple definitions issue". To fix this you should define the variable in a.cpp
file and just extern declare it in a header file.Edit: You are just allocating memory to it, doesnt count as initialization. You need to initialize the memory to 0 after allocation.
You can use
new int[128]()
instead of your more verbosemalloc
syntax, and this would perform initialization as well? Or you could take the easy road (thats what its there for) and usestd::vector
The key is this:
You said you put that in your header. This is not the way to export a symbol. Any file that includes the header will get its own static version of an uninitialised pointer called
pieces
.Instead, you put this in your header:
And in the source file, you do this:
Now when you include your header, your source file will know to get
pieces
from somewhere else, and will wait for the linker to work out where. I also suggested an 'init' function for the array. I did not put a 'release' function in, however.Note this is all C, not C++. If you're using C++ you should really use
new
or better still, use avector
.Also, when using statics in C++, be mindful of this: C++ static initialization order