473,765 Members | 2,070 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Overloading new[] and delete[]: how do they vary from new and delete?

I'm writing a memory manager and overloading the operators "::new" and
"::delete" I know this isn't always the smartest thing to do, but
regardless, I was wondering what sort of considerations I should be
making when writing the array-based versions "new[]" and "delete[]"
(in contrast to the single item "new" and "delete").

I know the fundamental issue between ensuring "new[]" is matched with
"delete[]" is making sure the destructor for each of the items in the
dynamically-allocated array is called (as opposed to just the first if
only "delete" were called). If I'm just getting a pointer to the
beginning of said array when "delete[]" is called, how do I handle
this properly?

Thanks in advance.

Dave
Jul 19 '05 #1
1 2023
On 5 Aug 2003 20:22:36 -0700, ve*****@hotmail .com (HeroOfSpielbur g) wrote:
I'm writing a memory manager
That's not a good idea except as an exercise.

To write a (useful) memory manager you need to really know
what you're doing.

Take a look in "Modern C++ Design" and in the Boost library.

...
If I'm just getting a pointer to the
beginning of said array when "delete[]" is called, how do I handle
this properly?


You don't. Your responsibility is not to call destructors but to
deallocate memory. Whatever the latter means in your code.

Jul 19 '05 #2

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

Similar topics

3
9411
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. I am presenting below a summary of what I have gathered. I would appreciate if someone could point out to something that is specific to Borland C++ and is not supported by the ANSI standard. I am also concerned that some of the information may be outdated...
5
5246
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that discussions of new and delete is a pretty damn involved process but I'll try to stick to the main information I'm looking for currently. I've searched around for about the last too weeks and have read up on new and overloading it to some extent but...
0
2327
by: Tim Milstead | last post by:
(CODE AT END OF POST) I have got some code for finding memory leaks that works well with: new() and needs to be extended to work with new
1
5381
by: Zachary Turner | last post by:
I'm having trouble overloading the global operator new and delete. I want to do this for debugging purposes to trace allocations and deallocations. What I have is #ifdef _DEBUG // User-defined operator new. void *operator new( size_t stAllocateBlock ) { static fInOpNew = 0; // Guard flag.
3
4652
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 >> 0x804d008 _Delete unknown block >> registered : 0x804d138 >> 0x804d008 _Delete unknown block >> 0x804d098 _Delete ok
3
385
by: md | last post by:
Hi, the following code is working for static objects. ie the statement IntArray x(20); my problem is i want to use this overloading operator for dynamically created objects for example the statement IntArray *x; Please give me some help regarding this ////////////////////////////////////////////////////////////////////////////////////////////////////////// class IntArray
9
2401
by: learning | last post by:
hi I am trying to make some simple app to learn exception safety coding. I know that new and delete can throw bad_alloc but how can I force them to throw by a flag "change" at run time? I am overloading new, new, delete and delete, change is set to 1 then it calls the custom operators. If 0, it calls the c++. But how can I overload delete and delete? The original library signature has const std::nothrow_ and throw() If I overload this,...
3
12258
by: Lighter | last post by:
The C++ Standard Doesn't Permit Overloading new and delete? In the 13.5 of The C++ standard (ISO/IEC 14882, 1998), I cannot find the specification on overloading the operators new and delete; however, many C++ books including "C++ Primer" say that the operators new and delete can be overloaded. I wonder if this has definitive specification? Who can tell me? Many thanks to those who answer.
5
3629
by: Jerry Fleming | last post by:
As I am newbie to C++, I am confused by the overloading issues. Everyone says that the four operators can only be overloaded with class member functions instead of global (friend) functions: (), , ->, =. I wonder why there is such a restriction. Some tutorials say that 'new' and 'delete' can only be overloaded with static member functions, others say that all overloading function should be non-static. Then what is the fact, and why? ...
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9398
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10156
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8831
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7375
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5275
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5419
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2805
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.