Not null-terminating a C-style string

后端 未结 6 2114
萌比男神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条回答
  •  Happy的楠姐
    2021-01-24 12:50

    No. It invokes undefined behavior - it means it doesn't have to crash - it can do literally anything, like nasal demons.

    Also, "gives a runtime error" - well, that depends on what do you mean by a runtime error. There's no dynamic runtime for C - if you expect a nicely formatted error message from an exception, that wouldn't happen. What would happen is most likely a segmentation fault.

    All in all, if one causes/uses undefined behavior, he must not rely on it crashing or not crashing.

提交回复
热议问题