Jump to content
  • Advertisement
Sign in to follow this  
glat

quick question

This topic is 4832 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Quick question about new. I'm writing a class that allows you to specify a size, the class then creates an internal buffer of that size. Is it safe to have code that allocates a buffer of zero?
char* data = new char[0];
The code works in MSVC++ Beta 2, but is it standard or undefined and just happens to be working for me? Thanks, glat

Share this post


Link to post
Share on other sites
Advertisement
It does work as it's supposed to. The C++ standard defines that even allocations of zero size will work and result in different addresses.

Share this post


Link to post
Share on other sites
Seems to work, don't know if it will be safe though. Why exactly do you need a buffer size of 0?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Does it really matter? There is no productive way to use a 0 sized array.

I don't know about various C++ compilers, but ideally, it should return NULL for a zero sized buffer. Having said this, if you use it correctly, this should work like any other sized buffer :). There are no elements to access, so you shouldn't access any and deleting NULL pointer can just be a no-op.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
I don't know about various C++ compilers, but ideally, it should return NULL for a zero sized buffer.


No, it shouldn't. That would cause a lot of problems. As I said in my post above, the C++ standard guarantees that the addresses will be different. This is because there can be empty classes (0 size) that can be valid and active and still need to be differentiated from each other. The behaviour MSVC++2k5b2 exhibits is correct according to the standard. It is expected and necessary. At any rate, allocating a zero-size buffer should be harmless, if you take care not to overrun the buffer (just as with any memory allocation).

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
it should return NULL for a zero sized buffer.


The default behavior is for new to throw.

Share this post


Link to post
Share on other sites
Quote:

Why exactly do you need a buffer size of 0?

In reality I don't, it was just to satisfy my curiosity.

Thanks for clearing my question, Kippesoep.

EDIT: If it returns different values for each zero allocation, does that mean that you can fill up memory with enough allocations of zero size?

Share this post


Link to post
Share on other sites
Quote:
Original post by glat
If it returns different values for each zero allocation, does that mean that you can fill up memory with enough allocations of zero size?


Yep, you'll even need to delete[] the buffer. In practice, the heap manager will allocate a few bytes more for each allocation. These bytes are used to store the size of the memory block. That way, the heap manager will know how large the block is to free/delete. This is implementation dependent, of course, but is often stored before the memory buffer you get (at a negative offset, basically).

Share this post


Link to post
Share on other sites
Quote:
Original post by krum
Quote:
Original post by Anonymous Poster
it should return NULL for a zero sized buffer.


The default behavior is for new to throw.

That's the behaviour when new fails. However, this is about what happens when the requested allocation is of zero size, and that case should not throw an exception unless the allocation fails. If it succeeds, as been said a few times, it should return a unique address.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!