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?

1
  • An array is never empty. What you want to ask is why do you get non-zero values in it. Oct 7, 2013 at 19:51

5 Answers 5

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.

6
  • Is that right: "Furthermore it declares them without an initializer, which (in function scope) means that they will be default initialized." ?
    – user2249683
    Oct 7, 2013 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 7, 2013 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. Oct 7, 2013 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". Apr 16, 2021 at 13:05
  • 1
    @ChefGladiator, zero sized arrays and empty {} initializers are gcc extension, which clang seem to have adopted, too. Apr 16, 2021 at 14:04
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.

1
  • That is not a declaration, but a definition, a declaration does not reserve memory, it only tells the compiler that there will be memory reserved somewhere else. Oct 7, 2013 at 19:47
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?

0

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

2
  • It depends on the types of the variables and the context. Oct 7, 2013 at 19:49
  • meaning if you create an array of objects with null constructors, they get constructed? Yes, that one way in which you can tell the compiler to initialize variables. Oct 7, 2013 at 20:26
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.