[Solved] Array has more values than expected


for(int i = 0; ; i++)

When does your loop end?!?!? Your second condition in for (the testing expression) is evaluated as true (at least as long as i<4). You need

for(int i = 0; i < 4 ; i++)

Otherwise it may break anytime after i>=4, but not before, since all components of a are non zero for i<4 (a[i] = i + 1;), and after i==4 you have undefined behaviour as you didn’t allocate enough memory for a.

Also, main should return int according to the C++ standard.

PS: For a vast majority of simple examples, most of the time you don’t even need a debugger, just simulate in your head, or on a piece of paper, what the program is doing, line by line, and you’ll catch the error.

8

solved Array has more values than expected