-4

I've brushing up on my C coding and going over some old exercises I did a couple of years back. I came accros a situation I am almost 100% sure it should give a segmentation fault, but instead the program runs smoothly and terminates correctly. Why is that happening?

#include <stdio.h>

int main(void){
    int vals[6] = {0,0,0,0,0,0};

    vals[8]++; //This should not be ok!!?

    printf("Done");
    return 0;
}
AmiguelS
  • 805
  • 2
  • 10
  • 28
  • 3
    There is no place where a segmentation fault should happen. – Iharob Al Asimi Feb 16 '16 at 12:18
  • How is accessing a non-existing position of an array not a segmentation fault? – AmiguelS Feb 16 '16 at 12:19
  • 5
    It's *undefined behavior*, whether it's a segmentation fault depends on other things. Oh, and there is no **vector** in your code, are you maybe thinking [tag:c++]? – Iharob Al Asimi Feb 16 '16 at 12:19
  • 1
    @iharob Damn objects oriented languages messing with my C :p – AmiguelS Feb 16 '16 at 12:21
  • @iharob: Old K&R used the term "vector" for arrays. – too honest for this site Feb 16 '16 at 12:26
  • Oh, again K&R. The thing is, one of them is responsible for me being unable to quit programming, so I love him. And if he called them "*vectors*" (*he was a physicist and I am not surprised*) maybe you should to. – Iharob Al Asimi Feb 16 '16 at 12:28
  • Undefined behavior is behavior which is not defined. – Lundin Feb 16 '16 at 12:29
  • @iharob: K&R (not the ANSI-version) was the first and only C book I bought. After some first steps, I threw it into the corner and moved to Modula-2, mostly because of the extremely weak typing and other quirks. So I'm actually more a Wirth-man. I moved to C only with the first ANSI version which at least had a little reasonable behaviour and typing/checking. I still think some more hardware-oriented Modula-2 would be the better PL. – too honest for this site Feb 16 '16 at 12:32
  • @Olaf I am not referring to the book, but to the creator of the [tag:c] language. – Iharob Al Asimi Feb 16 '16 at 12:33
  • @iharob: I actually refered to the book. Actually there were two creators. But I think you are right why they choose "vector". – too honest for this site Feb 16 '16 at 12:34
  • Indeed. Although physicists themselves tend to misuse the term *vector*. Mathematically, a *vector* is a one-dimensional tensor, that is it has to be invariant under tensor coordinate transfomations. I'd prefer the term *array* as they are just a bunch of numbers. – Bathsheba Feb 16 '16 at 12:36
  • @Olaf [Brian Kernighan](https://en.wikipedia.org/wiki/Brian_Kernighan) denied having anything to do with the creation of C, so there's just one creator. – Lundin Feb 16 '16 at 12:37
  • @Lundin: Thanks, I seem to have missed something then. – too honest for this site Feb 16 '16 at 12:41

1 Answers1

4

The behaviour of vals[8] is undefined.

It's equivalent to *(vals + 8) which is dereferencing memory outside the bounds of the array.

A "segmentation fault" is one of many things that could happen. The compiler could also eat your cat.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483