Why would you ever want to have an array on the heap?

Why would you ever want to have an array on the heap? My professor gave us two reasons:

  • To pass the array to functions, instead of passing a copy
  • So that the array outlives the scope
  • Can't these both instead by solved by:

  • Passing a pointer to an array on the stack
  • Returning the value of the array instead of the array itself (ie use the copy constructor)
  • Could someone give me an example of where an array in the heap has to be used?


    Arrays in heap are used to outlive the function's scope. Passing a pointer to an array on the stack is only valid if you don't want to use it later in a previous (upper) caller. And you can't return an array from a function, you can return a pointer to an array, but if it was allocated in stack, it will point to an invalid memory position after the function returns.

    The 1st reason is wrong: arrays are never passed by copy. When you call a function, array names always decay into a pointer to its first element, precisely to avoid copying the whole array. If you want to pass an array by copy, you have to embed it inside a struct and pass a struct instead.

    Dynamic array allocation is also useful if you don't know the size of your array in advance (although this is not true after C99 brought variable length arrays - but still, variable length arrays are alloced on stack, so you'd have the same problem).

    Another good reason to use heap allocation is that you can easily fall out of stack memory for very big arrays. The heap is generally larger.


    An array in C is represented as a pointer that references the location of the array data (it points to the first item in the array). In the case of stack-based arrays, the array pointer and data are in the same location. In the case of heap-allocated arrays, the array pointer is on the stack and points to the location on the heap where the array data begins.

    For point (2), you cannot return the value of the array. What is returned instead is the location of the array in memory or on the stack. Thus, allocating it on the heap ensures that the data is preserved when returning the array from a function.

    A std::vector on the other hand works functionally like an array. With this, the array data is allocated on the heap, but the object that manages the array is on the stack. Thus, the lifetime of the array is controlled by the lifetime of the vector object.

    The std::vector has the behaviour you describe:

  • passing a vector by value to a function causes the data to be copied when passing it to the function;

  • the vector data only lives for the lifetime of the function.

  • Passing the vector from a function can cause the array data to be copied. However, this can be optimised using things like return value optimisation and R-value references, which avoid the copy.


    如果此代码运行时不会崩溃,则可以将所有阵列分配到堆栈中。

    #include <string.h>
    int main() {
        volatile char buf[1024 * 1024 * 64];
        memset(buf, 0, sizeof(buf));
    }
    
    链接地址: http://www.djcxy.com/p/79842.html

    上一篇: 使用堆栈来避免动态分配是不好的方式吗?

    下一篇: 为什么你会想要在堆上有一个数组?