May 04, 2005
How about
  void main()
  {
    char[1000] abc;
  }
?

Maxime Larose wrote:
> I'm I missing something or is there no way to have true macros in D?  The
> facilities to generate boiler plate code doesn't seem to go far enough.
> 
> For instance, I want to define an alias/template/mixin/whatever for
> simplifying the syntax to create stuff on the stack. Here is how the code
> would look ideally:
> 
> void main()
> {
>   char[] abc = stack_char(1000);
> }
> 
> stack_char being a specialization of a stack_array!(T) or something like
> that.
> 
> I tried a thousand variations with mixins, templates and aliases, the
> compiler is always complaining one way or the other. And when it works, a
> function is called (defeating the purpose of creating the variable in the
> local stack).
> 
> Even doing something very simply like:
> alias cast(char*)std.c.stdlib.alloca stack_char;
> 
> doesn't work. The compiler complains about the (char*).
> 
> 
> Less ideally, I thought of doing:
> stack_array(abc, char, 1000);
> 
> which would expand to:
> char[] abc = (cast(char*)std.c.stdlib.alloca(1000))[0..1000]
> 
> This seems simple enough. However, it doesn't work because "abc" is not
> defined prior to use. (Even when I use an "alias" parameter for the
> template.)
> 
> I'm I missing something? This seems like very simple stuff...
> 
> 
> 
> 
> 
May 04, 2005
I'd forgotten you were doing stack allocations :p  What about a memory manager that uses a static array of ubyte as a memory pool?


In article <d5b0d5$2en9$1@digitaldaemon.com>, Maxime Larose says...
>
>In debug mode, that would simply crash... I.e. the "stack" allocated variable would be allocated on the stack of the callee, not the caller. Inlining only partially solves the problem in that you are never sure of what gets inlined and what does not. It leads to non-portable code at its worst.
>
>"Sean Kelly" <sean@f4.ca> wrote in message news:d5asqh$2arf$1@digitaldaemon.com...
>> In article <d5apgr$279m$1@digitaldaemon.com>, Maxime Larose says...
>> >
>> >I'm I missing something or is there no way to have true macros in D?  The facilities to generate boiler plate code doesn't seem to go far enough.
>>
>> Mixins are about as close as you can get.  Why not use plain old functions
>and
>> compile with -inline?  That should address performance concerns in most
>cases.
>>
>>
>> Sean
>>
>>
>
>


May 04, 2005
This will create an arrray in the heap, not on the stack. (That's a main
difference between C(++) and D)


"Russ Lewis" <spamhole-2001-07-16@deming-os.org> wrote in message news:d5b42s$2hns$1@digitaldaemon.com...
> How about
>    void main()
>    {
>      char[1000] abc;
>    }
> ?
>
> Maxime Larose wrote:
> > I'm I missing something or is there no way to have true macros in D?
The
> > facilities to generate boiler plate code doesn't seem to go far enough.
> >
> > For instance, I want to define an alias/template/mixin/whatever for simplifying the syntax to create stuff on the stack. Here is how the
code
> > would look ideally:
> >
> > void main()
> > {
> >   char[] abc = stack_char(1000);
> > }
> >
> > stack_char being a specialization of a stack_array!(T) or something like
> > that.
> >
> > I tried a thousand variations with mixins, templates and aliases, the compiler is always complaining one way or the other. And when it works,
a
> > function is called (defeating the purpose of creating the variable in
the
> > local stack).
> >
> > Even doing something very simply like:
> > alias cast(char*)std.c.stdlib.alloca stack_char;
> >
> > doesn't work. The compiler complains about the (char*).
> >
> >
> > Less ideally, I thought of doing:
> > stack_array(abc, char, 1000);
> >
> > which would expand to:
> > char[] abc = (cast(char*)std.c.stdlib.alloca(1000))[0..1000]
> >
> > This seems simple enough. However, it doesn't work because "abc" is not defined prior to use. (Even when I use an "alias" parameter for the template.)
> >
> > I'm I missing something? This seems like very simple stuff...
> >
> >
> >
> >
> >


