title: Static global variables in C++
body: <p>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:</p>
  
  <pre><code>static int *pieces;
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>pieces = malloc(sizeof(int) * 128);
  </code></pre>
  
  <p>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?</p>