Search code examples
c++heap-memorystack-memory

Does Stack being limited in size mean i can only get limited pointers to objects in heap?


if stack size is 1MB, does that mean i can only get less than 1000000/8 pointers to allocate ints in heap? (considering 1MB free stack) yeah i know you might not want to make that many individual ints on heap but still it limits number of objects you can dynamically allocate.

doesnt that just make making ints on stack better choice? (ints are 4 bytes here)

edit: i get comments of what usage of pointers are, that not what im asking, what i meant is that with limited stack size, isnt there limited "number", not "size" of stuff you can access/make on heap?


Solution

  • The Stack and the Heap have nothing to do with each other. The Stack having a limited size simply means that you have a limited amount of space to store things like local variables and function call parameters. That has nothing to do with how much space you can allocate dynamically in the Heap.

    For example:

    int *ptr1 = (int*) malloc(sizeof(int) * N);
    int *ptr2 = (int*) malloc(sizeof(int) * M);
    

    ptr1 and ptr2 are local variable created on the Stack, but they point to dynamic memory allocated on the Heap. The size of the Stack does not influence how much space is on the Heap:

       Stack                     Heap
    +-----------+           +-------------+
    |           |           |             |
    | +------+  |           |  +--------+ |
    | | ptr1 | -|-----------|->| 12345  | |
    | +------+  |           |  +--------+ |
    | +------+  |           |  | 67890  | |
    | | ptr2 | -|-----+     |  +--------+ |
    | +------+  |     |     |  | ...    | |
    |           |     |     |  +--------+ |
    +-----------+     |     |             |
                      |     |  +--------+ |
                      +-----|->| 12345  | |
                            |  +--------+ |
                            |  | 67890  | |
                            |  +--------+ |
                            |  | ...    | |
                            |  +--------+ |
                            |             |
                            +-------------+
    

    It seems you are assuming that having a small Stack means you can only have X number of pointers total into the Heap. That is not true. For example, a pointer can be moved from the Stack into an object member that exists in the Heap:

       Stack                     Heap
    +-----------+           +-------------+
    |           |           |             |
    | +------+  |           |  +--------+ |
    | | ptr1 | -|-----------|->| member | -----+
    | +------+  |           |  +--------+ |    |
    | +------+  |           |             |    |
    | | ptr2 | -|-----+     |  +--------+ |    |
    | +------+  |     |     |  | 12345  | <----+
    |           |     |     |  +--------+ |
    +-----------+     |     |  | 67890  | |
                      |     |  +--------+ |
                      |     |  | ...    | |
                      |     |  +--------+ |
                      |     |             |
                      |     |  +--------+ |
                      +-----|->| 12345  | |
                            |  +--------+ |
                            |  | 67890  | |
                            |  +--------+ |
                            |  | ...    | |
                            |  +--------+ |
                            |             |
                            +-------------+