Zero-length array under the hood? How does it work?

M

mikeyz9

Hi,

I understand that you can actually create object with zero length
array. Like example below:

struct Weirdness
{
int i;
Weirdness() : i(10) {std::cout << "im here" << std::endl;}
};

Weirdness* testing = new Weirdness[0];

My understanding is that new operator actually creates a new object of
the array size given from Heap through malloc/calloc at the end of the
line. Can anyone give me under the hood explanation on what is
actually going on with zero-length array, perhaps compiler
implementations on how it behaves under the hood?
My questions are the following:
1. Where does it allocate memory from?
2. Where does the zero-length array pointer points to? a valid address
but only readable?
3. What is the size being allocated?
5. Does it allocate several bytes randomly from the heap? or there is
a special section of memory dedicated for this weirdness?
6. Can a user use delete operator to de-allocate this memory?

When I tried the code above using GNU Compiler (gcc 4.3.3), It gives
me back valid address but I can not write anything to it (check the
code above, it will set i to 0 default value on new Weirdness[0];
while i to 10 for new Weirdness[1];). It also did not print anything,
it looks like the constructor not even being called at all.

Thanks in advance for the help,
Tom
 
J

Johannes Schaub (litb)

mikeyz9 said:
Hi,

I understand that you can actually create object with zero length
array. Like example below:

struct Weirdness
{
int i;
Weirdness() : i(10) {std::cout << "im here" << std::endl;}
};

Weirdness* testing = new Weirdness[0];

My understanding is that new operator actually creates a new object of
the array size given from Heap through malloc/calloc at the end of the
line. Can anyone give me under the hood explanation on what is
actually going on with zero-length array, perhaps compiler
implementations on how it behaves under the hood?
My questions are the following:
1. Where does it allocate memory from?

From where it gets the other memory too. For instance, from malloc. Notice
tho that it must never return 0. So if on that platform malloc returns 0 for
zero size requests, another technique has to be used. (including passing 1
instead of 0 to malloc).
2. Where does the zero-length array pointer points to? a valid address
but only readable?
Yes, only the pointer value returned can be accessed (acting as a past-the-
end value). You cannot dereference it.
3. What is the size being allocated?

For array allocations, the standard grants the implementation to add an
arbitrary value to the size requested from the allocation function (for
storing the element count allocated, for example).
5. Does it allocate several bytes randomly from the heap? or there is
a special section of memory dedicated for this weirdness?

Nothing is said by the Standard about a special memory pool for zero length
arrays. It uses the same operator[] like other allocations too. (instead of
that literal 0, you may have given a runtime value evaluating to 0 too).

Notice that each such allocation has to return another address (unless prior
allocations were free'ed subsequently, of course).
6. Can a user use delete operator to de-allocate this memory?
Yes, the user can use it, to avoid memory leaks like with other new
allocations.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top