memory allocation from stack and heap ?

This is a discussion on memory allocation from stack and heap ? within the C++ Programming forums, part of the General Programming Boards category; Hi, Can anybody tell me what is the difference memory allocation from stack and heap ? And how compiler decide ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    104

    memory allocation from stack and heap ?

    Hi,

    Can anybody tell me what is the difference memory allocation from stack and heap ?

    And how compiler decide from where to allocate the memory to objects and variable ??

    Thanks

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,660
    The easy explanation:
    The stack is the default place of allocation. Any variables you define reside there. They are cleaned up automatically after a function ends.
    The stack is limited (around 1 MB).

    The heap is essentially the big memory in the system. It's limited only to the amount of memory you have installed[1]. Objects on the heap are not automatically destroyed, so you need to do so manually (thankfully, smart pointers and RAII can take care of that for us in C++). This is good because it outlives the life of a function.

    [1] The heap is actually limited to 2/3 GB depending on OS. The OS will handle where the memory is stored. If you don't have enough physical memory, it will page it to the pagefile.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    int i; //stack

    int *i = malloc(sizeof(int)); // i is on the stack, *i is on the heap.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,660
    Or rather
    Code:
    int* i = new int; // i on stack, *i on heap
    This is C++, after all.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Ah yes. What Elysia said.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. heap vs stack memory question
    By donglee in forum C++ Programming
    Replies: 4
    Last Post: 01-23-2009, 03:34 PM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. stack vs heap memory allocation
    By gongchengshi in forum C Programming
    Replies: 9
    Last Post: 11-18-2007, 11:17 AM
  4. stl containers allocation in heap or stack?
    By sawer in forum C++ Programming
    Replies: 9
    Last Post: 08-06-2006, 03:08 PM
  5. Relocatable code and Symbol Table
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 06-10-2002, 11:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21