Abort instead of segfault with clear memory violation

后端 未结 6 586
渐次进展
渐次进展 2021-01-13 18:48

I came upon this weird behaviour when dealing with C strings. This is an exercise from the K&R book where I was supposed to write a function that appends one string onto

6条回答
  •  Happy的楠姐
    2021-01-13 19:36

    int main() {
      char s1[] = "hello";
      char s2[] = "eheheheheheh"; 
      printf("%s\n", strcat(s1, s2));
    }
    

    instead use:

       int main() {
      char s1[20] = "hello";
      char s2[] = "eheheheheheh"; 
      printf("%s\n", strcat(s1, s2));
    }
    

提交回复
热议问题