C动态内存与堆栈内存变量

时间:2022-09-15 03:10:23

When a C array is created using malloc, the array is stored in the heap, and when a C array is created statically it is stored in the stack. However, what happens if you return an element of a C array initialized statically from a function? My wording may be strange, so here is an example:

使用malloc创建C数组时,数组存储在堆中,当静态创建C数组时,它存储在堆栈中。但是,如果从函数中返回静态初始化的C数组元素,会发生什么?我的措辞可能很奇怪,所以这里有一个例子:

#include <stdio.h>
#include <stdlib.h>
char getStaticElement();
char getDynamicElement();

int main() {
    char dynamicElement = getDynamicElement();
    char staticElement = getStaticElement();
    printf("Dynamic Element: %c\n", dynamicElement);
    printf("Static Element: %c\n", staticElement);
    return 0;

}
char getStaticElement() {
    char staticArray [] = {'a','b','c'};
    return staticArray[1]; // returns b
}
char getDynamicElement() {
    char * dynamicArray = malloc(sizeof(char)*4);
    dynamicArray [0] ='a';
    dynamicArray [1] ='b';
    dynamicArray [2] ='c';
    dynamicArray [3] ='\0';
    return dynamicArray[1]; // returns b
}

So where is staticElement in memory? Is staticArray cleared off the stack because the function has finished, or is it still on the Stack because and element of staticArray has been returned?
NOTE: I know I did not free dynamic array, and am leaking memory, this is just an example and not meant to be used.

那么内存中的staticElement在哪里? staticArray是否已从堆栈中清除,因为函数已完成,或者它是否仍在堆栈上,因为已返回staticArray的元素?注意:我知道我没有释放动态数组,并且正在泄漏内存,这只是一个例子,并不打算使用。

1 个解决方案

#1


3  

In C, if you return a char or int from a function, you're returning a copy of that value from the function, not the actual object itself. In other words, if you return an element of an array you declared as a local variable, you're really returning a copy of that array element, so even when the original array is destroyed there's no worry that you somehow "lose" that array element. Similarly, if you return an element of an array allocated with malloc, you're returning a copy of the array element rather than the element itself. This is why you're not getting garbage values back in the above code.

在C中,如果从函数返回char或int,则从函数返回该值的副本,而不是实际的对象本身。换句话说,如果你返回一个你声明为局部变量的数组的元素,你真的会返回该数组元素的副本,所以即使原始数组被销毁,也不用担心你会以某种方式“丢失”该数组元件。类似地,如果返回使用malloc分配的数组元素,则返回数组元素的副本而不是元素本身。这就是为什么你没有在上面的代码中得到垃圾值。

The value that's handed back is not necessarily stored in either the stack or the heap. It's usually stored in a register somewhere. From the language's point of view it has automatic storage duration and so will get cleaned up automatically. Since you're storing that value in a local variable, it will be stored on the stack (or, more technically, it has automatic storage duration), but that's because you put it in a local variable and has nothing to do with the fact that it was originally in a dynamically- or statically-allocated array.

传回的值不一定存储在堆栈或堆中。它通常存储在某个寄存器中。从语言的角度来看,它具有自动存储持续时间,因此将自动清理。由于您将该值存储在本地变量中,因此它将存储在堆栈中(或者,从技术上讲,它具有自动存储持续时间),但这是因为您将其放在局部变量中并且与事实无关它最初是在动态或静态分配的数组中。

That said, you are leaking memory, since you never free the data that you allocated with malloc.

也就是说,你正在泄漏内存,因为你永远不会释放你用malloc分配的数据。

#1


3  

In C, if you return a char or int from a function, you're returning a copy of that value from the function, not the actual object itself. In other words, if you return an element of an array you declared as a local variable, you're really returning a copy of that array element, so even when the original array is destroyed there's no worry that you somehow "lose" that array element. Similarly, if you return an element of an array allocated with malloc, you're returning a copy of the array element rather than the element itself. This is why you're not getting garbage values back in the above code.

在C中,如果从函数返回char或int,则从函数返回该值的副本,而不是实际的对象本身。换句话说,如果你返回一个你声明为局部变量的数组的元素,你真的会返回该数组元素的副本,所以即使原始数组被销毁,也不用担心你会以某种方式“丢失”该数组元件。类似地,如果返回使用malloc分配的数组元素,则返回数组元素的副本而不是元素本身。这就是为什么你没有在上面的代码中得到垃圾值。

The value that's handed back is not necessarily stored in either the stack or the heap. It's usually stored in a register somewhere. From the language's point of view it has automatic storage duration and so will get cleaned up automatically. Since you're storing that value in a local variable, it will be stored on the stack (or, more technically, it has automatic storage duration), but that's because you put it in a local variable and has nothing to do with the fact that it was originally in a dynamically- or statically-allocated array.

传回的值不一定存储在堆栈或堆中。它通常存储在某个寄存器中。从语言的角度来看,它具有自动存储持续时间,因此将自动清理。由于您将该值存储在本地变量中,因此它将存储在堆栈中(或者,从技术上讲,它具有自动存储持续时间),但这是因为您将其放在局部变量中并且与事实无关它最初是在动态或静态分配的数组中。

That said, you are leaking memory, since you never free the data that you allocated with malloc.

也就是说,你正在泄漏内存,因为你永远不会释放你用malloc分配的数据。