Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static global variables in C++

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?

like image 494
Christian Daley Avatar asked Jan 16 '13 01:01

Christian Daley


People also ask

Can we use static as global variable in C?

A global variable can be accessed from anywhere inside the program while a static variable only has a block scope. So, the benefit of using a static variable as a global variable is that it can be accessed from anywhere inside the program since it is declared globally.

Where are global static variables stored in C?

The static variables are stored in the data segment of the memory. The data segment is a part of the virtual address space of a program. All the static variables that do not have an explicit initialization or are initialized to zero are stored in the uninitialized data segment( also known as the BSS segment).

What is difference between static global and non static global variable in C?

A global static variable is only available in the translation unit (i.e. source file) the variable is in. A non-static global variable can be referenced from other source files.

What is static and global function in C?

Unlike global functions in C, access to static functions is restricted to the file where they are declared. Therefore, when we want to restrict access to functions, we make them static. Another reason for making functions static can be reuse of the same function name in other files.


2 Answers

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 in main.cpp, it is initialized ONLY in main.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 verbose malloc syntax, and this would perform initialization as well? Or you could take the easy road (thats what its there for) and use std::vector

like image 158
Karthik T Avatar answered Sep 21 '22 20:09

Karthik T


The key is this:

static int *pieces; 

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:

extern int *pieces;  extern int init_pieces(); 

And in the source file, you do this:

static const size_t num_pieces = 128;  int *pieces = 0;  int init_pieces() {     pieces = malloc( num_pieces * sizeof(int) );     return pieces != NULL; } 

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 a vector.

Also, when using statics in C++, be mindful of this: C++ static initialization order

like image 28
paddy Avatar answered Sep 20 '22 20:09

paddy