new or not new

J

Johan den Boer

Hi,

Suppose we have a class A.Why or when do you use

main()
{
A *ptr = new A;

// instead of

A ptr;
}

Johan
 
J

Jeff Schwab

Johan said:
Hi,

Suppose we have a class A.Why or when do you use

main()
{
A *ptr = new A;

// instead of

A ptr;
}

Johan

Use "A ptr;" whenever possible. Allocating A dynamically (with "new")
can be very slow, and usually means you must be careful to free the
memory (with "delete") later in the program.

The need to allocate an individual object dynamically is rare. If you
are allocating an arbitrarily sized collection of A, it is usually
better to rely on a container from the standard library to allocate and
hold your collection, than it is to call "new" yourself.

Btw, if A is not a type of pointer, you may want to rethink the name of
the variable "ptr." :)

-Jeff
 
R

Ron Natalie

Johan den Boer said:
Hi,

Suppose we have a class A.Why or when do you use

main()
{
A *ptr = new A;

// instead of

A ptr;
}
Well I wouldn't call the variable "ptr" in the second case. However, the answer is that you
prefer the second case unless it won't work for you. The main reason why you need new is
when you need to manage the lifetime of the object independently from the function/block
it's declared in.
\
 
C

Cy Edmunds

Johan den Boer said:
Hi,

Suppose we have a class A.Why or when do you use

main()
{
A *ptr = new A;

// instead of

A ptr;
}

Johan

I don't actually use operator new very much. When I do it is generally to
create a polymorphic object. In that case I immediately wrap the result in a
smart pointer to make it exception safe and automate my memory management.

It seems to me from reading posts on this NG that newbies use operator new
all the time for no obvious reason.
 
R

Ron Natalie

Cy Edmunds said:
I don't actually use operator new very much. When I do it is generally to
create a polymorphic object. In that case I immediately wrap the result in a
smart pointer to make it exception safe and automate my memory management.

Of course, you don't even need to do that to create a polymorphic object. You
can always take the address of a non-dynamic object. Of course, it's harder when
you want to create one out of possibly several different ones...
It seems to me from reading posts on this NG that newbies use operator new
all the time for no obvious reason.

Some of this stems from conversion from Java which uses new to create all objects.
 
N

Nick Hounsome

Johan den Boer said:
Hi,

Suppose we have a class A.Why or when do you use

main()
{
A *ptr = new A;

// instead of

A ptr;
}

Johan

While agreeing with other replies in general and totally when it comes to
the explicit example above
I would point out one good use is the pimpl pattern:

class XImpl;
class X
{
XImpl* pimpl; // only data member
public:
// all the default methods must be defined
X();
~X();
X& operator=(const X&);
....
// only non-inline methods here
...
};

By only storing a pointer to an opaque type the implementation of X can be
changed without requiring clients to be recompiled.
 
E

E. Robert Tisdale

Johan said:
Suppose we have a class A.

Why or when do you use

int main(int argc, char* argv[]) {

A *ptr = new A;

// instead of

A ptr;

return 0;
}

Function main(int, char**) returns an int.

Don't write:

A *ptr = new A;

if you don't need to.
But it probably isn't a good idea to allocate storage
for large objects from automatic storage in a tight loop:

for (size_t j = 0; j < 1000000; ++j) {
A a[1000000];
// . . .
foo(a);
// . . .
}

because automatic storage is typically implemented
on the program call stack and will force [a series of] page faults.
 
D

Daniel =?iso-8859-1?Q?Lidstr=F6m?=

Hi,

Suppose we have a class A.Why or when do you use

main()
{
A *ptr = new A;

// instead of

A ptr;
}

Johan

The best reason to use new IMO is when you need to create large objects.
Especially static (C++ keyword static) objects. If you try to create a very
large object statically you might fill up the entire stack memory, which
will usually result in a crash.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,159
Messages
2,570,883
Members
47,415
Latest member
SharonCran

Latest Threads

Top