Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Difference in using new and not using new to create an object 1

Status
Not open for further replies.

titanandrews

Programmer
Feb 27, 2003
130
US
Hi,
I am a little confused about what the difference is when you use new to instantiate an object versus not using new. In the Java world you can only create objects using new, but it seems that C++ is different. Using the sample code below, I get the same output both ways. My book does not do a good job explaining the difference. Can someone please enlighten me?
Thanks!

Barry

ANewObj.cpp
Code:
#include <iostream>
using namespace std;

class ANewObj
{
public:
    ANewObj::ANewObj()
    {
        cout << &quot;ctor called\n\n&quot;;
    }

    ANewObj::~ANewObj()
    {
        cout << &quot;dtor called\n\n&quot;;
    }

    void someMethod()
    {
        cout << &quot;someMethod called\n\n&quot;;
    }
};

int main(int argc,char *argv[])
{
    cout << &quot;Using new &quot; << endl;
    ANewObj *myObj = new ANewObj();
    myObj->someMethod();
    delete myObj;

    cout << &quot;Not using new &quot; << endl;
    ANewObj anotherObj;
    anotherObj.someMethod();

    return(0);
}
 
When you use ANewObj anotherObj, you have created an object on the stack. That object has local scope, i.e., when the calling function exits, it destroys the object and then it goes out of scope.

When you use ANewObj *myObj = new ANewObj(), you are creating an object dynamically. This means that a certain amount of memory is reserved from the heap, and the ANewObj is constructed there.

Each method has its uses. Use dynamic creation when the number or type of objects isn't known until run time. For example, an array class must be able to add an indefinite number of elements...

Imagine if you were creating an class that implements a variable-size array. You might start here:

[tt]class ANewObjArray {
public:
ANewObjArray();
~ANewObjArray();
ANewObjArray& operator[](int index);
private:[/tt]

That's a start. But what do you put next? If you were using normal stack allocation (with using operator new), you would say

[tt]private:
ANewObj data[256];
};[/tt]

The problem there is that you limit yourself to 256 elements. So wouldn't you just change the number to something bigger, maybe 100000? Makes sense doesn't it....

But WAIT! What if an instance of your array only has five elements in it? That's a whole lot of memory.

Thus, you need a solution that allows you to create (or destroy) elements as they are needed (or are no longer needed.)

Remember, if you create an object with operator new, it must also be destructed with operator delete. Otherwise, the destructor is never called, and you get memory leaks.

The use for normal stack creation (not using operator new) is for variables local to a specific scope. When you use an object as a local variable, or as a member variable of another class, this is probably what you'll use.

I recommend further reading from Stroustrup's The C++ Programming Langugage -- available from Addison-Wesley.

I REALLY hope that helps.
Will
 
Heck, you should write a book. Great explanation! Thanks! I will also check out the book you mentioned.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top