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
}

The debugger panel shows the first 6 elements contain non zero ints. Why is this?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With