Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Zero-length array under the hood? How does it work?

Reply
Thread Tools

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

 
 
mikeyz9
Guest
Posts: n/a
 
      03-11-2010
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



 
Reply With Quote
 
 
 
 
Johannes Schaub (litb)
Guest
Posts: n/a
 
      03-11-2010
mikeyz9 wrote:

> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Tomcat 5.5+ On a Mac, Under Eclipse, Under OS X Edward V. Berard Java 4 04-04-2006 05:14 AM
Why does Rpy/R-plot work under PythonWin, but not under commandline/IDLE? Bo Peng Python 1 01-09-2006 07:05 AM
help : my jar file is not running under linux terminal , but it runs under JbuilderX ide bronby Java 1 07-15-2005 07:23 AM
Java application developped under Linux running ridiculously slow under Windows hshdude Java 12 11-04-2004 05:49 PM



Advertisments