Case study
Case 1:
struct A { ...}
void Caller()
{
struct A b=getA(); (1)
...
}
struct A getA()
{
struct A a;
a.xxx=xxx
....
return a;
}
The (1)
getA() really return the address of a, the address in the stack which is destroyed, but now no one is using this stack area.
= is the key part in this problem. = will cause the value copy (bitwise copy for the struct, copy construct for the object)
when (1) finished, the b get the value copy from a.
So the object or struct value returned in the function will caused a copy. It is not a good design if the large object returned from function.
Case 2:
struct A { ...}
void Caller()
{
struct A* b=getA(); (1)
...
}
struct A* getA()
{
struct A a;
a.xxx=xxx
....
return &
}
This is a bug refereed by lee, because the destroyed stack address is used in the caller.
Case 3:
struct A { ...}
void Caller()
{
struct A b=*(struct A*)getA(); (1)
...
}
struct A* getA()
{
struct A a;
a.xxx=xxx
....
return &
}
This may be the same as the case 1, but only valid for the structure. It will be failed it an object is returned unless * operation is overloaded.
The object returned in Java is more simple.
All the object are allocate from heap, so do not worry about the object in the stack.
The reference (object handle) is value copy. It is simple.
分享到:
相关推荐
技术运维-机房巡检表及巡检说明
第四次算法分析与设计整理
图像处理项目实战
该资源为jaxlib-0.4.18-cp311-cp311-macosx_11_0_arm64.whl,欢迎下载使用哦!
搭建说明. 运行环境 php5.6 mysql5.6 扩展sg11 前置条件: 前后端分离,需要准备两个域名,一个后台域名,一个前端域名 后端源码修改(cs2.ijiuwu.com批量替换改为你的后端域名)数据库修改(cs3.ijiuwu.com批量替换为你的前端域名)1、创建后台站点,上传后台源码并解压到根目录2、创建前端站点,上传前端源码并解压到根目录 3、创建数据库上传并导入数据库文件 4、修改数据库信息: 后台:app/database.php 前端:application/database.php 前端站点设置 伪静态thinkphp 运行目录public 关闭防跨站 访问后台域名/admin.php进入后台管理 admin 123456 系统-》系统设置-》附件设置-》Web服务器URL 改为你的前端域名 系统-》清前台缓存 改为你的前端域名 点击刷新缓存
【毕业答辩】爆款黑板风教育文艺毕业论文答辩通用模板.pptx
1、文件内容:systemd-devel-219-78.el7_9.9.rpm以及相关依赖 2、文件形式:tar.gz压缩包 3、安装指令: #Step1、解压 tar -zxvf /mnt/data/output/systemd-devel-219-78.el7_9.9.tar.gz #Step2、进入解压后的目录,执行安装 sudo rpm -ivh *.rpm 4、更多资源/技术支持:公众号禅静编程坊
win32汇编环境,对 WM-MOUSEMOVE 消息的理解
车牌识别项目
UE项目开发过程中的一些快捷脚本
lab1的words.txt文件
python、yolo、pytorch
人工智能、大语言模型相关学习资料
图像处理项目实战
python、yolo、pytorch
车牌识别项目
该资源为jaxlib-0.4.18-cp312-cp312-macosx_10_14_x86_64.whl,欢迎下载使用哦!
python、yolo、pytorch
Swift-IOS TODO_List应用开发
图像处理项目实战