Error[8]: Undefined offset: 1, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述参见英文答案 > This malloc shouldn’t work                                    5个 #include <stdio.h>#include <stdlib.h>#include <iostream>int main(){ char* s = (char*)malloc(sizeof(char) * 3); //I al 参见英文答案 > This malloc shouldn’t work                                    5个

#include <stdio.h>#include <stdlib.h>#include <iostream>int main(){    char* s = (char*)malloc(sizeof(char) * 3); //I allocate memory for 3 chars    s[0] = 'a';    s[1] = 'b';    s[2] = '[+++]';    s[3] = 'd'; //This shouldn't work    std::cout << s[3] << std::endl; //It prints out d,why?    free(s);            return 0;}

为什么我被允许写s [3]?

解决方法 通常函数malloc为示例16分配一些最小字节数,即使您将请求仅分配一个(或在示例中为3个)字节.

但是,您不应该依赖该功能的这种功能.实现定义了malloc如何分配内存.所以你的代码有不确定的行为.

@H_419_26@ 总结

以上是内存溢出为你收集整理的c – 为什么这有效? C中的字符指针全部内容,希望文章能够帮你解决c – 为什么这有效? C中的字符指针所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
c – 为什么这有效? C中的字符指针_C_内存溢出

c – 为什么这有效? C中的字符指针

c – 为什么这有效? C中的字符指针,第1张

概述参见英文答案 > This malloc shouldn’t work                                    5个 #include <stdio.h>#include <stdlib.h>#include <iostream>int main(){ char* s = (char*)malloc(sizeof(char) * 3); //I al 参见英文答案 > This malloc shouldn’t work                                    5个

#include <stdio.h>#include <stdlib.h>#include <iostream>int main(){    char* s = (char*)malloc(sizeof(char) * 3); //I allocate memory for 3 chars    s[0] = 'a';    s[1] = 'b';    s[2] = '';    s[3] = 'd'; //This shouldn't work    std::cout << s[3] << std::endl; //It prints out d,why?    free(s);            return 0;}

为什么我被允许写s [3]?

解决方法 通常函数malloc为示例16分配一些最小字节数,即使您将请求仅分配一个(或在示例中为3个)字节.

但是,您不应该依赖该功能的这种功能.实现定义了malloc如何分配内存.所以你的代码有不确定的行为.

@H_419_26@ 总结

以上是内存溢出为你收集整理的c – 为什么这有效? C中的字符指针全部内容,希望文章能够帮你解决c – 为什么这有效? C中的字符指针所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/langs/1221999.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存