Why would you ever want to allocate memory on the heap rather than the stack?

Possible Duplicate:
When is it best to use a Stack instead of a Heap and vice versa?

I've read a few of the other questions regarding the heap vs stack, but they seem to focus more on what the heap/stack do rather than why you would use them.

It seems to me that stack allocation would almost always be preferred since it is quicker (just moving the stack pointer vs looking for free space in the heap), and you don't have to manually free allocated memory when you're done using it. The only reason I can see for using heap allocation is if you wanted to create an object in a function and then use it outside that functions scope, since stack allocated memory is automatically unallocated after returning from the function.

Are there other reasons for using heap allocation instead of stack allocation that I am not aware of?


There are a few reasons:

  • The main one is that with heap allocation, you have the most flexible control over the object's lifetime (from malloc / calloc to free );
  • Stack space is typically a more limited resource than heap space, at least in default configurations;
  • A failure to allocate heap space can be handled gracefully, whereas running out of stack space is often unrecoverable.
  • Without the flexible object lifetime, useful data structures such as binary trees and linked lists would be virtually impossible to write.


  • You want an allocation to live beyond a function invocation
  • You want to conserve stack space (which is typically limited to a few MBs)
  • You're working with re-locatable memory (Win16, databases, etc.), or want to recover from allocation failures.
  • Variable length anything. You can fake around this, but your code will be really nasty.
  • The big one is #1. As soon as you get into any sort of concurrency or IPC #1 is everywhere. Even most non-trivial single threaded applications are tricky to devise without some heap allocation. That'd practically be faking a functional language in C/C++.


    So I want to make a string. I can make it on the heap or on the stack. Let's try both:

    char *heap = malloc(14);
    if(heap == NULL)
      {
        // bad things happened!
      }
    strcat(heap, "Hello, world!");
    

    And for the stack:

    char stack[] = "Hello, world!";
    

    So now I have these two strings in their respective places. Later, I want to make them longer:

    char *tmp = realloc(heap, 20);
    if(tmp == NULL)
      {
        // bad things happened!
      }
    heap = tmp;
    memmove(heap + 13, heap + 7);
    memcpy(heap + 7, "cruel ", 6);
    

    And for the stack:

    // umm... What?
    

    This is only one benefit, and others have mentioned other benefits, but this is a rather nice one. With the heap, we can at least try to make our allocated space larger. With the stack, we're stuck with what we have. If we want room to grow, we have to declare it all up front, and we all know how it stinks to see this:

    char username[MAX_BUF_SIZE];
    
    链接地址: http://www.djcxy.com/p/14008.html

    上一篇: 如何在Android中发现我的应用程序的内存使用情况?

    下一篇: 为什么你会想要在堆上分配内存而不是堆栈?