When to use the "new", and when not, in C ++?

Possible duplicate:
When should a new keyword be used in C ++?

When should I use the "new" operator in C ++? I come from C # / Java background, and the instance objects confuse me.

If I created a simple class called "Point", when I create a point, I should:

Point p1 = Point(0,0); 

or

 Point* p1 = new Point(0, 0); 

Can someone clarify for me when to use the new operator, and when not?

Duplicate:

When should a new keyword be used in C ++?

Connected:

About constructors / destructors and new / delete operations in C ++ for custom objects

Proper use of stack and heap in C ++?

+82
c ++ new-operator
Mar 24 '09 at 22:53
source share
4 answers

You must use new if you want the object to remain in place until you delete it. If you are not using new , then the object will be destroyed when it goes beyond the scope. Here are some examples:

 void foo() { Point p = Point(0,0); } // p is now destroyed. for (...) { Point p = Point(0,0); } // p is destroyed after each loop 

Some people will say that using new determines whether your object is on the heap or the stack, but this is only true for variables declared inside functions.

In the example below, the location of "p" will be where its containing Foo object is highlighted. I prefer to call this distribution "in place."

 class Foo { Point p; }; // p will be automatically destroyed when foo is. 

Allocating (and freeing) objects using new much more expensive than if they are allocated in place, so its use should be limited where necessary.

The second example, when allocating through new is for arrays. You cannot * resize the array in place or stack at run time, so when you need an array of undefined size, it must be assigned through a new one.

eg.

 void foo(int size) { Point* pointArray = new Point[size]; ... delete [] pointArray; } 

(* pre-emptive nitpicking - yes, there are extensions that allow you to distribute stacks by size).

+104
Mar 24 '09 at 22:56
source share

Take a look at this question and this question for good answers to an instance of a C ++ object.

This basic idea is that objects created by the heap instance (using the new one) must be cleaned manually, those created on the stack (without new ones) are automatically cleared when they go beyond the bounds.

 void SomeFunc() { Point p1 = Point(0,0); } // p1 is automatically freed void SomeFunc2() { Point *p1 = new Point(0,0); delete p1; // p1 is leaked unless it gets deleted } 
+9
Mar 24 '09 at 22:57
source share

You must use the new one if you want the object to be created on the heap instead of the stack. This allows access to the object from outside the current function or procedure using pointers.

You might find it useful to look for pointers and memory management in C ++, as these things are unlikely to be found in other languages.

+4
Mar 24 '09 at 22:58
source share

The new one is always used to allocate dynamic memory, which then needs to be freed.

Performing the first option, this memory will be automatically freed when the volume is lost.

 Point p1 = Point(0,0); //This is if you want to be safe and don't want to keep the memory outside this function. Point* p2 = new Point(0, 0); //This must be freed manually. with... delete p2; 
+1
Mar 24 '09 at 22:58
source share



All Articles