Quick int pointer allocation question

monarch_dodra monarchdodra at gmail.com
Fri Sep 14 03:52:39 PDT 2012


On Friday, 14 September 2012 at 10:33:47 UTC, David wrote:
>> Whilst I'm on the subject of questions, how does one allocate, 
>> but
>> bypassing the extra memcpy of T.init? Is this possible?
>
> int x = void;
>
> http://dpaste.dzfl.pl/24c1baa9

Hum, but that is a stack allocated variable.

What about:

--------
struct S { }
void main()
{
     S* ps = new S(void); //? doesn't work
}

On Friday, 14 September 2012 at 10:37:56 UTC, Jonathan M Davis 
wrote:
> On Friday, September 14, 2012 11:20:16 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?
>
> Nope. Though I think that it should be.
>
>> Do I have to two liner it?
>> 
>> int* p = new int();
>> *p = 5;
>
> Yep. Though I have a pull request which will make it so that 
> you can do
>
> auto p = makeNew!int(5);
>
> https://github.com/D-Programming-Language/phobos/pull/756
>
> - Jonathan M Davis

Thanks. Glad we have a library solution, but that's the kind of 
thing that should work out of the box I think.


More information about the Digitalmars-d-learn mailing list