-3

In the code below, I insert 6 elements in the array. The array size however is 5 and the for loop runs 7 times. The code then inserts and print the 7 elements.

It seems it doesn't matter what the size of the array is since I can apparently insert and print more elements. If that's the case, then why even declare a size for the array?

#include<stdio.h>
#include<stdlib.h>


int main()
 {
   int index, block[5], number;

   printf("\nEnter number  of elements which you want to append :");
   scanf("%d", &number);
   printf("\nEnter the values :");
   for (index = 0; index <= number; index++)
   {
       scanf("%d", &block[index]);
   }
   for (index = 0; index <= number; index++)
   {
       printf("%d\t", block[index]);
   }
   return (0);
}

Output:

Enter the values :1
2
3
4
5
6
7
1   2   3   4   5   6   7
dbush
  • 205,898
  • 23
  • 218
  • 273
  • 1
    C language is giving the programmer almost the full freedom, even if something is against the rules of the language (like writing outside the array bounds). You can, but the language itself does not guarantee what will happen with the program. It is called Undefined Bahavior which may be a great but dangerous tool in some circumstances or the curse in other (actually 99.99% of the cases or 100% for beginner programmers) – 0___________ Aug 14 '18 at 12:16
  • 1
    Every night at 3am I walk from my house to the all-night diner across the street. There's a Walk / Don't Walk sign at the intersection, but there's never any traffic, and I got tired of waiting, so I started crossing even though the sign says Don't Walk. Nothing bad ever happens. What's the point of having the Walk / Don't Walk sign if I can cross whenever I like? – Steve Summit Aug 14 '18 at 14:24
  • Please use the linked canonical duplicate for FAQ questions like this. I think this was the 3rd "why can I access the array out of bounds" question today alone. – Lundin Aug 14 '18 at 14:58

3 Answers3

4

The C language has the concept of undefined behavior. It means the language doesn't prevent you from doing things you aren't supposed to do, but if you do there's no guarantee what the program will do afterwards.

It may crash, it may output strange results, or it may appear to work normally. How undefined behavior manifests itself can change if you make a seemingly unrelated code change, such as adding an unused local variable, adding a printf call for debugging purposes, or compiling with different optimizations settings.

In this case, you got "lucky" that nothing bad seemed to happen. Someone else running the same program may get different output from you because you wrote past the end of the array.

When I ran this code I got the following output:

Enter number  of elements which you want to append :6

Enter the values :1
2
3
4
5
6
7
*** stack smashing detected ***: ./x1 terminated
1   2   3   4   5   6   7   Aborted (core dumped)
dbush
  • 205,898
  • 23
  • 218
  • 273
1

This code results in undefined behaviour because, there is an attempt to access elements outside of array. There are two options to overcome this issue:

  1. block[X] - X should be defined with fixed size. Value provided by user should be compared with X - then You are sure, You are not jump outside of array size.
  2. block[X] - array is created dynamically with X provided by user (check malloc() function)
WedaPashi
  • 3,561
  • 26
  • 42
0

In C arrays don't have boundary check. You have array of int it's width is 5. It doesn't stop at only five elements if you add more than five elements to the array that is undefined behaviour.

It's user responsibility to make sure array boundaries doesn't exceed than allocated.

danglingpointer
  • 4,708
  • 3
  • 24
  • 42