Yet another "static" confusion

Steven Schveighoffer schveiguy at yahoo.com
Wed Feb 20 05:47:44 PST 2013


On Wed, 20 Feb 2013 03:03:49 -0500, Lubos Pintes <lubos.pintes at gmail.com>  
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.

What about a fixed sized array?  That won't even incur a heap allocation  
cost, and makes the function reentrant:

char[4096] buffer = void; // use = void to ensure the compiler doesn't  
initialize the buffer to all 0xff

-Steve


More information about the Digitalmars-d-learn mailing list