May 04, 2005
> This variant is not really re-useable because the variable name is fixed,
> i.e. "v". It prevents you from having many such variables in the same
> function (or forces you to define many different templates...)  (I had
> tried
> it)

What about this:

import std.stdio;
import std.c.stdlib;

template stackArray(T, uint N)
{
  char[] elements = (cast(T*)std.c.stdlib.alloca(N*T.sizeof))[0..N];
}

int main(char[][] args)
{
  mixin stackArray!(char,1000) one;
  mixin stackArray!(char,2000) two;
  one.elements[0] = 'h';
  two.elements[0] = 'h';
  writef("%d\n", one.elements.length );
  writef("%d\n", two.elements.length );
  return 0;
}



"Maxime Larose" <mlarose@broadsoft.com> wrote in message news:d5atv3$2c3j$1@digitaldaemon.com...
>> >> template stackArray(T, uint N)
>> >> {
>> >>   char[] v = (cast(T*)std.c.stdlib.alloca(N))[0..N];
>> >> }
>
> This variant is not really re-useable because the variable name is fixed,
> i.e. "v". It prevents you from having many such variables in the same
> function (or forces you to define many different templates...)  (I had
> tried
> it)
>
>
>> > template stack_array(T, size_t count)
>> > {
>> >    T[] value = (cast(T*)std.c.stdlib.alloca(count * T.sizeof))[0 ..
>> > count];
>> > }
>> >
>> > int main()
>> > {
>> >    mixin stack_array!(char, 500) abc;
>> >
>> >    abc.value[0 .. 500] = 'f';
>
> This variant is good and generally useable. I didn't try it, because I
> didn't know it was possible to put the name of a variable after the
> mixin...
> It's not in the doc at any rate and not really intuitive (IMO). But if it
> works... ;)
>
> All right, thanks. I'll try it out tonight.
>
> Max
>
>
>
>
>
> "Andrew Fedoniouk" <news@terrainformatica.com> wrote in message news:d5ariq$29j2$1@digitaldaemon.com...
>> Oh, yeh :)))
>>
>> Thanks a lot.
>> As always with concept cars
>> they can move in theory but in practice only on podium.
>> That was just such a car. Quick and ugly.
>>
>> Beg my pardon,
>>
>> Uwe, thanks you too.
>>
>> Andrew.
>>
>>
>>
>>
>> "Vathix" <vathix@dprogramming.com> wrote in message news:op.sp9cntpqkcck4r@esi...
>> > On Wed, 04 May 2005 11:41:23 -0400, Andrew Fedoniouk <news@terrainformatica.com> wrote:
>> >
>> >> Maxime,
>> >>
>> >> could you check your code again?
>> >> Following just works:
>> >> ----------------------------------
>> >> import std.stdio;
>> >> import std.c.stdlib;
>> >>
>> >> template stackArray(T, uint N)
>> >> {
>> >>   char[] v = (cast(T*)std.c.stdlib.alloca(N))[0..N];
>> >> }
>> >>
>> >> int main(char[][] args)
>> >> {
>> >>   mixin stackArray!(char,1000);
>> >>   v[0] = 'h';
>> >>   writef("%d\n", v.length );
>> >>   return 0;
>> >> }
>> >> -----------------------------------
>> >
>> >
>> > Beat me to it :p
>> >
>> >
>> > private import std.c.stdlib, std.stdio;
>> >
>> > template stack_array(T, size_t count)
>> > {
>> >    T[] value = (cast(T*)std.c.stdlib.alloca(count * T.sizeof))[0 ..
>> > count];
>> > }
>> >
>> > int main()
>> > {
>> >    mixin stack_array!(char, 500) abc;
>> >
>> >    abc.value[0 .. 500] = 'f';
>> >    writefln("The length is %d", abc.value.length);
>> >    writefln("The answer is '%s'", abc.value);
>> >
>> >    return 0;
>> > }
>>
>>
>
> 


