473,322 Members | 1,268 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

operator new() and new[]

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;
---------------
?
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Apr 2 '06 #1
13 2184
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

Apr 2 '06 #2
Alex Vinokur wrote:
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
--
Please remove capital As from my address when replying by mail
Apr 2 '06 #3
Leander de Graaf wrote:
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
--
Please remove capital As from my address when replying by mail
Apr 2 '06 #4
Victor Bazarov wrote:
Alex Vinokur wrote:
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

Apr 2 '06 #5
Alex Vinokur wrote:
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
--
Please remove capital As from my address when replying by mail
Apr 2 '06 #6
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
::operator 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.

Apr 2 '06 #7

SuperKoko wrote:
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
::operator 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

Apr 6 '06 #8
Alex Vinokur wrote:
[..]
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 6 '06 #9
Alex Vinokur wrote:
[..]
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 6 '06 #10
Victor Bazarov wrote:
Alex Vinokur wrote:
[..]
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

Apr 7 '06 #11
Alex Vinokur wrote:
Victor Bazarov wrote:
Alex Vinokur wrote:
[..]
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 7 '06 #12
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
Apr 7 '06 #13
Tomás wrote:
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.


It depends entirely on the size (and nature) of the projectile[s], on
the size of the charge behind the projectile[s], 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 7 '06 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: joesoap | last post by:
Hi can anybody please tell me what is wrong with my ostream operator??? this is the output i get using the 3 attached files. this is the output after i run assignment2 -joesoap #include...
1
by: Senthilvel Samatharman | last post by:
I am just curious about the case 3 in the follwouing program. I understand that case 1 is the right way to overload, while Case 2 is erroneous. But i do think that the implementation of operator...
20
by: Ioannis Vranos | last post by:
When we use the standard placement new operator provided in <new>, and not a definition of owr own, isn't a call to placement delete enough? Consider the code: #include <new>
11
by: Jonan | last post by:
Hello, For several reasons I want to replace the built-in memory management with some custom built. The mem management itlsef is not subject to my question - it's ok to the point that I have...
22
by: BekTek | last post by:
Hi.. I'm so sorry about that I've postes so many questions recently.. :) I'm still confused about the differences between malloc and operator new.. I know that when we work with class object and...
1
by: Tony Johansson | last post by:
This class template and main works perfectly fine. But could be better. I have this class template called Handle that has a pointer declared as T* body; As you can see I have a reference counter...
2
by: Shark | last post by:
Hi, if we need to change the behavior of operator new, it is called overriding or overloading? My other question is, if we change the behavior of operator new, do we use malloc to do that or we use...
8
by: valerij | last post by:
Yes, hi How to write "operator +" and "operator =" functions in a class with a defined constructor? The following code demonstrates that I don't really understand how to do it... I think it has...
10
by: siddhu | last post by:
Dear Experts, I want to make a class whose objects can be created only on heap. I used the following approach. class ss { ss(){} public: void* operator new(size_t sz)
3
by: C++Liliput | last post by:
Hi, I was looking at the implementation of operator new and operator new in gcc source code and found that the implementation is exactly the same. The only difference is that the size_t argument...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.