Dynamic memory managment

This is a discussion on Dynamic memory managment within the C++ Programming forums, part of the General Programming Boards category; What is the best way to use new/delete in programs?? When declaring a class, should everything be declared as pointers ...

  1. #1
    Unregistered
    Guest

    Dynamic memory managment

    What is the best way to use new/delete in programs?? When declaring a class, should everything be declared as pointers and made dynamic or is there advantages to not doing this? At the moment I know how to declare and use dynamic variables, but I dont know if there is a reason I shouldnt do so.

    TIA

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Why would you force yourself to deal with all the extra syntax if you don't have to? Also, the amount of memory you can use is limited; so you can't just write a large program with all dynamically allocated memory.

  3. #3
    Unregistered
    Guest
    No reason not to. Free's up space on program stack. Passing less information back and forth between functions so may be a little quicker (depending how big and how many objects there are), etc. If you feel comfortable doing it, do it.

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    golfinguy4: Also, the amount of memory you can use is limited; so you can't just write a large program with all dynamically allocated memory.


    why not?

    u think staticmemory isn't limited?

  5. #5
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Personally, I don't think it's worth it. The performance increase would not be that great. My theory is if it isn't broken, don't fix it.

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    114
    I say: If you know how to handle pointers and dynamically allocated memory, use it. If you don't know how to handle it, you can screw things up bad. But if you don't know how to use it, you should learn nontheless.

    Now to your question of when to use it:
    You classes should have pointers to other classes. They should not hold the object directly. In a time critical loop, you should try to minimize the use of new/delete. If a section is time critical, allocate all you can before the section, and delete all after that section. Allocation in a timecritical loop is a bad thing. What more advantages is there to have pointers? If you call a function, with the class as param, or a pointer to the class as param, which will be faster? the pointer of cause. The pointer is 4 - 8 byte long, the class could be 100 byte long or more. pushing 4 bytes to the stack compared to 100 bytes is a big difference. Also, you will have 100 bytes on 2 places. Where it was declared, and on the call stack... so you save memory by the use of pointers. So event if the class isn't dynamic in the first place, always make functions that take this class take pointers...

    /dave

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    dynamomemo is closer than static

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    156
    ok
    Compiler: MingW(IDE: Bloodshed Dev-C++ 4.01)
    Web Site: Zoo Crew
    Forums: Zoo Boards
    E-mail: goldenbunny84@hotmail.com

    "Do you wanna go to jail or do you wanna go home?!?!" - Alonzo(Training Day)

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