0

I have set an int array size of "20" in my code , but when filling it with for loop i noticed i can fill it up to 24 times ! ( i up to 23 , starting with 0 ) before a message shows up : *** stack smashing detected ***: terminated Aborted (core dumped) , so why that happens ? code :

int marks[20], i;

for (i = 0; i <= 23; i++) {
  printf("enter la note de letudiant ! \n");
  scanf("%d", & marks[i]);
}

for (i = 0; i <= 23; i++) {
  printf("%d\n", marks[i]);
}
h0r53
  • 3,034
  • 2
  • 16
  • 25
abdo
  • 1
  • 1
  • 1
    C gives you the freedom to do many things in memory that you likely shouldn't do. An array is just a memory location storing sequential elements of the same size. You can go beyond the bounds of the array if you wish, but you'll be overwriting other data, possibly variables, function addresses, or anything really. In C this is considered undefined behavior. Stack smashing means you've written beyond the stack frame, which you typically don't want to happen. – h0r53 Nov 19 '21 at 18:43
  • A buffer​ overflow occurs when the user input exceeds the buffer capacity. . In such a ​case, the compiler will throw the stack smashing detected error. – vegan_meat Nov 19 '21 at 18:46
  • @shreydeshwal that's the correct idea, but the particular verbiage used isn't exactly correct. The compiler doesn't throw the stack smashing error, it's a runtime error that is thrown because the stack canary was overwritten. Also, just because you overflow a buffer doesn't mean you'll receive a stack smashing error. – h0r53 Nov 19 '21 at 18:49
  • Thanks you very much , this cleared my doubt , thanks ! – abdo Nov 19 '21 at 18:50

0 Answers0