0

In c program we can initialize an array like int array[10]. So it can store 10 integer value.But when I give input using loop it takes input more than 10 and doesn't show any error. actually what is happening??

#include<stdio.H>
main()
{
 int array[10],i;
 for(i=0;i<=11;i++)
 scanf("%d",&array[i]);
 for(i=0;i<10;i++)
 printf("%d",array[i]);
} 
Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
  • Can you post your code? – Boop Aug 09 '14 at 17:43
  • Hard to tell without any code. In general, if you access an array out-of-bounds, all bets are off. It's your responsibility to take care not to do so. – mafso Aug 09 '14 at 17:43
  • Could you give us your code or a snippet of where you are inserting the elements as this is very ambiguous – c0d3junk13 Aug 09 '14 at 17:43
  • 1
    If the case of a local array - stack overflow (how ironic)... In the case of a global array - overriding some other piece of global data, or performing an illegal memory access... – barak manos Aug 09 '14 at 17:44
  • @barakmanos: I think you may be confusing stack overflow with stack smashing. They are not quite the same. – NPE Aug 09 '14 at 17:58
  • @NPE: Yeah, you are generally correct (unless the array happens to be exactly at the end of the stack **every time** the function is called, in which case my answer would also be correct). The comment was really just to point out the irony... In any case, I wrote-down a proper answer without referring to the SO issue, which is not accurate as you mentioned. Thanks for pointing that out :) – barak manos Aug 09 '14 at 18:04

3 Answers3

7

Because C doesn't do any array bounds checking. You as a programmer are responsible for making sure that you don't index out of bounds.

Depending on the used compiler and the system the code is running on, you might read random data from memory or get a SIGSEGV eventually when reading/writing out of bounds.

Julian
  • 757
  • 1
  • 6
  • 21
3

The C compiler and the runtime are not required to perform any array bounds checking.

What you describe is an example of a whole class of programming errors that result in undefined behavior. From Wikipedia:

In computer programming, undefined behavior refers to computer code whose behavior is specified to be arbitrary.

What this means is that the program is allowed to misbehave (or not) in any way it pleases.

In practice, any of the following are reasonably likely to happen when you write past the end of an array:

  • The program crashes, either immediately or at a later point.
  • Other, unrelated, data gets overwritten. This could result in arbitrary misbehaviour and/or in serious security vulnerabilities.
  • Internal data structures that are used to keep track of allocated memory get corrupted by the out-of-bounds write.
  • The program works exactly as if more memory had been allocated in the first place (memory is often allocated in block, and by luck there might happen to be some spare capacity after the end of the array).

(This is not an exhaustive list.)

There exist tools, such as Valgrid, that can help discover and diagnose this type of errors.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
1

The C-language standard does not dictate how variables should be allocated in memory.

So the theoretical answer is that you are performing an unsafe memory access operation, which will lead to undefined behavior (anything could happen).

Technically, however, all compilers allocate local variables in the stack and global variables in the data-section, so the practical answer is:

  • In the case of a local array, you will either override some other local variable or perform an illegal memory access operation.

  • In the case of a global array, you will either override some other global variable or perform an illegal memory access operation.

barak manos
  • 29,648
  • 10
  • 62
  • 114