简体   繁体   English

静态、堆栈和堆分配有何不同?

[英]How are static, stack, and heap allocation different?

How can i declare an array like this one:我如何声明一个这样的数组:

int array[1000000];

as a static array, a stack array, and a heap-allocated array?作为静态数组、堆栈数组和堆分配数组?

Your assignment appears to be looking for this:您的作业似乎正在寻找以下内容:

// global variable; NOT on the stack. Exists in the data segment of the program
int globalvar[1000000];

void func()
{
    // local stack-variable. allocated on the stack on function entry
    //  unavailable outside this function scope.
    int stackvar[1000000];

    // allocated on the heap. the only stack space used in the
    //  space occupied by the pointer variable.
    int *heapvar = malloc(1000000 * sizeof(int));
    if (heapvar != NULL)
    {
        // use heap var

        // free heap var
        free(heapvar)
    }
}

Or perhaps this:或者这个:

void func()
{
    // static variable; NOT on the stack. Exists in a program data segment (usually)
    static int staticvar[1000000];
    
    // local stack-variable. allocated on the stack on function entry
    //  unavailable outside this function scope.
    int stackvar[1000000];

    // allocated on the heap. the only stack space used in the
    //  space occupied by the pointer variable.
    int *heapvar = malloc(1000000 * sizeof(int));
    if (heapvar != NULL)
    {
        // use heap var

        // free heap var
        free(heapvar)
    }
}

For what it is worth, unless you have a four or eight megabyte reserved call-stack (or larger), the function above will likely croak on entry.就其价值而言,除非您有 4 或 8 兆字节的保留调用堆栈(或更大),否则上面的函数可能会在进入时发出咝咝声。 For such large sizes it is customary to use the heap ( malloc() / free() ).对于如此大的尺寸,习惯上使用堆( malloc() / free() )。 But that is not what your assignment appears to be about (yet).但这不是你的任务(目前)的内容。

A static declaration inside functions means, that the declared variable is shared among executions of the function it is declared in. The stack is a place in memory that may be used by whatever function is running right now;函数内部的静态声明意味着,声明的变量在声明它的函数的执行之间共享。堆栈是内存中的一个地方,可以被任何正在运行的函数使用; there is no way to protect an area on the stack from being overwritten while your function is not running.当您的函数未运行时,无法保护堆栈上的区域不被覆盖。 Static variables are usually stored either in the data or if unitialized into the bss section of your program.静态变量通常存储在数据中,或者如果单元化到程序的 bss 部分中。 If you have the strict requirement to have the array in the stack, you could try copying it:如果您对堆栈中的数组有严格要求,您可以尝试复制它:

void foo(void) {
   static int my_static_array[16];
   int array_copy[16];

   memcpy(array_copy,my_static_array,sizeof array_copy);

   /* do funny stuff */

   memcpy(my_static_array,array_copy,sizeof my_static_array);
}

A static variable cannot be on the stack, this is because static and local variables are fundamentally different, with local variables "living" in the stack while static variables "living" in the static segment.静态变量不能在堆栈上,这是因为静态变量和局部变量根本不同,局部变量“活”在堆栈中,而静态变量“活”在静态段中。 If you want a local variable to be visible to a function called by the function in which the local variable is declared, then you should pass that local variable as an argument.如果希望局部变量对声明局部变量的函数调用的函数可见,则应将该局部变量作为参数传递。 an other solution not recommended is to have a static pointer to an array and have it point to the array existing in the stack, this will work as long as the function in which the local array is declared, has not returned.另一个不推荐的解决方案是有一个指向数组的静态指针并让它指向存在于堆栈中的数组,只要声明本地数组的函数没有返回,这就会起作用。 After returning though the pointer will point to an area that other data may exist meaning that it is possible to overwrite a return address or an irrelevant local variable or function argument.返回后,指针将指向可能存在其他数据的区域,这意味着可以覆盖返回地址或不相关的局部变量或函数参数。

如果你想让它公开array ,你可以在任何范围之外(在代码块之外)定义它,它将在二进制文件的文本段上声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM