Not null-terminating a C-style string

后端 未结 6 2109
萌比男神i
萌比男神i 2021-01-24 12:08

Given a string , say ,

char *str = \"Hello,StackOverflow!\"
char newStr[30];
int l = strlen(str);
for(int i =0 ; i

        
6条回答
  •  青春惊慌失措
    2021-01-24 12:38

    A "crash" is not guaranteed. A program that improperly handles null terminators in strings - more generally accesses data outside of buffer boundaries - or violates the printf format string, may seem to work just fine, functioning and not e.g. causing a segfault. But this is just happenstance: the behavior of your code is undefined.

    It will be the same in C++.

提交回复
热议问题