Yet another "static" confusion

Lubos Pintes lubos.pintes at gmail.com
Wed Feb 20 03:15:33 PST 2013


Ok thank you. I see now.
One unrelated question: Why the safe attribute has the at-sign, while 
nothrow doesn't?
Dňa 20. 2. 2013 11:19 monarch_dodra  wrote / napísal(a):
> On Wednesday, 20 February 2013 at 08:03:48 UTC, Lubos Pintes wrote:
>> Hi,
>> I want to allocate a buffer which I use in a function which reads data
>> from socket.
>> So I did as a first line in that function:
>> static char[] buffer=new char[4096];
>>
>> The compiler (2.062) complained that it cannot evaluate new char[] at
>> compile time.
>> I Then tried to move the declaration before function, the same thing
>> happened. Allocating statically sized array bloats the executable.
>> My idea is to return only a slice of array if less than 4K data was
>> read and prevent new allocation on every read.
>>
>> So what I am doing wrong or is this not possible?
>> Thank.
>
> In D (and unlike C++), anything static MUST have an initial state that
> is statically evaluable. "new char[4096]" is a run-time call, so it
> cannot be done.
>
> The truth is that this actually isn't much different from C++, which
> hides an invisible "is_initialized" bool somewhere to make it work.
>
> You can try to run-time initialize your buffer the module constructor,
> for example. Or just create an accessor to get an initialized buffer:
>
> void getBuffer() @safe nothrow
> {
>      static char[] buffer;
>      if (buffer.empty)
>          buffer = new char[4096];
>      return buffer;
> }



More information about the Digitalmars-d-learn mailing list