Thread overview | |||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
September 14, 2012 Quick int pointer allocation question | ||||
---|---|---|---|---|
| ||||
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. |
September 14, 2012 Re: Quick int pointer allocation question | ||||
---|---|---|---|---|
| ||||
Posted in reply to monarch_dodra | 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.
Or dup a pointer to an int for that matter:
int* p1 = ... ;
int* p2 = p1.dup;
?
|
September 14, 2012 Re: Quick int pointer allocation question | ||||
---|---|---|---|---|
| ||||
Posted in reply to monarch_dodra | On Friday, 14 September 2012 at 09:22:09 UTC, monarch_dodra wrote:
> 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.
>
> Or dup a pointer to an int for that matter:
>
> int* p1 = ... ;
> int* p2 = p1.dup;
>
> ?
Whilst I'm on the subject of questions, how does one allocate, but bypassing the extra memcpy of T.init? Is this possible?
|
September 14, 2012 Re: Quick int pointer allocation question | ||||
---|---|---|---|---|
| ||||
Posted in reply to monarch_dodra | > 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 |
September 14, 2012 Re: Quick int pointer allocation question | ||||
---|---|---|---|---|
| ||||
Posted in reply to monarch_dodra | 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 |
September 14, 2012 Re: Quick int pointer allocation question | ||||
---|---|---|---|---|
| ||||
Posted in reply to David | 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. |
September 14, 2012 Re: Quick int pointer allocation question | ||||
---|---|---|---|---|
| ||||
Posted in reply to monarch_dodra | On 2012-09-14 12:52, monarch_dodra wrote: >> int x = void; >> >> http://dpaste.dzfl.pl/24c1baa9 > > Hum, but that is a stack allocated variable. Perhaps using GC.malloc? -- /Jacob Carlborg |
September 14, 2012 Re: Quick int pointer allocation question | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | On Friday, 14 September 2012 at 11:17:55 UTC, Jacob Carlborg wrote:
> On 2012-09-14 12:52, monarch_dodra wrote:
>
>>> int x = void;
>>>
>>> http://dpaste.dzfl.pl/24c1baa9
>>
>> Hum, but that is a stack allocated variable.
>
> Perhaps using GC.malloc?
Hum, apparently, there is a second (default aka-hidden) argument that is a bitmask applied to the allocated memory. So not much gain there.
I'm allocating an array of 500_000 ulongs, and afterwards, I'm initializing them all "by hand", making the default allocation useless.
I'm not going to lose any sleep over this, but there is no way in D to get (garbage collected) un-initialized memory/allocations?
|
September 14, 2012 Re: Quick int pointer allocation question | ||||
---|---|---|---|---|
| ||||
Posted in reply to monarch_dodra | On Fri, 14 Sep 2012 05:20:16 -0400, monarch_dodra <monarchdodra@gmail.com> 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;
int *p = [5].ptr;
-Steve
|
September 14, 2012 Re: Quick int pointer allocation question | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Friday, 14 September 2012 at 14:33:51 UTC, Steven Schveighoffer wrote:
> On Fri, 14 Sep 2012 05:20:16 -0400, monarch_dodra <monarchdodra@gmail.com> 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;
>
> int *p = [5].ptr;
>
> -Steve
Fancy!
Thankyou.
|
Copyright © 1999-2021 by the D Language Foundation