简体   繁体   中英

Array Initialization

I am trying to assign values within an array within the condition of a for loop:

#include <iostream>
using namespace std;
int* c;
int n;
int main()
{
    scanf("%d", &n);
    c = new int[n];
    for (int i = 0; i < n; c[i] = i++  )
    {
        printf("%d \n", c[i]);
    }
    return 0;
}

However, I am not obtaining the desired output, for n = 5, 0 1 2 3 4 . Instead, if I am using the instruction, c[i] = ++i , I am obtaining the output -842150451 1 2 3 4 . Could you please explain me we does my code behave like this and how can I correct it?

The value of the expression ++i is the value after i has been incremented. So if it started at 0, you assign value 1 the first time and so on. You can see where the value got assigned, but asking why it got assigned there opens a can of worms.

Using i in an expression where i is modified via i++ or ++i is undefined behavior unless there is a so-called "sequence point" in between the two. In this case, there isn't. See Undefined behavior and sequence points for this rather complicated part of the language.

Although the behaviour is undefined by the standard, and may not be consistent from one run to another, clearly your program has done something . Apparently it didn't assign to index 0 at all (at least, not before the first print, which is understandable considering that the loop body happens before the last part of the "for"), so you got whatever just so happened to be in that raw memory when it was allocated to you. It assigned 1 to index 1 and so on.

This means that it may also have attempted to assign the value 5 to c[5] , which is a class of bug known as a "buffer overrun", and more undefined behavior on top of what you've already got. Attempting to assign to it probably overwrites other memory, which on any given day may or may not contain something important.

The fix is to assign some value to c[0] , and don't try to assign to c[5] , which doesn't exist anyway, and don't try to invalidly use i "at the same time as" incrementing it. Normally you'd write this:

for (int i = 0; i < n; ++i) {
    c[i] = i;
    printf("%d \n", c[i];
}

If you're desperate for some reason to assign in the third clause of a for loop, you could use the comma operator to introduce a sequence point:

for (int i = 0; i < n; c[i] = i, ++i) {
}

But of course if you do that then you can't print the value of c[i] in the loop body. It hasn't been assigned yet, because the third clause isn't evaluated until the end of each loop.

You could also try c[i] = i+1, ++i , but not ++i, c[i] = i because then we're back to trying to assign to c[5] , on the last iteration.

First you need to understand that the last part of the for loop is executed at the end of each iteration, so the reason you see this:

-842150451 1 2 3 4

Is because you print c[0] before it is assigned, so the value could be anything. The rest falls into line as expected.

Lesson; don't be sneaky and stuff things into the last part of the for loop like that. Make your code clear and simple:

for (int i = 0; i < n; ++i  )
{
    c[i] = i;
    printf("%d \n", c[i]);
}

Firstly, you are claiming that you want to assign the values "within the condition of the loop". In for loop the condition is the second part of the header ( i < n in your case). You are performing the assignment in the third part, which is not a condition. So, why are you saying that you want to assign the values within the condition, and yet not doing that?

Secondly, expressions like c[i] = i++ or c[i] = ++i do not have any defined behavior in C++ language. In C++ it is illegal to modify a variable and at the same time read it for any other purpose without an intervening sequence point. Yet, you are doing exactly that. There's no meaningful explanation for the behavior of your code. The behavior of your code is undefined . It can do anything for any random reason.

Thirdly, initializing anything in the for condition is generally not a good idea. Could you explain in more detail what you are trying to do and why? Without it it is hard to come up with anything meaningful.

The reason why c[i] = ++i produces undefined behavior. It's undefined to both ++ a value (pre or post) and use it again within the same expression. In this case it appears that ++i is being evaluated before anything else and causing the execution to essentially be

c[1] = 1;
c[2] = 2;
...

This means c[0] is never initialized and instead has essentially a garbage value. It seems like the order you want is

c[0] = 0;
c[1] = 1;

To get this ordering you'll need to separate the initialization and increment into separate statements.

c[i] = i;
i++;

Your fundamental problem is how the statements in the for(;;) structure get broken down and executed. The for(st1; st2; st3) structure is intended to be identical to:

st1;
while (st2) {
    <body>
    st3;
}

Therefore, your 3rd statement, c[i] = i++ , gets executed after the printf statement and you're printing uninitialized data.

The pre-increment vs. post-increment issue is obscuring this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM