No out of bounds error

后端 未结 7 867
既然无缘
既然无缘 2020-11-22 01:01

I have this code in C which takes in bunch of chars

#include 
# define NEWLINE \'\\n\'
int main()
{

char c;
char str[6];
int i =         


        
7条回答
  •  庸人自扰
    2020-11-22 01:38

    1. C doesn't check array boundaries. A segmentation fault will only occur if you try to dereference a pointer to memory that your program doesn't have permission to access. Simply going past the end of an array is unlikely to cause that behaviour. Undefined behaviour is just that - undefined. It may appear to work just fine, but you shouldn't be relying on its safety.
    2. Your program causes undefined behaviour by accessing memory past the end of the array. In this case, it looks like one of your str[i] = c writes overwrites the value in i.
    3. C++ has the same rules as C does in this case.

提交回复
热议问题