Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
September 20, 2002 Allocate a struct on the heap? | ||||
---|---|---|---|---|
| ||||
I feel kind of stupid to ask, since (I guess) this should be simple, but how do you allocate a single instance of a struct on the heap? new doesn't work, giving the error "Error: new can only create arrays or class objects, not <structName>'s". I know, I could allocate an array of length 1, but what I really want is a pointer, not an array (I'm going to cast it to a void pointer soon, and send it flying through C-space...) -- The Villagers are Online! villagersonline.com .[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ] .[ (a version.of(English).(precise.more)) is(possible) ] ?[ you want.to(help(develop(it))) ] |
September 23, 2002 Re: Allocate a struct on the heap? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russ Lewis | Russ Lewis wrote:
> I feel kind of stupid to ask, since (I guess) this should be simple, but
> how do you allocate a single instance of a struct on the heap? new
> doesn't work, giving the error "Error: new can only create arrays or
> class objects, not <structName>'s".
>
> I know, I could allocate an array of length 1, but what I really want is
> a pointer, not an array (I'm going to cast it to a void pointer soon,
> and send it flying through C-space...)
>
> --
> The Villagers are Online! villagersonline.com
>
> .[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ]
> .[ (a version.of(English).(precise.more)) is(possible) ]
> ?[ you want.to(help(develop(it))) ]
Any thoughts on this, anybody?
|
September 24, 2002 Re: Allocate a struct on the heap? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russell Lewis | Russell Lewis <spamhole-2001-07-16@deming-os.org> wrote in news:3D8F7EB9.8050305@deming-os.org: > Russ Lewis wrote: >> I feel kind of stupid to ask, since (I guess) this should be simple, but >> how do you allocate a single instance of a struct on the heap? new doesn't work, giving the error "Error: new can only create arrays or class objects, not <structName>'s". >> >> I know, I could allocate an array of length 1, but what I really want is >> a pointer, not an array (I'm going to cast it to a void pointer soon, and send it flying through C-space...) > > Any thoughts on this, anybody? struct Foo { int a; int b; } int main(char[][] argv) { Foo* pFoo = new Foo[1]; return 0; } |
September 24, 2002 Re: Allocate a struct on the heap? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Patrick Down | Patrick Down wrote:
> Russell Lewis <spamhole-2001-07-16@deming-os.org> wrote in news:3D8F7EB9.8050305@deming-os.org:
>
>
>>Russ Lewis wrote:
>>
>>>I feel kind of stupid to ask, since (I guess) this should be simple,
>>
> but
>
>>>how do you allocate a single instance of a struct on the heap? new
>>>doesn't work, giving the error "Error: new can only create arrays or
>>>class objects, not <structName>'s".
>>>
>>>I know, I could allocate an array of length 1, but what I really want
>>
> is
>
>>>a pointer, not an array (I'm going to cast it to a void pointer soon,
>>>and send it flying through C-space...)
>>
>>Any thoughts on this, anybody?
>
>
> struct Foo
> {
> int a;
> int b;
> }
>
> int main(char[][] argv)
> { Foo* pFoo = new Foo[1];
> return 0;
> }
>
>
But why???
|
September 24, 2002 Re: Allocate a struct on the heap? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russell Lewis | Russell Lewis <spamhole-2001-07-16@deming-os.org> wrote in news:3D8FAF8B.2050907@deming-os.org: > > But why??? You mean why can't you just do Foo* p = new Foo; //?? I don't know. <g> new Foo[1] seems pretty much the same to me. It creates a block of heap memory large enough to hold one Foo. |
September 24, 2002 Re: Allocate a struct on the heap? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Patrick Down | Looks like a compiler bug to me. -Walter "Patrick Down" <pat@codemoon.com> wrote in message news:Xns9292CBA453F87patcodemooncom@63.105.9.61... > Russell Lewis <spamhole-2001-07-16@deming-os.org> wrote in news:3D8FAF8B.2050907@deming-os.org: > > > > But why??? > > You mean why can't you just do > > Foo* p = new Foo; //?? > > I don't know. <g> new Foo[1] seems > pretty much the same to me. It > creates a block of heap memory > large enough to hold one Foo. |
Copyright © 1999-2021 by the D Language Foundation