473,405 Members | 2,344 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,405 software developers and data experts.

delete [] operator help

Hi Folks,

How does the ANSI C++ specification treat the following situation?

char *ptr = new char[25];
ptr += 5;
delete [] ptr;

Will all of the memory be freed? Will only 20 bytes be freed? Is this,
perhaps, undefined?

Thanks!
Jul 23 '06 #1
5 1191
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Anonymous wrote:
Hi Folks,

How does the ANSI C++ specification treat the following situation?

char *ptr = new char[25];
ptr += 5;
delete [] ptr;

Will all of the memory be freed? Will only 20 bytes be freed? Is this,
perhaps, undefined?

Thanks!
No, it's not legal.

Just to double-check, I tried it. I get the following error at runtime:

*** glibc detected *** free(): invalid pointer: 0x080497bd ***
Aborted

What exactly are you trying to do?

- --
Regards,
Jonathan Lamothe

/*
* Oops. The kernel tried to access some bad page. We'll have to
* terminate things with extreme prejudice.
*/

die_if_kernel("Oops", regs, error_code);
-- From linux/arch/i386/mm/fault.c
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFEw21sq9nD47x87JYRAgQiAKCoJ1uLmzZc4LcTSutANe fJbnWDhACfSHQj
PuHLVB+7aOVq6YQVzNRfalE=
=OyX2
-----END PGP SIGNATURE-----
Jul 23 '06 #2
Thank you for your reply. In essence I have a function that takes a char
*&ptr and does a ptr = new char[x]. This function uses the first y bytes as
workspace and so before it returns, it does a ptr += y so that the calling
function is not bothered with its workspace. Then the calling function is
responsible for a delete [] ptr. I don't find it to be elegant to require
the calling function to know what y is so that it can do a delete [] (ptr -
y);

Thanks.
"Jonathan Lamothe" <jo**************@users.sourceforge.netwrote in message
news:44**********************@unlimited.newshostin g.com...
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Anonymous wrote:
>Hi Folks,

How does the ANSI C++ specification treat the following situation?

char *ptr = new char[25];
ptr += 5;
delete [] ptr;

Will all of the memory be freed? Will only 20 bytes be freed? Is this,
perhaps, undefined?

Thanks!

No, it's not legal.

Just to double-check, I tried it. I get the following error at runtime:

*** glibc detected *** free(): invalid pointer: 0x080497bd ***
Aborted

What exactly are you trying to do?

- --
Regards,
Jonathan Lamothe

/*
* Oops. The kernel tried to access some bad page. We'll have to
* terminate things with extreme prejudice.
*/

die_if_kernel("Oops", regs, error_code);
-- From linux/arch/i386/mm/fault.c
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFEw21sq9nD47x87JYRAgQiAKCoJ1uLmzZc4LcTSutANe fJbnWDhACfSHQj
PuHLVB+7aOVq6YQVzNRfalE=
=OyX2
-----END PGP SIGNATURE-----

Jul 23 '06 #3

"Anonymous" <An*******@home.netwrote in message
news:z_******************************@comcast.com. ..
Thank you for your reply. In essence I have a function that takes a char
*&ptr and does a ptr = new char[x]. This function uses the first y bytes
as workspace and so before it returns, it does a ptr += y so that the
calling function is not bothered with its workspace. Then the calling
function is responsible for a delete [] ptr. I don't find it to be
elegant to require the calling function to know what y is so that it can
do a delete [] (ptr - y);
I wouldn't consider it elegant for the caller to be responsible
for deleting something that it didn't new to begin with.
Why not avoid the whole issue and use a container
that doesn't require this like std::string or std::vector<char>
etc.?
Jul 23 '06 #4
In article <z_******************************@comcast.com>,
"Anonymous" <An*******@home.netwrote:
Thank you for your reply. In essence I have a function that takes a char
*&ptr and does a ptr = new char[x]. This function uses the first y bytes as
workspace and so before it returns, it does a ptr += y so that the calling
function is not bothered with its workspace. Then the calling function is
responsible for a delete [] ptr. I don't find it to be elegant to require
the calling function to know what y is so that it can do a delete [] (ptr -
y);
There are a couple of ways to deal with this. One is for the function to
return an object that knows the beginning of the memory but doesn't
expose it to the caller. Another would be to simply put the 'workspace'
at the end of the block instead of the beginning.
Jul 23 '06 #5
Anonymous wrote:
Hi Folks,

How does the ANSI C++ specification treat the following situation?

char *ptr = new char[25];
ptr += 5;
delete [] ptr;

Will all of the memory be freed?
Maybe, maybe not.
Will only 20 bytes be freed?
Maybe, maybe not.
Is this, perhaps, undefined?
Yes, it is.

Jul 23 '06 #6

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

Similar topics

2
by: Ian McBride | last post by:
(was: delete() confusion) I have a class with multiple base classes. One of these base classes (base1) has its own new/delete operators and nothing else. Another base class (base 2) has a...
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...
3
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...
5
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...
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...
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 >>...
9
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...
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...
12
by: yufufi | last post by:
Hello, How does delete know how much memory to deallocate from the given pointer? AFAIK this informations is put there by new. new puts the size of the allocated memory before the just before...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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...

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.