1

The following C program creates an array which has fixed number of elements. Even when we assign three more value which exceeding the number of maximum array elements works fine.

#include <stdio.h>

int main(){

//declares a fixed sized array
int balance[5];

//initializing
balance[0] = 1;
balance[1] = 2;
balance[2] = 3;
balance[3] = 4;
balance[4] = 5;
//three more
balance[5] = 6;
balance[6] = 7;
balance[7] = 8;


printf("%d \n", balance[0]);
printf("%d \n", balance[1]);
printf("%d \n", balance[2]);
printf("%d \n", balance[3]);
printf("%d \n", balance[4]);
printf("%d \n", balance[5]);
printf("%d \n", balance[7]);

//and it works fine!

return 0;
}

But when we try to assign more than three element it throws an exception

#include <stdio.h>

int main(){

//declares a fixed sized array
int balance[5];

//initializing
balance[0] = 1;
balance[1] = 2;
balance[2] = 3;
balance[3] = 4;
balance[4] = 5;
//three more
balance[5] = 6;
balance[6] = 7;
balance[7] = 8;
//one more again
balance[8] = 9;


printf("%d \n", balance[0]);
printf("%d \n", balance[1]);
printf("%d \n", balance[2]);
printf("%d \n", balance[3]);
printf("%d \n", balance[4]);
printf("%d \n", balance[5]);
printf("%d \n", balance[7]);
printf("%d \n", balance[8]);

//throws an exception!

return 0;
}

But after declaring and initializing an array by using integer literals we can insert more elements without getting an error.

#include <stdio.h>

int main(){

//declares and initialize a fixed sized array
int balance[5] = {1,2,3,4,5};

//let's add few more
balance[5] = 6;
balance[6] = 7;

//and even more
balance[7] = 8;
balance[8] = 9;
balance[9] = 10;

printf("%d \n", balance[0]);
printf("%d \n", balance[1]);
printf("%d \n", balance[2]);
printf("%d \n", balance[3]);
printf("%d \n", balance[4]);
printf("%d \n", balance[5]);
printf("%d \n", balance[6]);
printf("%d \n", balance[7]);
printf("%d \n", balance[8]);
printf("%d \n", balance[9]);

//and it works fine!

return 0;
}

What is the reason for this behavior of C language?

4
  • 5
    The reason is that all statements that access elements a[5] and beyond are undefined behaviour, which is not a guarantee for failure. Stop trying to probe how far you can go and learn to spot and avoid such cases. Commented May 4, 2015 at 17:10
  • What do you mean "throws an exception"?? Maybe you're compiling C code with a C++ compiler? Commented May 4, 2015 at 17:25
  • In C there is no certain net to catch code when it strays from the rules. No safety features to slow the system down, no training wheels either. If you do not want unexpected results, do not work outside array bounds. Commented May 4, 2015 at 17:37
  • Actually it was a run time error. Thanks for all of your responses. Commented May 4, 2015 at 17:39

4 Answers 4

2

Your code invokes Undefined Behavior as you try to access array out of bounds, which is unsafe memory, and hence the outcome is undefined.

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

Comments

1

You are accessing unallocated memory which will invoke undefined behavior. Anything could happen. These are all out of bound access

balance[5] = 6;
balance[6] = 7;
balance[7] = 8;
balance[8] = 9;

Comments

1

Array out of bound access leads to undefined behavior. Your array can hold 5 integer elements so the valid index for your array is 0 to 4 anything other than this is array out of bound access.

Comments

1

I'd like to give a more generic answer. Everyone here got' the right answer, but i want to explain why & be a little more lengthy about the explanation. Let's say you have a char array of 3 cases.

  char tab[3];

In terms of memory, what we got is a 3 cases of data allocated. Since it's an array, it's alignated in your memory. Let's initialise that array :

tab[0] = 'A';
tab[1] = 'B';
tab[2] = 'C';

In your memory, it looks something like that :

enter image description here

Now, what happens if you write tab[6] ? Since it's C, you can clearly do that, no problem whatsoever. (There are no electric-fence ;) )

In that case, tab[6] has not been allocated to your program. You can have any scenario from now on :

  • Maybe this data is critic to the system, and your program wont have access to that memory (ie Segmentation Fault)
  • Maybe your programm has another array somewhere, and you could get the luck of the draw and actually get something out of there.
  • (others thing worst than that)
  • ???

Basically when you provoke an Out of Bounds error, you cause unexpected behaviour. What you should do is always look after thoses mistakes are they can be deadly killers of your beloved application.

1 Comment

@RasanSamarasinghe you can choose an answer that fits your question then !

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.