Why is allocation performed with a zero byte?

This is similar to What does allocating a zero size array mean?

I have the following code

int *p = new int[0]; delete []p; 

p gets the address and is deleted properly.

My question is: why is the allocation of null bytes allowed by the C ++ standard in the first place? Why doesn't he throw a bad_alloc or some special exception?

I think it just postpones a catastrophic failure, making life difficult for the programmer. Because, if the size to be allocated is calculated at runtime, and if the programmer correctly distributes it and tries to write something to this memory, it ends the memory corruption !!! and Crash could happen somewhere else in the code.

EDIT: How much memory does it allocate when requesting zero size?

+6
c ++ memory-management
source share
5 answers

3.7.3.1/2:

[32. The goal is to implement the new () operator by calling malloc () or calloc (), so the rules are basically the same. C ++ is different from C, requiring a null query to return a non-null pointer.]

Compare a dynamically allocated array with std::vector , for example. You can have a vector of size 0, so why not allow it for an array? And there is always an error accessing the end of the array, regardless of whether it is equal to 0 or not.

+4
source share

Why do you want him to fail? If a programmer tries to read / write non-existent elements, then this is an error. The initial distribution is not (this is no different from, for example, int *p = new int[1]; p[1] = 5; ).

+6
source share

Once upon a time, before using exceptions, the malloc function returned a NULL pointer if the distribution failed.

If allocating null bytes would also return a NULL pointer, it would be difficult to distinguish between a failed allocation and a distribution followed by a null byte.

On the other hand, if allocating null bytes will return a non-NULL pointer, you will get a situation in which two different null byte distributions may have the same pointer.

Therefore, to keep things simple, the malloc function with zero bytes allocates 1 byte.

+5
source share

The same can be said for int [N], where N> 0:

Because, if the size to be allocated is calculated at runtime, and if the programmer chooses to distribute it correctly and tries to write something at the last end of this memory, it eventually corrupts the memory !!! and Crash could happen somewhere else in the code.

+3
source share

The distribution of arrays of zero size is considered in the ISO C ++ standard under 5.3.4, paragrahp 7

When the value of an expression in direct-new-declarator is zero, the distribution function is called to allocate an array without elements.

This makes code that facilitates the distribution of dnaymic distributions.

In general: if someone calls a function and asks her to return an array with elements n (0 in your case), the code should not try to read the returned array past the nth element in any case.

So, I really do not see a catastrophic failure, since the code would be faulty for the start of any n.

How do you say:

Because if the size to be allocated is calculated at runtime, and if the programmer assumes that it is correctly distributed

The calculated size will be “0” if it tries to access more than its estimated size, then, well .. I repeat myself;)

+2
source share

All Articles