quick question

Started by
7 comments, last by Brother Bob 18 years, 7 months ago
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
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.
Kippesoep
Seems to work, don't know if it will be safe though. Why exactly do you need a buffer size of 0?
______________________TradeMark Designs
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.
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).
Kippesoep
Quote:Original post by Anonymous Poster
it should return NULL for a zero sized buffer.


The default behavior is for new to throw.

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?
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).
Kippesoep
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.

This topic is closed to new replies.

Advertisement