operator new() and new[]

L

Leander de Graaf

Hi Alex, to my knowledge the difference between variable names with and
without brackets has to do with declaring variables which respectively
accept a single value and multiple values usings arrays .
This also is the case with the new and delete statements.

Correct me if i'm wrong
 
V

Victor Bazarov

Alex said:
Is the any difference between

------ 1 ------
Foo* p = operator new (n * sizeof (Foo));
// Stuff
delete p;
---------------

and

------ 2 ------
Foo* p = new [n];
// Stuff
delete[] p;

Yes, abslutely. The former shouldn't compile.

V
 
V

Victor Bazarov

Leander said:
Hi Alex, to my knowledge the difference between variable names with
and without brackets has to do with declaring variables which
respectively accept a single value and multiple values usings arrays .
This also is the case with the new and delete statements.

Correct me if i'm wrong

You're wrong. You misread the OP's code. Please read the first part
again.

V
 
A

Alex Vinokur

Victor said:
Alex said:
Is the any difference between

------ 1 ------
Foo* p = operator new (n * sizeof (Foo));
// Stuff
delete p;
---------------

and

------ 2 ------
Foo* p = new [n];
// Stuff
delete[] p;

Yes, abslutely. The former shouldn't compile.
[snip]

Sorry, my mistake.
Thanks.

Here is what I meant.

------ 1 ------
Foo* p = static_cast<Foo*>(operator new (n * sizeof (Foo)));
// Stuff
delete p;
---------------

------ 2 ------
Foo* p = new Foo [n];
// Stuff
delete[] p;
---------------

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn
 
V

Victor Bazarov

Alex said:
Here is what I meant.

------ 1 ------
Foo* p = static_cast<Foo*>(operator new (n * sizeof (Foo)));
// Stuff
delete p;
---------------

------ 2 ------
Foo* p = new Foo [n];
// Stuff
delete[] p;
---------------


Define the default c-tor in Foo, make it output something; do the
same with the d-tor; compare the results of both fragments with
properly defined c-tor and d-tor. Make conclusions.

V
 
S

SuperKoko

The former will allocate with the global operator new allocator, enough
room for n Foo objects, but will not call any constructor.
So, p will point to raw memory, and accessing any member of Foo, or
casting Foo to one of its base class, or doing similar things, will
have undefined behaviour (see [basic.life]-5)
Moreover, the delete expression will call a destructor on a non-alive
Foo object.
Thus, the behaviour is undefined.
If, you replace the delete expression by an explicit call to the global
operator delete, it correct this problem:

Foo* p = static_cast<Foo*>(operator new (n * sizeof (Foo))); // calls
the global operator new
// You can't access Foo members or subtypes here
::eek:perator delete (p); // free the memory

The later code allocates memory with operator new[], which may be the
global operator new[], but may also be an overloaded operator new[] by
the Foo class.
It also constructs all objects, using the default-constructor of Foo
(or nothing if Foo is a POD type).

delete[] p, will destroy properly all Foo objects, and release memory
with the correct version of operator delete[], which may be overloaded
by the Foo class.
 
A

Alex Vinokur

SuperKoko said:
The former will allocate with the global operator new allocator, enough
room for n Foo objects, but will not call any constructor.
So, p will point to raw memory, and accessing any member of Foo, or
casting Foo to one of its base class, or doing similar things, will
have undefined behaviour (see [basic.life]-5)
Moreover, the delete expression will call a destructor on a non-alive
Foo object.
Thus, the behaviour is undefined.
If, you replace the delete expression by an explicit call to the global
operator delete, it correct this problem:

Foo* p = static_cast<Foo*>(operator new (n * sizeof (Foo))); // calls
the global operator new
// You can't access Foo members or subtypes here
::eek:perator delete (p); // free the memory
[snip]

Does
Foo* p = static_cast<Foo*>(operator new (n * sizeof (Foo)));
differ from
Foo* p = static_cast<Foo*>(malloc (n * sizeof (Foo)));
?



Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn
 
V

Victor Bazarov

Alex said:
[..]
Does
Foo* p = static_cast<Foo*>(operator new (n * sizeof (Foo)));
differ from
Foo* p = static_cast<Foo*>(malloc (n * sizeof (Foo)));
?

Yes. You need to use 'operator delete' to dispose of the pointer
obtained in the former case and 'free' for the latter.

V
 
V

Victor Bazarov

Alex said:
[..]
Does
Foo* p = static_cast<Foo*>(operator new (n * sizeof (Foo)));
differ from
Foo* p = static_cast<Foo*>(malloc (n * sizeof (Foo)));
?

I forgot to mention that usefulness of such constructs was doubtful.
What is it you're trying to accomplish here?

V
 
A

Alex Vinokur

Victor said:
Alex said:
[..]
Does
Foo* p = static_cast<Foo*>(operator new (n * sizeof (Foo)));
differ from
Foo* p = static_cast<Foo*>(malloc (n * sizeof (Foo)));
?

I forgot to mention that usefulness of such constructs was doubtful.
What is it you're trying to accomplish here?
[snip]

Of course, I am not going to use malloc() instead of operator new() in
such situations.
I wanted to know if there is any difference between them here.

Thanks.

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn
 
V

Victor Bazarov

Alex said:
Victor said:
Alex said:
[..]
Does
Foo* p = static_cast<Foo*>(operator new (n * sizeof (Foo)));
differ from
Foo* p = static_cast<Foo*>(malloc (n * sizeof (Foo)));
?

I forgot to mention that usefulness of such constructs was doubtful.
What is it you're trying to accomplish here?
[snip]

Of course, I am not going to use malloc() instead of operator new() in
such situations.
I wanted to know if there is any difference between them here.

The difference between them is similar to the difference between shooting
oneself in the heart and shooting oneself in the head.

V
 
T

Tomás

The difference between them is similar to the difference between shooting
oneself in the heart and shooting oneself in the head.

I wonder if a gunshot to the heart is more lethal (percentage wise) than
a gunshot to the head? You hear of plenty of people who've been shot in
the head and are still alive... but who's ever been shot right in the
heart and lived?

Then again you've to consider that there's a far greater liklihood that a
bullet will miss the heart, rather than that a bullet will miss the
brain.

-Tomás
 
V

Victor Bazarov

Tomás said:
I wonder if a gunshot to the heart is more lethal (percentage wise)
than a gunshot to the head? You hear of plenty of people who've been
shot in the head and are still alive... but who's ever been shot
right in the heart and lived?

Then again you've to consider that there's a far greater liklihood
that a bullet will miss the heart, rather than that a bullet will
miss the brain.

It depends entirely on the size (and nature) of the projectile, on
the size of the charge behind the projectile, on the ability of the
shooter to locate the heart (or the head, for that matter). The
existence of the heart and/or the head is, of course, assumed.

V
 

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

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top