Quick int pointer allocation question

Maxim Fomin maxim at maxim-fomin.ru
Fri Sep 14 10:54:24 PDT 2012


On Friday, 14 September 2012 at 09:20:03 UTC, monarch_dodra wrote:
> This is going to be quick: Is it possible to allocate and 
> initialize an int in the same line?
>
> int* p = new int(5);
>
> I haven't found a way to 1 liner it. Is it possible?
> Do I have to two liner it?
>
> int* p = new int();
> *p = 5;
>
> Thanks.

Firstly I thought this thread is purely about syntax. However, 
after rereading I think it is about problem of allocation on GC 
heap without default initialization. If I understood it right, 
possible solution is:

struct S
{
    ulong[500_000] array = void;
    void load(ulong value)
    {
       foreach (i, ref ulong v; array)
       {
          array[i] = value;
       }
    }
}

void main()
{
    // int* p = new int, x = ((*p=5) == 5) ? null : null;
    // assert(*p == 5);
    auto s = new S;
    s.load(5);
}


More information about the Digitalmars-d-learn mailing list