C++ New Int[0] - Will It Allocate Memory

C++ new int[0] -- will it allocate memory?

From 5.3.4/7

When the value of the expression in a direct-new-declarator is zero, the allocation function is called to allocate an array with no elements.

From 3.7.3.1/2

The effect of dereferencing a pointer returned as a request for zero size is undefined.

Also

Even if the size of the space requested [by new] is zero, the request can fail.

That means you can do it, but you can not legally (in a well defined manner across all platforms) dereference the memory that you get - you can only pass it to array delete - and you should delete it.

Here is an interesting foot-note (i.e not a normative part of the standard, but included for expository purposes) attached to the sentence from 3.7.3.1/2

[32. The intent is to have operator new() implementable by calling malloc() or calloc(), so the rules are substantially the same. C++ differs from C in requiring a zero request to return a non-null pointer.]

Use `delete []` when `new [0]`

It's ok to new a zero-sized array, and to delete it. This is more of a convenience than anything, as it means you don't have to write separate conditions for the 0 case.

Memory size of the new allocated int in c++, Is there a different and better way to see it?

No, there is no way. The compiler internally adds information about how much memory was allocated and how many elements were created by new[], because otherwise it couldn't perform delete[] correctly. However, there is no portable way in C++ to get that information and use it directly.

So you have to store the size separately while you still know it.

Actually, you don't, because std::vector does it for you:

#include <iostream>
#include <vector>
#include <new>

int main() {

const int gib = 268435256;

try {
std::vector<int> v(gib);
std::cout << (v.capacity() * sizeof(int)) << '\n';
} catch (std::bad_alloc const& e) {
std::cerr << e.what() << '\n';
}
}

You should practically never use new[]. Use std::vector.


Note that I've used capacity and not size, because size tells you how many items the vector represents, and that number can be smaller than the number of elements supported by the vector's currently allocated memory.

There is also no way to avoid the sizeof, because the size of an int can vary among implementations. But that's not a problem, either, because a std::vector cannot lose its type information, so you always know how big one element is.

You wouldn't need the multiplication if it was a std::vector<char>, a std::vector<unsigned char> or a std::vector<signed char>, because those three character types' sizeof is guaranteed to be 1.

Does new[] allocate memory contiguously?

The C++ standard absolutely guarantees this.

arr[0] through to arr[9] are contiguous with no padding allowed between elements. Pointer arithmetic is valid in the allocated region. You are allowed to set a pointer to arr + 10, but don't dereference it.

This applies to any class. The amount of memory allocated per element is sizeof(Y) where Y is the class or plain old data type.

Allocating memory by size zero in c++?

In c++, what happens if allocate memory size by zero?

A zero sized array is valid.

You just can de-reference any members.

This is why the Greeks spent such a long time debating it thinking about zero as a number. But we can thank India at its great minds for eventually bringing zero as a number 0. It took them forever and aren't we glad they did, a lot of modern maths would not be possible without such a unique value.

after allocation I get valid memory pointer,

That is correct.

If it failed it would throw an exception.

and prints valid number.

Your assignment: p = &a; is probably not what you want. As this just leaks memory and set p to point at the variable a. What you were probably trying to do is:

(*p) = a;
// or
p[0] = a;

This is undefined behavior.

As you are assigning to values after the end of the array.

But I think new operator should return something like FAILD or NULL.

No.

Can anybody explain detail?

Yes.



Related Topics



Leave a reply



Submit