-4

i am currently learning C++ and i had a question about some "weird" things i noticed while testing what i have learnt.

So i use this :

int marc[9];
int sum = 0;

for (int x =0; x < 9; x++) {
    marc[x] = x*4;
    sum += marc[x];
}
cout << sum << endl;

And i get a normal result which is 144

However, if i changed

int sum = 0;

to

int sum;

I got a crazy result like 19557555002

So i was wondering what is happening in the second case?

Thanks for your answer :)

Steeph
  • 3
  • 5
  • 2
    obviously, `sum` in second case is not initialized so you 're getting garbage – DimChtz Jul 28 '16 at 10:14
  • 2
    This has nothing to do with the operator `+=`. This is a classic undefined behaviour scenario: using a variable before initializing it. See [What happens to a declared, uninitialized variable in C? Does it have a value?](http://stackoverflow.com/q/1597405/183120) for relevant details, it applied to C++ too. – legends2k Jul 28 '16 at 10:14
  • thanks for the link, didn't know it was called like that – Steeph Jul 28 '16 at 10:19

4 Answers4

1

Its called an undefined behavior and it happens when you don't initialize your variables.

int sum;

above code can only declare a variable but it doesn't initialize it by default so the variable contains a garbage value.

Itban Saeed
  • 1,660
  • 5
  • 25
  • 38
1

this creates an uninitialized int int sum;

it can have "garbage" values, and this is exactly what happened to you

how this happens: let's say you use an int x in address y, and sets it to 19557555002. now, lets say you "leave" that address (go out of scope, program terminates, OS takes that memory...) and someone else takes it because he wants to put there a new int. if he just declares his int, without initializing it, his int can be stationed (if the OS so desires...) in address y, that previously used to hold your int, which means in address y, he will find 19557555002. That is what could happen to you if you don't initialize variables.

Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
CIsForCookies
  • 12,097
  • 11
  • 59
  • 124
1

You are operating on uninitialized memory in the second case. Non-static local variables are not initialized to zero by the runtime like in other languages, if you need sum to have a defined value, you must initialize it yourself. The 19557555002 will be the integer interpretation of any bytes that were present at the memory address allocated for sum.

Further reading: What happens to a declared, uninitialized variable in C? Does it have a value?

Community
  • 1
  • 1
Cee McSharpface
  • 8,493
  • 3
  • 36
  • 77
0

Memory for local variables is typically allocated on the stack. This means that without some initialization, they will hold some data that was residing there previously.

As others said it is undefined behavior, but practically, on most implementations, it results in effects like this:

void foo()
{
    int a = 5;
}

void bar()
{
    int b;
    std::cout << b;
} 

void someCaller()
{
    foo();
    bar();
}

On most implementations, this will usually result in the printing of 5 on the stdout.

Note that some compilers like MSVC initialize all variables in Debug configuration, but usually any kind of optimization flags will avoid initializing memory, if not explicitly requested.

vordhosbn
  • 333
  • 4
  • 16