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]); } 
6
  • Can you post your code? Commented Aug 9, 2014 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. Commented Aug 9, 2014 at 17:43
  • Could you give us your code or a snippet of where you are inserting the elements as this is very ambiguous Commented Aug 9, 2014 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... Commented Aug 9, 2014 at 17:44
  • @barakmanos: I think you may be confusing stack overflow with stack smashing. They are not quite the same. Commented Aug 9, 2014 at 17:58

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.