堆栈帧内存分配(Stack frame memory allocation)

编程入门 行业动态 更新时间:2024-10-27 05:26:48
堆栈帧内存分配(Stack frame memory allocation)

就像每个函数放在一个堆栈框架上执行,并在完成后刷新一样。 所以,任何局部变量都不会被其他函数使用。 但那么我们怎么能够返回一个局部变量给调用者呢?

int pickMin( int x, int y, int z ) { int min = x ; if ( y < min ) min = y ; if ( z < min ) min = z ; return min ; }

上面的代码工作正常。 但是,在下面的代码中,编译器确实给出了警告消息 - “ warning: function returns address of local variable [-Wreturn-local-addr] return a; ”但它在最后打印一个垃圾值,我认为这很好因为变量已经被刷新。 但是为什么没有在上述计划中发生? 我的意思是,它也应该返回给我一个垃圾值。另外,我知道下面的代码中的问题可以使用malloc解决,然后返回该值。 :)

int *returnarray(){ int a[10]; int i; for(i=0;i<10;++i){ a[i] = i; }return a;}

Like every function is put on a stack frame for its execution and it is flushed after its completion. So, any local variable wont be available to other functions. But then how are we able to return a local variable to the caller?

int pickMin( int x, int y, int z ) { int min = x ; if ( y < min ) min = y ; if ( z < min ) min = z ; return min ; }

The above code works fine. However the in the below code, compiler does give a warning message- "warning: function returns address of local variable [-Wreturn-local-addr] return a; " but it prints a garbage value at the end, which I think is fine because the variable has already been flushed. But why didn't that happen in the ABOVE program?! I mean, it should also have returned me a garbage value.Moreover, I know that the problem in the below code can be solved using malloc, and then returning that value. :)

int *returnarray(){ int a[10]; int i; for(i=0;i<10;++i){ a[i] = i; }return a;}

最满意答案

C通过价值传递一切。 在你的第一个片段中, return min返回一个int变量。 它的值被返回。 第二个片段由return和一个数组名称组成,后者衰减为一个指针。 返回的局部变量的内存地址。 然而,这个变量存在的函数已经返回,并且访问这个函数使用的内存然后调用未定义的行为。

处理这种情况(即:需要返回一个数组)的方法是将目标数组作为参数传递给函数,或者使用malloc分配内存并返回该指针。 堆内存比较慢,更容易出错,并且需要你照顾它。 不过, 这是两种方法的一个例子 create_fill分配,分配并返回一个指向堆内存的指针, fill_array不返回任何东西,但期望你传递一个数组(它衰变成一个指针),并且填充一个最大长度。 其优势在于:堆栈内存不需要太多的关注,并且会超越堆。

C passes everything around by value. In your first snippet, return min returns an int variable. Its value is returned. The second snippet consists of return and an array name, which decays into a pointer. The value that is returned, is the memory address of a local variable. The function where this variable existed has returned, though, and accessing the memory that this function used then invokes undefined behaviour.

The way to handle this kind of situation (ie: needing to return an array) is either by passing the target array to the function as an argument, or by allocating the memory using malloc and returning that pointer. Heap memory is a tad slower, more error prone, and requires you to look after it though. Still, here's an example of both approaches create_fill allocates, assigns and returns a pointer to the heap memory, fill_array doesn't return anything, but expects you to pass an array (which decays into a pointer), and a max length to fill. The advantage being: stack memory doesn't require as much care, and will outperform the heap.

更多推荐

本文发布于:2023-08-07 22:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466036.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:堆栈   分配   内存   Stack   memory

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!