www.digitalmars.com         C & C++   DMDScript  

D - Allocate a struct on the heap?

reply Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
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))) ]
Sep 20 2002
parent reply Russell Lewis <spamhole-2001-07-16 deming-os.org> writes:
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?
Sep 23 2002
parent reply Patrick Down <pat codemoon.com> writes:
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, 


 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 


 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; }
Sep 23 2002
parent reply Russell Lewis <spamhole-2001-07-16 deming-os.org> writes:
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, 


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 


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???
Sep 23 2002
parent reply Patrick Down <pat codemoon.com> writes:
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.
Sep 23 2002
parent "Walter" <walter digitalmars.com> writes:
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.

Sep 24 2002