Morgan Casale
Morgan Casale

Reputation: 3

Initialize a new static variable every loop, C++

There is a way in C++ to create a new static variable (with a different memeory address) in a loop for every iteration?

Here it is an example:

static vector<int *> vec;
for(int i=0; i<n; i++){ //n is a generic number 
    static int var=i; //var should be a new static variable every loop
    vec.pushback(&var);
}

The vector "vec" should contain all the different addresses of the newly created variables.

Thanks in advance

Edit 1: This was only a simple example to summarize my problem; my goal is to have a function that creates a new static variable every time it is called, then this variable should be modified and finally its address stored in a vector of pointers.

Upvotes: 0

Views: 743

Answers (1)

Zan Lynx
Zan Lynx

Reputation: 54325

What is a static variable?

In most operating systems the global variables and all static variables are placed into the program image in their own data section. If they have complicated initialization then those are run on program start, or protected with locks and set on first use.

The only difference between global and static variables is that the compiler will not allow your program to use a static variable that is outside the current scope. From an assembly language viewpoint, statics and globals are identical.

Since all globals and statics are created from the program image at startup, you cannot add more of them later.

If you want to use dynamic amounts of memory that grows over time, you need to use the heap. C++ has new T, new T[], delete and delete[] in order to handle that. The C++ standard containers use allocators, which in turn call new in special ways. Normally you don't worry about allocators and just use the containers.

Some fun details!

I made a little C file which I compiled into a .o all on its own, so it wouldn't get optimized away when combined with main. All main does is call f().

#include <stdio.h>

int global_0;
int global_1 = 1;
int global_2 = 2;

static int static_0;
static int static_3 = 3;
static int static_4 = 4;

int f() {
  static int static_func_0;
  static int static_func_5 = 5;

  printf("%p\n", (void *)&global_0);
  printf("%p\n", (void *)&global_1);
  printf("%p\n", (void *)&global_2);

  printf("%p\n", (void *)&static_0);
  printf("%p\n", (void *)&static_3);
  printf("%p\n", (void *)&static_4);

  printf("%p\n", (void *)&static_func_0);
  printf("%p\n", (void *)&static_func_5);

  return 0;
}

And then a few inspections of the resulting program:

$ objdump -t ./global-static-test
[...snipped a bunch of junk...]
000000001002002c l     O .data  0000000000000004              global_1
0000000010020030 l     O .data  0000000000000004              global_2
0000000010020034 l     O .data  0000000000000004              static_3
0000000010020038 l     O .data  0000000000000004              static_4
000000001002003c l     O .data  0000000000000004              static_func_5.0
0000000010020044 l     O .bss   0000000000000004              global_0
0000000010020048 l     O .bss   0000000000000004              static_0
000000001002004c l     O .bss   0000000000000004              static_func_0.1

So you can see that the globals, the file scope statics and the function statics are all huddled together in .data and .bss sections.

The reason for .bss is it is for all zero-initialized things. The program loader does not actually load .bss. Instead it just allocates some number of zero-filled memory pages. A global array of a hundred megabytes wouldn't need to be read off of disk, as long as it contained only zeroes.

And the reason the "_func_" variables have numbers on the end is because you could have many functions with static variables, all with the same names. The compiler adds a number to make each one unique.

And here's the contents of .data

$ objdump -s -j .data ./global-static-test
./global-static-test:     file format elf64-powerpcle

Contents of section .data:
 10020028 00000000 01000000 02000000 03000000  ................
 10020038 04000000 05000000                    ........

Upvotes: 4

Related Questions