[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-devel
Subject:    Re: dumb C++ question
From:       Johannes Sixt <Johannes.Sixt () telecom ! at>
Date:       1999-05-25 18:34:46
[Download RAW message or body]

On Sun, 23 May 1999, Stephan Kulow wrote:
>Hi!
>
>What does new int[0]?
>
>In QString (about line 10300) is:
>
>    QChar* result = 0;
>    uint l = 0;
>    if ( str ) {
>        if ( maxlen != (uint)-1 ) {
>            while (str[l] && l < maxlen)
>                l++;
>        } else {
>            // Faster?
>            l = strlen(str);
>        }
>->        QChar *uc = new QChar[l];
>        result = uc;
>        uint i = l;
>        while ( i-- )
>            *uc++ = *str++;
>    }                               
>
>if str == "", then the marked line will
>do new QChar[0]. But what does it return?
>A test said me, it returns something, but
>what does it return? Is it defined? I must
>admit, it's the first time I think about this
>"problem" :)

It is ok. The C++ standard requires:

"When the value of the expression ... is zero, the allocation function is called
to allocate an array with no elements. The pointer returned by the
new-expression is non-null and distinct from the pointer to any other object."

An implementation usually just allocates the smallest possible chunk of memory
and returns a pointer to it.

As a detail aside, the cited requirement only addresses non-constant
expressions. OTOH, writing the constant 0, as eg. in   new QChar[0]   is not
allowed.

-- Hannes

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic