-4

Debug why the below mentioned code goes into an infinite loop

int a[10]; 
int i; 
for(i=0;i<=10;i++) { 
   a[i]=0; 
}

i am not able to find any valid explanation for it. Although one possibility is a[9] pointing back to i ... but it doesn't seem convincing

Tushar Saha
  • 1,978
  • 24
  • 29
  • 3
    should be `i<10` or you get out of bounds array access. It is probably overwriting the variable `i` at that time. – Rishikesh Raje Dec 16 '16 at 09:41
  • Possible duplicate of [C++ compilation bug?](http://stackoverflow.com/questions/32506643/c-compilation-bug) – LogicStuff Dec 16 '16 at 09:41
  • 1
    As you have allocated 10 digit initially and in the for loop you are assigning the 11th member that is a[10]. So change i < 10. – Swapnil Dec 16 '16 at 09:42
  • Both `a` and `i` are stored on stack, possibly next to each other. You can check the address of `a[10]` and `i`. If both are same then `i` has been overwritten. – sameerkn Dec 16 '16 at 09:54
  • In order to get hired, you must add a boundary check: `for(i=0;i<=10;i++) int val = 0; if(i > 10) goto fail; a[i]=val; ` – Lundin Dec 16 '16 at 10:50
  • @Lundin not to funny, you skipped the `{brackets}` thus making the code non-compiling because `val` is used out of scope. – grek40 Dec 16 '16 at 11:30
  • @grek40 Apple requires that the code must compile? Awww :( – Lundin Dec 16 '16 at 12:02
  • @grek40 Fixed: `int val; for(i=0;i<=10;i++) val = 0; if(i >= 10) goto fail; a[i]=val;`. – Lundin Dec 16 '16 at 12:04

4 Answers4

7

Classic side effect of "Buffer overflow". In this case what is happening is that the value of i is getting overwritten. Check the range of variable a it's an array of 10 bytes 0 through 9. However you loop for 11 bytes 0 through 10.

Change the loop as

int a[10]; 
int i; 
for(i=0;i<10;i++) { 
   a[i]=0; 
}

and it won't go in infinite loop. Again this is a problem of "buffer overflow" and can have undefined behavior. In your case that undefined behavior is infinite loop.

A.N
  • 541
  • 2
  • 13
  • `overwriting the value of i.`...can you explain? – Sourav Ghosh Dec 16 '16 at 09:42
  • 1
    Check my post at http://stackoverflow.com/questions/41158314/memory-confusion-for-strncpy-in-c/41159101#41159101 which explains a similar issue but different side effect. – A.N Dec 16 '16 at 09:45
2

For the value of i equals to 10,

 a[i]=0;

is off by one access. It invokes undefined behavior. Anything can happen.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 1
    I'm so sick of the C UB "Anything can happen" attitude. While it is true for the language standard, most people don't execute the standard but a compiled program in a specific OS and both the compiler and the OS are free to define parts of the language UB (like not allowing a program to make demons fly out of your nose when executed in user mode). Just had to mention this somewhere :) – grek40 Dec 16 '16 at 09:56
  • @grek40 Then, a question expecting that sort of answer should contain the additional information, like platform, compiler, optimization and likewise. Without that, you (we all) have to sate with UB. – Sourav Ghosh Dec 16 '16 at 10:00
2

This code has undefined behavior. But if it is going into infinite loop the most suitable explanation would be that a[10] = 0 is overwriting i with 0, since i is defined immediately after a, therefore, in memory is most probably placed as a contiguous element after array elements.

nishantsingh
  • 4,537
  • 5
  • 25
  • 51
0

You could try this so that you don't get undefined behavior:

int i, a[10];
for(i = 0; i <= 9; i++) 
    a[i]=0;
MD XF
  • 7,860
  • 7
  • 40
  • 71
Abel Tom
  • 145
  • 2
  • 8