Why use Dynamic Memory

This is a discussion on Why use Dynamic Memory within the C++ Programming forums, part of the General Programming Boards category; Hi, Ive been reading round about dynamic memory. Apparently it leads to very fragmented memory, so why would you ever ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    45

    Why use Dynamic Memory

    Hi,

    Ive been reading round about dynamic memory. Apparently it leads to very fragmented memory, so why would you ever use it alot like most programs do?

    I mean normally static memory allocation can do the same job and leave less fragmented memory..

    Thanks
    Alex

  2. #2
    すまん Hikaru's Avatar
    Join Date
    Aug 2006
    Posts
    46
    Quote Originally Posted by appleGuy
    I mean normally static memory allocation can do the same job and leave less fragmented memory..
    Normally being the key word. What if you need more memory than the stack can hold? What if using static allocation would blow your executable way out of proportion? What if you don't know the size of something until after it's too late to use the stack or static allocation?

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,467
    Dynamic memory is central to any project worth its name. You will get around it pretty quickly and understand its need better than what we could possibly tell you.

    However, the "main" reason dynamic memory exists is exactly because it saves... memory

    Without it, you would soon find yourself out of stack space (the amount of memory programs are allocated by the operating system when they are fired up). And even if you had unlimited stack space, the only way to fight the problem of not knowing the size of an array, for instance, as Hikaru described, would probably be to create large arrays that could fit all requests. Now... can you imagine the wasted memory?

    Finally a word about memory fragmentation. It's no so much a problem these days as it used to be. Operating systems deal with it quiet well. There may be a slight delay to allocate... say an array of 150 bytes, if your remaining memory was so badly fragmented you didn't have a big enough block of free memory. But the operating system will know what to do... more often than not it will call for some more virtual memory.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Quote Originally Posted by appleGuy
    Hi,

    Ive been reading round about dynamic memory. Apparently it leads to very fragmented memory, so why would you ever use it alot like most programs do?

    I mean normally static memory allocation can do the same job and leave less fragmented memory..

    Thanks
    Alex
    Here's one fragment from Bruce Eckel's book Thinking in C++:

    "How many planes will an air-traffic system need to handle? How many shapes will a CAD system use? How many nodes will there be in a network?

    To solve the general programming problem, it’s essential that you be able to create and destroy objects at runtime."

    As you can see dynamic memory is like necessary evil.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Dynamic memory is only evil to those who do not know how to use it. IMO if you cannot use it you should not be a programmer.

    This very line of thinking that dynamic memory is evil or unsafe is why we have crappo virtual machines that supposedly handle memory for you. However the VM only handles it relative to your program and cleans up when it feels the need to do so. Not such a good thing in a cooperative pre-emptive multi-tasking OS.

  6. #6
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Fragmentation is really not an issue in most cases. The runtime libraries handle the underlying allocation, and they're quite good at what they do, especially as nowadays each program lives in its own virtual memory space, not a memory space shared by other programs.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by Bubba
    Dynamic memory is only evil to those who do not know how to use it. IMO if you cannot use it you should not be a programmer.
    Dynamic memory is evil, its just the lesser of the other evils, so its the one which is used when its necessary.
    This very line of thinking that dynamic memory is evil or unsafe is why we have crappo virtual machines that supposedly handle memory for you.
    Just because someone came up with a bad solution doesn't mean that there isn't a problem. It is also the reason why we have the STL and smart pointers.

  8. #8
    Registered User
    Join Date
    Aug 2006
    Posts
    45
    Hey,
    Yeah ive got it now!..
    Thanks
    Alex

  9. #9
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    It's fairly simple.

    If you use allocate with malloc, de-allocate with free.

    If you use new to allocate, de-allocate with delete.

    If you allocate an array with new - de-allocate with delete [] ptrArray.

    Again I fail to see the problem or evil here.


    Ya know here in America it's pretty risky turning left at an intersection with oncoming traffic. I guess you could
    either find and alternate route or learn to do it safely.

    Just because it can be done incorrectly does not mean it's evil.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    Presumably, the term "evil" is meant in the context described here: http://www.parashift.com/c++-faq-lit....html#faq-6.15

    Given that, dynamic memory itself is not evil. However, explicit management of the dynamic memory often is evil (by that definition). For example, using dynamic arrays is considered evil because there are usually better, safer solutions.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Dynamic memory isn't evil. People who use dynamic memory are evil.
    My best code is written with the delete key.

  12. #12
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,467
    And I would add footnote [3]

    [3] Irresponsibly, that is


    Dynamic memory is a cute and sexy. Some people just don't have any manners. That's all
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

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, 03:50 PM
  5. Dynamic Memory Allocation for fstream (binary)
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 12-12-2001, 09:52 AM

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