0

http://en.cppreference.com/w/cpp/language/storage_duration

Static local variables

Static variables declared at block scope are initialized the first time control passes through their declaration (unless their initialization is zero- or constant-initialization, which can be performed before the block is first entered). On all further calls, the declaration is skipped.

What is the meaning of 'static' in this quote? Is it:

static storage duration. The storage for the object is allocated when the program begins and deallocated when the program ends.

If so, then it doesn't explain what happens to int k; in the main or any other function in terms of initialization, because k is not a static variable (it doesn't get allocated when the program begins and deallocated when the program ends - wait a second, you might say that the main function starts running when the program begins and returns when the program ends, but that's not how it works I guess).

In the main function:

int k;
k++;

results in an error: uninitialized local variable 'k' used.

So if k above is not a static local variable, could you give an example of such variables?

And could anyone tell me why the following code compiles and runs without any issues, even though k is not initialized?

Given a function with no body:

void foo(int* number) { }

And calling it like this in main:

int k;    
foo(&k);
k++;

It now compiles and runs with no problems, but the value of k is -858993459. The compiler didn't like the fact I attempted to increment it without initiation, but passing it to foo caused that the compiler forgot about it. Why?

Community
  • 1
  • 1
user5539357
  • 1,024
  • 1
  • 9
  • 19
  • Downvoters, care to explain the reason? – user5539357 Mar 08 '16 at 12:38
  • A program begins before `main` is called and ends after `main` has returned. Local variables have automatic storage duration unless you specify static duration with `static`, such as `static int k;` – molbdnilo Mar 08 '16 at 13:02

3 Answers3

0

Probably depends on your compiler implementation. I guess the compiler sees that you variable is passed to a function in a non-const way, so the function can potentially modify/initialize the value of k. Your compiler doesn't dig into the function to extrapolate the logic, so it let you compile the code.

EDIT : I don't get the same behaviour with this online compier : http://cpp.sh/9axqz But the compilation warning disapears as soon as I do something with the number in the function.

Neozaru
  • 1,109
  • 1
  • 10
  • 25
0

A static local variable is something like this:

void example() {
  static int i = 0; // a static local variable
  int j = 0; // not static
  // ...
}

Builtin types such as int are not automatically initialized. Therefore you may get a warning when you try to read from that variable before having set it to something meaningful because it can hold just about anything.

Just because your particular compiler no longer gave you a warning does not mean that your variable is initialized properly. As a rule of thumb you should always explicitly initialize your variables to avoid accidentally reading from it without having set it first:

int i; // not initialized
int j = 0; // explicitly set to 0

Here is a link to the C++ Core Guidelines on the topic of initialization: https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es20-always-initialize-an-object, it may be worth a read.

villintehaspam
  • 8,540
  • 6
  • 45
  • 76
  • I was confused as of the meaning of static in that article. Somehow I thought they used a different meaning of static variable there, especially that static storage has been explained in the preceding paragraph... – user5539357 Mar 08 '16 at 12:43
0

A static variable can be defined like this:

int nextIndex()
{
    static int index = -1;
    return ++index;
}

So when you first call this function the static variable index will be initialized with -1;. The next line will increase index by 1 and return the result.

All subsequent calls of nextIndex will only execute the return ++index; part and skip the initialization.

Also static varibales are automatically initialized with zero if not implemented different.

So if you change above function to

int nextIndex()
{
    static int index;
    return ++index;
}

the first value it returns will be one. Also the first call won't initialize the index, but it will be initialized as soon as your program starts.

Simon Kraemer
  • 5,700
  • 1
  • 19
  • 49