static memory and dynamic memory

This is a discussion on static memory and dynamic memory within the C++ Programming forums, part of the General Programming Boards category; ok...i always wanted to know this deeply.. Code: int number = 5; //stores in static memory int* pnum = new ...

  1. #1
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196

    static memory and dynamic memory

    ok...i always wanted to know this deeply..

    Code:
    int number = 5; //stores in static memory
    
    int* pnum = new int;
    *pnum = 5; //stores off the heap
    whats the difference between static and off the heap memory...?
    nextus, the samurai warrior

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    static memory is allocated on the stack while dynamic is on the heap. how this works exactly depends on the OS but usually the stack starts right before Kernel space and grows downwards. the heap will start at a lower address and grow upward (towards the stack) when a function or subroutine is called it will allocate its neccessary stack space (referenced by a frame pointer) and all local 'static' vars are stored there. because the program does not allways know how much space to allocate for dynamic variables, it does not use stack space for this, instead, each dynamically allocated variable will be stored in the heap with a reference (the pointer that 'new' returns) to the memory address. when a subroutine finishes, its stack space is no longer needed so the frame pointer is moved back and the stack space can be reused. however, memory on the heap is not managed this way which is why you must explicitly need to delete dynamically allocated variables.

    ive left alot out but i wasnt sure how much info you wanted.. hope that helps.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers and dynamic memory
    By tkansara in forum C Programming
    Replies: 1
    Last Post: 06-16-2009, 09:22 AM
  2. Static Memory allocation
    By p3rry in forum C Programming
    Replies: 25
    Last Post: 12-23-2008, 07:30 AM
  3. memory leaks
    By TehOne in forum C Programming
    Replies: 4
    Last Post: 10-10-2008, 09:33 PM
  4. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM

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