View mode: basic / threaded / horizontal-split · Log in · Help
June 17, 2007
Pointer to array allocation
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./
June 17, 2007
Re: Pointer to array allocation
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.
June 17, 2007
Re: Pointer to array allocation
Reply to 0ffh,

> Hi!
[...]

alias Var[] Lst;

void test()
{
Lst *l=(new Lst[1]).ptr;
}
June 18, 2007
Re: Pointer to array allocation
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
June 18, 2007
Re: Pointer to array allocation
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
June 18, 2007
Re: Pointer to array allocation
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.
June 18, 2007
Re: Pointer to array allocation
Reply to Myron,

> 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.
> 

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);
June 19, 2007
Re: Pointer to array allocation
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.
> I have done this befor
> [...]

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
Top | Discussion index | About this forum | D home