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 ...

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 "}" )

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).

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 ..

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.

"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 <_<

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.

Originally Posted by Daved

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.

Originally Posted by Daved

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.

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.

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.

>> 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.

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.)