当前位置:网站首页>c语言在结构体传参时参数压栈问题
c语言在结构体传参时参数压栈问题
2022-04-23 14:13:00 【KissKernel】
首先在内存中,分为三大区域,栈区,堆区,以及静态区。栈区中存放有局部变量,形参,以及函数调用所开辟的空间。堆区主要是关于动态内存的分配。静态区主要是存放全局变量和静态变量。
关于参数压栈的问题是在结构体传参这个地方提出的,c语言中函数传参时,参数是需要压栈的。如果传一个结构体对象结构体过大,参数压栈的系统开销过大,性能就会下降。涉及的就是函数栈帧的创建和销毁。
下面引用以简单代码为例
#include<stdio.h>
int add(int x, int y)
{
int z = 0;
z = x + y;
return z;
}
int main()
{
int a = 20, b = 10;
int ret = 0;
ret = add(a, b);
return 0;
}
代码在内存中如图抽象
b传参给y时相当于在栈区中拷贝了一份b。c也是一样的道理。这只是给函数传一个变量,如果b变为结构体,就会因为结构体过大而造成栈区内存消耗多,导致性能下降。所以我们在结构体传参时通常采用传地址的方法。
版权声明
本文为[KissKernel]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_62745420/article/details/121779865
边栏推荐
猜你喜欢
随机推荐
ie8 浏览器提示是否 阻止访问js脚本
SSH 通过跳板机连接远程主机
After entering the new company, the operation and maintenance engineer can understand the deployment of the system from the following items
Thread group ThreadGroup uses introduction + custom thread factory class to implement threadfactory interface
Use the executors class to quickly create a thread pool
setcontext getcontext makecontext swapcontext
剑指offer刷题(2)--面向华为
Five ways of using synchronized to remove clouds and fog are introduced
微信小程序轮播图swiper
xx项目架构随记
Tongxin UOS uninstall php7 2.24, install php7 4.27 ; Uninstall and then install PHP 7.2.34
How does void * exist?
关于UDP接收icmp端口不可达(port unreachable)
STD:: map and STD:: vector memory free
Returns the subscript after array sorting
squid代理
flannel 原理 之 子网划分
flannel 原理 之 TUN模式
DP energy Necklace
kprobe 的 3 种使用