3

I write the following code and set a breakpoint in xcode:

#include <iostream>
using namespace std;
int main(int argc, const char * argv[])
{
    int array[12];
    return 0;        //Set breakpoint here
}

enter image description here

The debugger panel shows the first 6 elements contain non zero ints. Why is this?

Matt Harrison
  • 13,381
  • 6
  • 48
  • 66

5 Answers5

8
int array[12];

This declares an array with 12 elements, not an empty array.

Furthermore it declares them without an initializer, which (in function scope) means that they will be default initialized. For int that means no initialization is performed and the resulting ints will have indeterminate values. This behavior is defined in the specification for C++.

If you want to zero initialize the array then you need to give it an initializer:

int array[12] = {};

The reason that this is not forced behavior is that there is a performance cost to initialization and some programs are written to work correctly without needing to suffer that penalty.

bames53
  • 86,085
  • 15
  • 179
  • 244
  • Is that right: "Furthermore it declares them without an initializer, which (in function scope) means that they will be default initialized." ? –  Oct 07 '13 at 20:02
  • 1
    @DieterLücking Yep; See 8.5/11 in the spec. 8.5 also defines default initialization, value initialization, zero initialization, etc. – bames53 Oct 07 '13 at 20:07
  • 1
    Just to complete the answer for C, since there is a C tag, too. In C `{}` is not a valid initializer. You'd have to use `{ 0 }`, there. – Jens Gustedt Oct 07 '13 at 21:50
  • @JensGustedt it is now 2021 an we are "led to believe" `int arr[] = { };` is a valid initalization of an "empty" array? Please advise? ( `https://godbolt.org/z/Pj1ae3P6x` ) ps: yes, I see not exactly in the context of "zeroing the array". – Chef Gladiator Apr 16 '21 at 13:05
  • 1
    @ChefGladiator, zero sized arrays and empty `{}` initializers are gcc extension, which clang seem to have adopted, too. – Jens Gustedt Apr 16 '21 at 14:04
  • @JensGustedt many thanks. "Is standard C actually GCC?" might be an approx. title for a very useful article on some blog (wink, wink). – Chef Gladiator Apr 17 '21 at 09:24
4

Because you only declared the array, not initialized it.

When you declare the only thing that happens is that you reserve a certain area of memory. What is already stored on that area can be anything left over from other operations/programs.

paul23
  • 8,799
  • 12
  • 66
  • 149
2

Only global and static variables (incl. arrays) are assured to have zero initial values. For local arrays (as in your code) you can initialize to zeroes using:

int array[12] = {0};

Check this link for more details: How to initialize array to 0 in C?

Community
  • 1
  • 1
Igor Popov
  • 2,588
  • 17
  • 20
0

The C++ compiler does not initialize variables unless you tell it to.

Dale Wilson
  • 9,166
  • 3
  • 34
  • 52
0

Because your array is not initialized. Debugger panel is showing you the previous value stored at that locations.

haccks
  • 104,019
  • 25
  • 176
  • 264