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

Discussion in 'C++' started by mikeyz9, Mar 11, 2010.

  1. mikeyz9

    mikeyz9 Guest

    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
    mikeyz9, Mar 11, 2010
    #1
    1. Advertising

  2. 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.
    Johannes Schaub (litb), Mar 11, 2010
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roedy Green

    String.substring, under the hood

    Roedy Green, Jan 14, 2006, in forum: Java
    Replies:
    10
    Views:
    3,430
    Alan Krueger
    Jan 16, 2006
  2. Roedy Green
    Replies:
    32
    Views:
    4,757
    Chris Uppal
    Jan 19, 2006
  3. Davmagic .Com

    What's Under The Hood? (OT)

    Davmagic .Com, Dec 9, 2003, in forum: HTML
    Replies:
    1
    Views:
    333
    Louis Somers
    Dec 9, 2003
  4. Eric Pederson

    File objects? - under the hood question

    Eric Pederson, Jan 19, 2005, in forum: Python
    Replies:
    3
    Views:
    286
    Jeremy Bowers
    Jan 21, 2005
  5. Fernando Perez
    Replies:
    2
    Views:
    90
    Fernando Perez
    Jul 6, 2010
Loading...

Share This Page