www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Pointer to array allocation

reply 0ffh <spam frankhirsch.net> writes:
Hi!

Probably most here know that something like:

   alias Var[] Lst;

   void test()
   {
     Lst *l=new Lst;
     foo(l);
   }

will result in "Error: new can only create structs,
dynamic arrays or class objects, not Var []'s".
So what is the D way of doing what I mean?
I came up with:

   Lst* newLst()
   {
     void[] v;
     v.length=Lst.sizeof;
     return cast(Lst*)v.ptr;
   }

but this looks like an ugly hack to me, actually
I'm not even sure yet it won't run into trouble...

Regards, Frank

p.s.
/I really do mean a pointer to a dynamic array./
Jun 17 2007
next sibling parent Myron Alexander <someone somewhere.com> writes:
Frank,

I have looked through the documentation, and some of the source code, 
and I cannot find how to get a pointer to a dynamic array. I don't know 
if it is possible, or if there is some undocumented feature.

If it is not possible, then is this by design, or an oversight?

Hope someone can help.

Regards,

Myron.
Jun 17 2007
prev sibling next sibling parent reply BCS <ao pathlink.com> writes:
Reply to 0ffh,

 Hi!

alias Var[] Lst; void test() { Lst *l=(new Lst[1]).ptr; }
Jun 17 2007
parent reply 0ffh <spam frankhirsch.net> writes:
Thanks for the answers!

Derek Parnell wrote:
 struct LD
 {
   Lst da;
 }

Thanks for the suggestion! Been thinking about the same thing, but it's a bit unwieldy: You'd have to write bar.da[baz] all the time instead of just bar[baz]. BCS wrote:
 void test()
 {
   Lst *l=(new Lst[1]).ptr;
 }

That looks a much less hacky than what I did, all but neat... cool! :) Myron: Thanks, too! Regards, Frank
Jun 18 2007
parent reply Myron Alexander <someone somewhere.com> writes:
0ffh wrote:
  Myron: Thanks, too!

No problem. I'm curious as to the reason for a pointer to a dynamic array instead of a pointer to the array? How do you use the Lst* and what does it allow you to do that the pointer to the array cannot. Regards, Myron.
Jun 18 2007
parent reply BCS <ao pathlink.com> writes:
Reply to Myron,

 0ffh wrote:
 
  Myron: Thanks, too!
 

I'm curious as to the reason for a pointer to a dynamic array instead of a pointer to the array? How do you use the Lst* and what does it allow you to do that the pointer to the array cannot. Regards, Myron.

I have done this befor typedef char[] char_str typedef char_str* char_str_ptr char_str[] data; //set up data holes data.length = 3; //set up template char_str[] template = ["hello "[], "\n you have won a ", " and it will be sent to ", \n]; // build array of parts char_str_ptr[] all = [&template[0], &data[0], &template[1], &data[1], &template[2], &data[2], &template[3]]; // fill holes data[0] = "bob"; data[1] = "fork"; data[3] = "123 2nd st"; // dump foreach(i, all) writef("%s", *i); // refill holes data[0] = "sally"; data[1] = "airplane"; data[3] = "1645 w apple Avn."; //dump again foreach(i, all) writef("%s", *i);
Jun 18 2007
parent 0ffh <spam frankhirsch.net> writes:
BCS wrote:
 Myron wrote:
 I'm curious as to the reason for a pointer to a dynamic array instead
 of a pointer to the array? How do you use the Lst* and what does it
 allow you to do that the pointer to the array cannot.

[...]

In my particular case the reason is a union where all alternatives take 4 bytes but for the array, which takes 8. As I expect *lots* of instances of this union - and only a small part being of the array case - I wanted to avoid the waste of good memory... :) Regards, Frank
Jun 19 2007
prev sibling parent Derek Parnell <derek nomail.afraid.org> writes:
On Sun, 17 Jun 2007 23:51:25 +0200, 0ffh wrote:

 So what is the D way of doing what I mean?

 /I really do mean a pointer to a dynamic array./

Does this do the trick for you? alias int[] Lst; struct LD { Lst da; } void test() { LD* l=new LD; // Structs can be new'd on heap. foo(&l.da); // Use address of dynarr in struct. } void foo(Lst *p) { } -- Derek (skype: derek.j.parnell) Melbourne, Australia "Justice for David Hicks!" 18/06/2007 3:19:35 PM
Jun 17 2007