allocating zero bytes



 DEVELOP > c-Plus-Plus > allocating zero bytes

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Serve Laurijssen"
Date: 20 Jan 2008 11:42:09 AM
Object: allocating zero bytes
char *s = new(std::nothrow) char[0];
std::cout << (void *)s << std::endl;
On my machine the value of s does not equal NULL. What does the standard say
about that?
.

User: "peter koch"

Title: Re: allocating zero bytes 20 Jan 2008 11:53:21 AM
On 20 Jan., 18:42, "Serve Laurijssen" <n...@hao.com> wrote:

=A0char *s =3D new(std::nothrow) char[0];

=A0std::cout << (void *)s << std::endl;

On my machine the value of s does not equal NULL. What does the standard s=

ay

about that?

This is fine and defined. Remember to delete [] when you have finished
using the pointer. I assume you are on an embedded platform doing
this? I would normally expect std::vector or std::string to be better
for this (calling terminate if the allocation should fail).
/Peter
.
User: "Serve Laurijssen"

Title: Re: allocating zero bytes 20 Jan 2008 12:28:49 PM
"peter koch" <peter.koch.larsen@gmail.com> schreef in bericht
news:d17eaa53-2e0b-4bcb-a754-0dcd98404013@j20g2000hsi.googlegroups.com...
On 20 Jan., 18:42, "Serve Laurijssen" <n...@hao.com> wrote:

char *s = new(std::nothrow) char[0];

std::cout << (void *)s << std::endl;

On my machine the value of s does not equal NULL. What does the standard
say
about that?

This is fine and defined. Remember to delete [] when you have finished
using the pointer. I assume you are on an embedded platform doing
this? I would normally expect std::vector or std::string to be better
for this (calling terminate if the allocation should fail).
After allocating zero, is the following UB then?
std::cout << *s << std::endl;
.
User: "Jack Klein"

Title: Re: allocating zero bytes 20 Jan 2008 01:17:33 PM
On Sun, 20 Jan 2008 19:28:49 +0100, "Serve Laurijssen" <ni@hao.com>
wrote in comp.lang.c++:


"peter koch" <peter.koch.larsen@gmail.com> schreef in bericht
news:d17eaa53-2e0b-4bcb-a754-0dcd98404013@j20g2000hsi.googlegroups.com...
On 20 Jan., 18:42, "Serve Laurijssen" <n...@hao.com> wrote:

char *s = new(std::nothrow) char[0];

std::cout << (void *)s << std::endl;

On my machine the value of s does not equal NULL. What does the standard
say
about that?


This is fine and defined. Remember to delete [] when you have finished
using the pointer. I assume you are on an embedded platform doing
this? I would normally expect std::vector or std::string to be better
for this (calling terminate if the allocation should fail).

After allocating zero, is the following UB then?

std::cout << *s << std::endl;

Please find the setting in your Windows Mail program to properly quote
material in your reply. Thank you.
As to your follow-up question, yes, it is undefined behavior. There
are exactly 0 bytes that you may access through that pointer, which is
exactly what you asked for.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
.




  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER