473,382 Members | 1,611 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,382 software developers and data experts.

Mixing new/delete and operator new/delete?

Is it allowed by the C++ standard to mix the use of 'new/delete' and
'operator new/delete'?

(a) alloc (version 1) + free (version 2)
(b) alloc (version 2) + free (version 1)
// VERSION 1 ALLOC (using operator new + placement new)
type* data = (type*)operator new(n * sizeof(type));
for (type* p = data; p != data + n; ++p)
new ((void *)p) value_type();

// VERSION 1 FREE (using calling destructor + operator delete)
for (type* p = data; p != data + n; ++p)
p->~type();
operator delete(data);

// VERSION 2 ALLOC (using new)
type* data = new type[n];

// VERSION 2 FREE (using delete)
delete [] data;
Jul 22 '05 #1
1 2005

"Jef Driesen" <je********@hotmail.com.nospam> schrieb im Newsbeitrag
news:cs**********@ikaria.belnet.be...
Is it allowed by the C++ standard to mix the use of 'new/delete' and
'operator new/delete'?

(a) alloc (version 1) + free (version 2)
(b) alloc (version 2) + free (version 1)
// VERSION 1 ALLOC (using operator new + placement new)
type* data = (type*)operator new(n * sizeof(type));
for (type* p = data; p != data + n; ++p)
new ((void *)p) value_type();

// VERSION 1 FREE (using calling destructor + operator delete)
for (type* p = data; p != data + n; ++p)
p->~type();
operator delete(data);

// VERSION 2 ALLOC (using new)
type* data = new type[n];

// VERSION 2 FREE (using delete)
delete [] data;


I can imagine it might now cause much problems, as new and delete
internally call malloc, however I would not rely on that + there is no
rule this has to be so. Thus, this can crash badly.
-Gernot
Jul 22 '05 #2

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

Similar topics

2
by: Dave | last post by:
Hello all, In the code below, I see the following output: base::operator new(size_t, int) base::base() base::~base() base::operator delete(void *) In the case of an exception being thrown...
1
by: Marc Cromme | last post by:
I would like to ask a question about (good ?) style and possibilities in mixing C FILE* and C++ file streams. The background is that I want to use the C libpng library from within C++, but I...
1
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. In fact there...
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>
3
by: silver360 | last post by:
Hello, I'm trying to create a basic Heap manager and i have some question about new/delete overloading. The following code give me this output : >> $./heap >> registered : 0x804d098 >>...
5
by: junw2000 | last post by:
I use the code below to study delete and destructor. #include <iostream> using namespace std; struct A { virtual ~A() { cout << "~A()" << endl; }; //LINE1 void operator delete(void* p) {...
6
by: Lighter | last post by:
Big Problem! How to overload operator delete? According to C++ standard, "A deallocation function can have more than one parameter."(see 3.7.3.2); however, I don't know how to use an overloaded...
2
by: Jared Kail | last post by:
I've never mixed managed and unmanaged C++ before, but I've done a lot of "research" out on the web. Using some examples out there, I wrote this: #using <mscorlib.dll> using namespace System; ...
10
by: jeffjohnson_alpha | last post by:
We all know that a new-expression, foo* a = new foo() ; allocates memory for a single foo then calls foo::foo(). And we know that void* p = ::operator new(sizeof(foo)) ; allocates a...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.