No out of bounds error

后端 未结 7 677
既然无缘
既然无缘 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

    Memory allocation is more complicated than it seems. The variable "str," in this case, is on the stack, next to other variables, so it's not followed by unallocated memory. Memory is also usually word-aligned (one "word" is four to eight bytes.) You were possibly messing with the value for another variable, or with some "padding" (empty space added to maintain word alignment,) or something else entirely.

    Like R.. said, it's undefined behavior. Out-of-bounds conditions could cause a segfault... or they could cause silent memory corruption. If you're modifying memory which has already been allocated, this will not be caught by the operating system. That's why out-of-bounds errors are so insidious in C.

提交回复
热议问题