creating object with [new] ?

This is a discussion on creating object with [new] ? within the C++ Programming forums, part of the General Programming Boards category; Dear all, I have a class named as "Corr". I will create one object like this Corr* cc = new ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    23

    Question creating object with [new] ?

    Dear all,

    I have a class named as "Corr". I will create one object like this

    Corr* cc = new Corr;

    After I used "cc" object, do I have to delete it? and Why?

    If you could explain , I will be glad.
    thanks

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    cc is allocated on a special chunc of memory called "heap". In opposite to the memory area called "stack", you are personal responsible for management of the heap. so yes, you have to delete it, or nobody does until the program exits.

    To put cc on the stack you would write just Corr cc; Corr in this case is deleted automatically as it goes "out of scope" (while passing the next "}" )

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,255
    Not really pheres. heap and stack are ancient terms that were used with some architectures (eg old IBM compatible PC's from 1980s to early 1990s with extended memory). They have no meaning on most modern machines or operating systems.

    The basic answer to the question is that operator new is a means by which a programmer can explicitly create an object, and the lifetime of that object must be explicitly controlled by the programmer. Operator delete is the defined mechanism for ending the object lifetime (i.e. destroying the object).

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Although most modern systems will free your allocated memory when your program terminates, you shouldn't rely on it as some (such as OS's with unprotected memory (16bit DOS for example)).

  5. #5
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Wherever there's new there should be delete! It's simplified if you use a class to do your memory allocation (either a standard container or one of your own), so the destructor can do the memory management - it's done automatically*, you don't have to do it all the time ..

    * provided your destructor does it right, of course

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    C++ also has smart pointers. These may seem advanced, but in reality they are no more difficult to learn than new/delete. Ideally, you should be using smart pointers and RAII so that you don't have to worry about calling delete. This makes code cleanup much easier in the case of exceptions or early returns from functions.

  7. #7
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186
    This as some people said, provides an excellent way to manipulate through functions
    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
    Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,255
    Quote Originally Posted by Daved View Post
    C++ also has smart pointers.
    C++ has the auto_ptr<> class, which contains an object and invokes operator delete on it when it is no longer required. C++ also has various standard containers, which manage dynamically allocated memory for a set of objects (i.e. they allocate and deallocate when required).

    Various libraries (eg boost) also provide a family of smart pointer types.
    Quote Originally Posted by Daved View Post
    These may seem advanced, but in reality they are no more difficult to learn than new/delete.
    The most difficult thing is that the notion of "smart pointer" means multiple things: there are several methods for managing the lifetime of objects. It is therefore necessary to pick the right type of smart pointer (or smart pointers) for your application. This is one reason that libraries like boost support more than one type of smart pointer.

    It is actually quite simple to write a smart pointer type, and it is not that difficult to use one. A smart pointer is typically a class that contains a pointer, and manages the lifetime of the object pointed at by that pointer. This requires appropriate behaviours from constructors, destructors, assignment operators, operator functions (eg for operator->() and operator *()), and other memer functions.

    Quote Originally Posted by Daved View Post
    Ideally, you should be using smart pointers and RAII so that you don't have to worry about calling delete. This makes code cleanup much easier in the case of exceptions or early returns from functions.
    I agree with you that RAII is a good technique to use. But, for a lot of applications, there is often no use for smart pointers. Smart pointers, in practice, are often just a class that applies the RAII idiom to managing lifetime of a dynamically allocated object.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    Not really pheres. heap and stack are ancient terms that were used with some architectures (eg old IBM compatible PC's from 1980s to early 1990s with extended memory). They have no meaning on most modern machines or operating systems.
    On the other hand, from what I understand similiar terms are used in the context of C++, though they may or may not have anything to do with the underlying architecture: GotW #9: Memory Management - Part I.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,255
    Such terms have no relevance in the context of C++. They are, however, used quite often by people who have a long background programming on a Microsoft operating system, and have become a sort of defacto description used by a fair few authors of the difference between dynamically allocated objects and those that are local to a function in C/C++. The terms heap and stack were used quite often in the MS-DOS days (because early PCs had distinct areas of physical memory that came to be called heap and stack) and are still used in Mocrosoft documentation. Herb Sutter (the author of the GoTW series) is a software architect at Microsoft.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> Various libraries (eg boost) also provide a family of smart pointer types.
    The boost smart pointers (or at least some of them) have been added to TR1, although my point was more about how C++ makes using smart pointers easier, not that they are specifically part of the language itself.

    As for choosing which smart pointer to use, I imagine that choice shouldn't be any harder than figuring out where to call delete yourself.

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The term "stack", at least, is universally recognized in computer science to mean, when it's not about the ADT, a place to hold the activation records of functions in a language that allows recursion. The computer might offer hardware stack support (dedicated registers to hold stack pointers and stack manipulation instructions; in the case of x86, esp, ebp, push and pop) or it might not, but a stack is the only relevant way of implementing recursive calls currently known. (Or at least the various courses in my CompSci bachelor do not mention the existence of any other.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating A Thread W/ PThreads within an Object
    By HalNineThousand in forum Linux Programming
    Replies: 12
    Last Post: 03-28-2008, 02:57 PM
  2. Object destroy itself?
    By cminusminus in forum C++ Programming
    Replies: 28
    Last Post: 03-27-2008, 01:08 AM
  3. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 08:38 PM
  4. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM
  5. DirectX 8: Creating the DirectX Object
    By Toraton in forum Game Programming
    Replies: 3
    Last Post: 11-28-2002, 02:45 AM

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