当前位置:网站首页>Parameter stack pressing problem of C language in structure parameter transmission

Parameter stack pressing problem of C language in structure parameter transmission

2022-04-23 14:25:00 KissKernel

First, in memory , It is divided into three areas , The stack area , Heap area , And static area . Local variables are stored in the stack area , Shape parameter , And the space opened up by function calls . Heap area is mainly about dynamic memory allocation . The static area is mainly used to store global variables and static variables .

The problem of parameter stack pressing is put forward in the place of structure parameter transmission ,c When a function passes parameters in a language , Parameters need to be stacked . If you pass a structure object, the structure is too large , The system overhead of parameter stack pressing is too high , Performance will decline . It involves the creation and destruction of function stack frames .

The following reference takes simple code as an example

#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;
}

The code is abstracted in memory as shown in the figure

 b Pass the reference to y It is equivalent to copying a copy in the stack area b.c It's the same thing . This is just passing a variable to the function , If b Become a structure , Because the structure is too large, it will consume too much memory in the stack area , Performance degradation . We usually use the method of address transmission .

版权声明
本文为[KissKernel]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231412252056.html