May 04, 2005
I'm pretty sure that this is not correct.  Fixed-length arrays are created on the stack.  Dynamic arrays, of course, are on the heap.  Try running this program:

  import std.stdio;
  void main() {
    int i;
    char[1000] abc;
    int j;
    writefln("&i=%d &abc=%d &abc[0]=%d &j=%d\n", &i,&abc,&abc[0],&j);
  }

I get:
  &i=bffff458 &abc=bffff45c &abc[0]=bffff45c &j=bffff844

Maxime Larose wrote:
> This will create an arrray in the heap, not on the stack. (That's a main
> difference between C(++) and D)
> 
> "Russ Lewis" <spamhole-2001-07-16@deming-os.org> wrote in message
> news:d5b42s$2hns$1@digitaldaemon.com...
> 
>>How about
>>   void main()
>>   {
>>     char[1000] abc;
>>   }
>>?
>>
>>Maxime Larose wrote:
>>
>>>I'm I missing something or is there no way to have true macros in D?
> 
> The
> 
>>>facilities to generate boiler plate code doesn't seem to go far enough.
>>>
>>>For instance, I want to define an alias/template/mixin/whatever for
>>>simplifying the syntax to create stuff on the stack. Here is how the
> 
> code
> 
>>>would look ideally:
>>>
>>>void main()
>>>{
>>>  char[] abc = stack_char(1000);
>>>}
>>>
>>>stack_char being a specialization of a stack_array!(T) or something like
>>>that.
>>>
>>>I tried a thousand variations with mixins, templates and aliases, the
>>>compiler is always complaining one way or the other. And when it works,
> 
> a
> 
>>>function is called (defeating the purpose of creating the variable in
> 
> the
> 
>>>local stack).
>>>
>>>Even doing something very simply like:
>>>alias cast(char*)std.c.stdlib.alloca stack_char;
>>>
>>>doesn't work. The compiler complains about the (char*).
>>>
>>>
>>>Less ideally, I thought of doing:
>>>stack_array(abc, char, 1000);
>>>
>>>which would expand to:
>>>char[] abc = (cast(char*)std.c.stdlib.alloca(1000))[0..1000]
>>>
>>>This seems simple enough. However, it doesn't work because "abc" is not
>>>defined prior to use. (Even when I use an "alias" parameter for the
>>>template.)
>>>
>>>I'm I missing something? This seems like very simple stuff...
>>>
>>>
>>>
>>>
>>>
> 
> 
> 
May 04, 2005
I don't think this is true.  Static arrays should be allocated on the stack just as in C.  The page on arrays and the page on memory management seem to confirm this.  The only catch is that this syntax *will* zero out the array contents, which may be undesirable from a performance perspective.

In article <d5b9dl$2n39$1@digitaldaemon.com>, Maxime Larose says...
>
>This will create an arrray in the heap, not on the stack. (That's a main
>difference between C(++) and D)
>
>
>"Russ Lewis" <spamhole-2001-07-16@deming-os.org> wrote in message news:d5b42s$2hns$1@digitaldaemon.com...
>> How about
>>    void main()
>>    {
>>      char[1000] abc;
>>    }


May 04, 2005
In article <d5b9dl$2n39$1@digitaldaemon.com>, Maxime Larose says...
>
>This will create an arrray in the heap, not on the stack. (That's a main
>difference between C(++) and D)

Is that bad?  Would that cause performance problems?  I'm guessing the answers to these questions are it depends (what application domain?; what's bad?)

Is that a fundamental shift from other languages?

-Kramer

