Quick question about new and delete

This is a discussion on Quick question about new and delete within the C++ Programming forums, part of the General Programming Boards category; Considering c++ doesnt implement automatic garbage collection I understand that its a good idea to keep tight control of memory ...

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

    Quick question about new and delete

    Considering c++ doesnt implement automatic garbage collection I understand that its a good idea to keep tight control of memory usage. I know that you can initialise a variable using the new and then clear it from the memory using delete. My question is that if you have a program that will be running indefinately, usually for days/weeks perhaps months at a time, is it advisable to use this method for nearly every variable. Or are objects and variables unloaded every time they go out of scope, and therefore should this only be used for objects with a wider scope, which you dont want accumulating in the memory as time goes by.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    For most objects, you want to keep them local to the class, function or block they are declared in so they are destroyed when they go out of scope. The only times you really want to use new/delete is if the object is very large (e.g. it includes a very large static array of some sort) which might eat up limited stack space, if you want the object to persist past the current scope. Use of polymorphism is another common reason for using new/delete, although that is really because you usually need the objects to persist past the current scope to take advantage of polymorphism.

    Of course, in many cases in C++, you want to use classes to help you manage that memory so that you don't need to do it explicitly yourself. For example, instead of creating a large array with new, you use a vector (which uses new itself). That vector should normally be allocated locally like any other variable. So while the data is allocated on the heap by the vector class, you are still not directly using new/delete yourself.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    In my opinion the only time you want to use variable memory is if you don't know the size of an array at compile time or if you plan to have absolutely huge arrays. Huge like over 1 megabyte in size.

    The reason is simple, static variables are automatically deleted and their memory freed when the scope they're decalred in is exited so you never need to fret about memory.

    The main reason for the existance of dynamically allocated variables is if you don't know how much memory you're going to need and don't want to declare a large upper bound. If you know the expected size of every array, then just declare them static.

    Even if you want arrays in global scope or whatever, you can still declare them static. C++ (unlike C) allows you to initalize static variables in global scope (I think).

Popular pages Recent additions subscribe to a feed

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