Thread: Dynamic memory

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    486

    Dynamic memory

    What exactly does it meant to be a dynamic array? I know it means malloc, but what is different about a dynaic array versus a (I presume?) static one?

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Dynamic memory is memory that was allocated in addition to the memory your program was initially given by the OS. So, essentially, it just means malloc.

    It doesn't mean that can just resize the array willy-nilly (although're there's realloc), but what's nice is that since it's just a pointer you're using, you can free() that pointer and than use it for something else, or for an array of a different size. That's what make it dynamic.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You could go to (say) dictionary.com to find the difference in meaning of the two words.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Dynamic memory is the storage that is allocated at runtime.

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    Thanks Sean

    tabstop - i know the difference in the definition, I am looking for a definition in a specific context. No need to be a smartass.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Dynamic refers to it having varying sizes throughout its lifetime - presumably governed by user input or some other external data. The program is expected to accommodate such an array - but usually there is at least some additional knowledge required such as the maximum expected size.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I might as well take a stab at it too:

    Dynamic memory is allocated on the heap by a program at runtime. It has several advantages over e.g. automatic variables declared in stack memory.
    • The amount of memory that you dynamically allocate can be determined at runtime. As mentioned by nonoob, this means that you can e.g. allocate just enough memory to store external data. With an array, you'd have to put an upper limit on the size and usually waste space, as well as running the risk of hitting the upper limit.
    • The heap has a lot more space than the stack does. If you need 200MB of memory for an expensive calculation, you can probably get it with malloc(); you certainly couldn't fit it onto the stack under most circumstances.


    Of course, dynamic memory has its drawbacks. You have to free it manually (though sometimes this is an advantage, if different sections of code need to use the same memory); stack memory is automatically freed for you. Plus your program might fail to get heap memory.

    All in all, though, dynamically allocated memory is a lot more flexible and useful.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Although "stack" and "heap" are implementation concepts and not C concepts.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Linking & Memory usage
    By @nthony in forum C Programming
    Replies: 2
    Last Post: 06-02-2007, 09:57 PM
  2. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 03:59 AM
  3. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM
  4. dynamic memory + linked lists
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 02-10-2002, 04:50 PM
  5. Dynamic Memory Allocation for fstream (binary)
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 12-12-2001, 10:52 AM