>
>
>"Russ Lewis" <spamhole-2001-07-16@deming-os.org> wrote in message news:d5b42s$2hns$1@digitaldaemon.com...
>> How about
>>    void main()
>>    {
>>      char[1000] abc;
>>    }
>> ?
>>
>> Maxime Larose wrote:
>> > I'm I missing something or is there no way to have true macros in D?
>The
>> > facilities to generate boiler plate code doesn't seem to go far enough.
>> >
>> > For instance, I want to define an alias/template/mixin/whatever for simplifying the syntax to create stuff on the stack. Here is how the
>code
>> > would look ideally:
>> >
>> > void main()
>> > {
>> >   char[] abc = stack_char(1000);
>> > }
>> >
>> > stack_char being a specialization of a stack_array!(T) or something like
>> > that.
>> >
>> > I tried a thousand variations with mixins, templates and aliases, the compiler is always complaining one way or the other. And when it works,
>a
>> > function is called (defeating the purpose of creating the variable in
>the
>> > local stack).
>> >
>> > Even doing something very simply like:
>> > alias cast(char*)std.c.stdlib.alloca stack_char;
>> >
>> > doesn't work. The compiler complains about the (char*).
>> >
>> >
>> > Less ideally, I thought of doing:
>> > stack_array(abc, char, 1000);
>> >
>> > which would expand to:
>> > char[] abc = (cast(char*)std.c.stdlib.alloca(1000))[0..1000]
>> >
>> > This seems simple enough. However, it doesn't work because "abc" is not defined prior to use. (Even when I use an "alias" parameter for the template.)
>> >
>> > I'm I missing something? This seems like very simple stuff...
>> >
>> >
>> >
>> >
>> >
>
>


May 04, 2005
Reading the section on memory management, I really got the feeling that arrays were created on the heap... I got confused with classes apparently. Very sorry to have wasted your time... (Though I'm glad I now know the "mixin template!(T) var" way of using mixins...)



"Sean Kelly" <sean@f4.ca> wrote in message news:d5bakl$2oau$1@digitaldaemon.com...
> I don't think this is true.  Static arrays should be allocated on the
stack just
> as in C.  The page on arrays and the page on memory management seem to
confirm
> this.  The only catch is that this syntax *will* zero out the array
contents,
> which may be undesirable from a performance perspective.
>
> In article <d5b9dl$2n39$1@digitaldaemon.com>, Maxime Larose says...
> >
> >This will create an arrray in the heap, not on the stack. (That's a main
> >difference between C(++) and D)
> >
> >
> >"Russ Lewis" <spamhole-2001-07-16@deming-os.org> wrote in message news:d5b42s$2hns$1@digitaldaemon.com...
> >> How about
> >>    void main()
> >>    {
> >>      char[1000] abc;
> >>    }
>
>


May 04, 2005
"Maxime Larose" <mlarose@broadsoft.com> wrote in message news:d5b9dl$2n39$1@digitaldaemon.com...
> This will create an arrray in the heap, not on the stack. (That's a main
> difference between C(++) and D)

No, it'll be on the stack just like in C/C++.


May 05, 2005
Maxime Larose wrote:
> This variant is good and generally useable. I didn't try it, because I
> didn't know it was possible to put the name of a variable after the mixin...
> It's not in the doc at any rate and not really intuitive (IMO). But if it
> works... ;)

It is in the docs... see ( http://www.digitalmars.com/d/mixin.html ) in two places.  Once at the beginning where the mixin grammar is given as:
> TemplateMixin:
>   mixin TemplateIdentifier ;
>   mixin TemplateIdentifier MixinIdentifier ;
>   mixin TemplateIdentifier !( TemplateArgumentList ) ;
>   mixin TemplateIdentifier !( TemplateArgumentList ) MixinIdentifier ;
>
> MixinIdentifier:
>   Identifier

And again fairly near the end, where it says "If a mixin has a MixinIdentifier, it can be used to disambiguate" and then gives an example.

I know its easy to miss some things in the docs... all kinds of hidden treasures in there if one gets out the proverbial fine-toothed comb.

-- Chris Sauls