delete [] operator help

A

Anonymous

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!
 
J

Jonathan Lamothe

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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

iD8DBQFEw21sq9nD47x87JYRAgQiAKCoJ1uLmzZc4LcTSutANefJbnWDhACfSHQj
PuHLVB+7aOVq6YQVzNRfalE=
=OyX2
-----END PGP SIGNATURE-----
 
A

Anonymous

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 said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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

iD8DBQFEw21sq9nD47x87JYRAgQiAKCoJ1uLmzZc4LcTSutANefJbnWDhACfSHQj
PuHLVB+7aOVq6YQVzNRfalE=
=OyX2
-----END PGP SIGNATURE-----
 
D

Duane Hebert

Anonymous said:
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.?
 
D

Daniel T.

"Anonymous said:
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.
 

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
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top