3

The following code works, but is it well defined?

const int N = 8;
int arr[N];
int* ptr = arr;
for(int i=0; i<N; i++) {
  std::cout << ptr[i] << "\n";
}

and a second question:

running this code with -O2 optimizations compiled as C++14 http://cpp.sh/7eagy gives me the following output:

1
0
0
0
4196448
0
4196198
0

wherease the following version:

const int N = 8;
int arr[N] = {};
int* ptr = arr;
for(int i=0; i<N; i++) {
  std::cout << ptr[i] << "\n";
}

gives

0
0
0
0
0
0
0
0

indicating that without the = {} arr was not zero initialized, but according to https://en.cppreference.com/w/cpp/language/zero_initialization it should be

at least the given example states: double f[3]; // zero-initialized to three 0.0's

so what's the reason I'm not getting zeros in the first version?

6
  • 2
    Why it should be zero initialized? Commented Jul 15, 2020 at 17:31
  • 1
    int arr[N]; is declared but not defined. However, in the second version it is declared and initialized to 0 explicitly. Commented Jul 15, 2020 at 17:31
  • 3
    You seem to be under the impression that the array in your first example is static (that's what the title of the question says). But it isn't, and because it isn't there is no zero initialisation. Commented Jul 15, 2020 at 17:32
  • 1
    @user1282931 Yes, well defined. Commented Jul 15, 2020 at 17:38
  • 1
    The title of the question asks about casts and pointer increments; neither one appears in the code. Commented Jul 15, 2020 at 17:46

3 Answers 3

3

In the first version, you have

int arr[N];

instead of, second version:

int arr[N] = {};

It has nothing to do with the pointer access. The page you link says:

double f[3]; // zero-initialized to three 0.0's

but that is true only for static variables. Yours is a local, non-static, variable (we don't see the full code, but as it is at the same scope as a for, we can infer it). In standardese, it's written as:

  1. For every named variable with static or thread-local storage duration that is not subject to constant initialization, before any other initialization.
Sign up to request clarification or add additional context in comments.

2 Comments

The OP read that static arrays are always zero initialized, and that's causing the confusion.
yeah, we should have made the static keyword more obscure, it is too obvious what it does in C++ </sarcasm> :-)
2

is it well defined to cast a static array to a pointer and then incrementing this pointer to access the elements?

Yes, this is well defined.

The following code works, but is it well defined?

No, that code is not well defined.

You default initialise the array which has automatic storage, not static. The values of the trivially constructible elements are indeterminate. You read those indeterminate values. The behaviour of the program is undefined.

so what's the reason I'm not getting zeros in the first version?

Because you didn't zero-initialise the array in the first version.

2 Comments

I think the OP is confused about the fact that the array is not static.
yep, I somehow confused static with constant size
1

Jeffrey has given you three quarters of an answer. I'll make it more complete.

The first example you define an array but you don't initialize the values. In the second example, you actually initialize it to zero.

So in the first example, your array contains random values, whatever is sitting on the stack space. And that's why your example is working oddly.

I'd initialize the array and your first example should work fine.

2 Comments

The OP read that static arrays are always zero initialized, and that's causing the confusion.
Ah. And I'm not an expert on the standard and tend to fall back to ancient habits from C unless I specifically learn something different.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.