为什么这不会返回警告?这段代码应该是什么问题?
char *str = malloc(strlen("hello" + 1));
strcpy(str, "hello");
Run Code Online (Sandbox Code Playgroud)
谢谢!
这个
char *str = malloc(strlen("hello" + 1));
strcpy(str, "hello");
Run Code Online (Sandbox Code Playgroud)
几乎相当于:
char *temp = "hello";
char *temp2 = temp + 1;
char *str = malloc(strlen(temp2));
strcpy(str, "hello");
Run Code Online (Sandbox Code Playgroud)
temp + 1
指针数学也是如此(它返回指针ello
,strcpy
并不检查目标是否有足够的内存(由C中的错误代码引起的"标准"内存损坏)
最终结果是strlen
返回4,strcpy
使用6个字节的内存,并随机删除一堆堆.
归档时间: |
|
查看次数: |
5048 次 |
最近记录: |