cycle for does not stop

1

This code is wrong and I do not know why, I want to do a array[5]={1,2,3,4,5,6} and show it on the screen.

int main(void){

    int arr[5];
    arr[0]=1;
    arr[1]=2;
    arr[2]=3;
    arr[3]=4;
    arr[4]=5;
    arr[5]=6;

    for(int i=0; i<=5; i++) cout<<arr[i]<<endl;

return 0;}

The program runs, but it shows this:

position: 0 data: 1
position: 1 data: 2
position: 2 data: 3
position: 3 data: 4
position: 4 data: 5
position: 5 data: 0
position: 6 data: 4196822
position: 7 data: 0
position: 8 data: 0
position: 9 data: 0
position: 10 data: 0
position: 11 data: 0
position: 12 data: 4196822
position: 13 data: 0
position: 14 data: -982238395
position: 15 data: 27786
...
    
asked by Andres Camilo Sierra Hormiga 21.10.2016 в 07:14
source

5 answers

1

The problem with your code is due to the following lines:

int arr[5];
arr[0]=1;
arr[1]=2;
arr[2]=3;
arr[3]=4;
arr[4]=5;
arr[5]=6;

The previous code does the following:

1) Create an array of 5 integers, this means that the program asks the Operating System to give it a memory space (contiguous) to store 5 integers, in 5 different 'boxes'. 2) With this memory space the program assigns to the first box the number 1, to the second box the number 2, ..., to the fifth box the number 5 and to the sixth box the number 6 !.

The problem here is that you asked the Operating System space to store 5 variables of the whole type contiguously but you are accessing a space beyond the reserved (a sixth value). While this region is in the limits of all the memory space reserved for your program, the code will continue to 'work', the drawback is that beyond the space that the SO reserved (known as stack) are other important variables of your program, including those that control the correct flow of the for cycle (most likely the value that variable i takes for the condition i

answered by 21.10.2016 в 07:59
0

Without executing your code, only by eye, the array is badly declared. If you do int arr [5], the maximum index is arr [4] (starts counting by 0). If you want an array for 6 elements, you should do int arr [6].

It would be a matter of looking at it with a debugger, but maybe that index that you are accessing out of range, arr [5], is actually overwriting some internal data of the function. Since the array is local to the function, it is created on the stack. That arr [5] is accessing some element of the stack that the compiler uses (some internal temporary variable, the return address, or anything else).

    
answered by 21.10.2016 в 07:43
0

Try the following correction:

int main(void){

int arr[6];
arr[0]=1;
arr[1]=2;
arr[2]=3;
arr[3]=4;
arr[4]=5;
arr[5]=6;

for(int i=0; i < 6; i++)    {cout<<arr[i]<<endl;} return 0;}
    
answered by 21.10.2016 в 08:00
0

The error is that you originally declared an array of size 5, but you assigned 6 values, the pointers go crazy to the point they got with the memory, in C ++ it is typical that this happens. It seems that there is a confusion of the index with the value. A tip with for conditions, use the same size of the array without the equal sign, for example, if you have int array[20] , that the for condition is i < 20

    
answered by 21.10.2016 в 10:55
0

As everyone indicates, your drawback is that you declared an array for 5 elements and placed 6. In addition to this, C ++ places in the N + 1 place an end-of-string character "\ 0" and you are overwriting it with the value 6. This causes the cycle to never stop because it does not find the end of the chain. Anyway, the code that you sample does not match the output that you put. If your code is correct, you should only show the first 5 elements because your "for" is from 1 to 5.

    
answered by 21.10.2016 в 14:37