www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to reserve memory for a slice in a struct

reply "Namal" <sotis22 mail.ru> writes:
Hello,

I am new to D. According to that power of 2 rule I want to
reserve 2 sized chunks to my array, But how do I do that in a
struct? Say:

struct Stack(int){

int a[];


}


Where do I put that a.reserve(2);

Because right after the declaration of a I get an error

Error: unexpected ( in declarator
Error: basic type expected, not 2
Error: found '2' when expecting ')'
Error: no identifier for declarator a.reserve(int)
Error: semicolon expected following function declaration
Error: Declaration expected, not ')'
May 07 2013
next sibling parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
On Tuesday, 7 May 2013 at 10:29:44 UTC, Namal wrote:
 Hello,

 I am new to D. According to that power of 2 rule I want to
 reserve 2 sized chunks to my array, But how do I do that in a
 struct? Say:

 struct Stack(int){

 int a[];


 }
"2 sized chunks"? Perhaps you want something like this: struct Stack{ int a[]; this(size_t r) { a.reserve(r); } } void main() { auto s = Stack(2); }
May 07 2013
parent reply "evilrat" <evilrat666 gmail.com> writes:
On Tuesday, 7 May 2013 at 10:58:42 UTC, John Colvin wrote:
...
 	int a[];
please don't use C style declarations, D style is "type followed by id": int[] a;
May 07 2013
parent "John Colvin" <john.loughran.colvin gmail.com> writes:
On Tuesday, 7 May 2013 at 11:49:07 UTC, evilrat wrote:
 On Tuesday, 7 May 2013 at 10:58:42 UTC, John Colvin wrote:
...
 	int a[];
please don't use C style declarations, D style is "type followed by id": int[] a;
woops, just copied it from the original post.
May 08 2013
prev sibling next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Tue, 07 May 2013 06:29:43 -0400, Namal <sotis22 mail.ru> wrote:

 Hello,

 I am new to D. According to that power of 2 rule I want to
 reserve 2 sized chunks to my array, But how do I do that in a
 struct? Say:

 struct Stack(int){

 int a[];


 }


 Where do I put that a.reserve(2);

 Because right after the declaration of a I get an error

 Error: unexpected ( in declarator
 Error: basic type expected, not 2
 Error: found '2' when expecting ')'
 Error: no identifier for declarator a.reserve(int)
 Error: semicolon expected following function declaration
 Error: Declaration expected, not ')'
You cannot statically reserve heap space for any type. You have two choices: 1. make 'a' a fixed sized array (this reserves space inside the struct): int[2] a; 2. reserve space at runtime (e.g. during constructor): this(size_t n) { a.reserve(n); } Unfortunately, structs have no default constructor, so you can't specify that a *always* is reserved. A note of caution -- reserving space via a.reserve does *not* give you access to the space, it just reserves a block for your use to append the slice into the array. So for example: a.reserve(2); assert(a.length == 0); // length is still 0. assert(a.ptr !is null); // but block has been allocated with at least 2 elements. a ~= 1; // pointer has not changed, a is now filling into allocated block. If you want to reserve accessible space, set the length: a.length = 2; One other thing, the space you reserve is not exactly a power of 2. It will be 2^^n - 1. This is due to requirements of the array runtime. The minimal heap space for an int array is 3. Then it goes 7, 15, 31, etc. Once you get to page size, the amount of space you can reserve grows linearly. These are implementation details of the GC, so you shouldn't depend on this never changing. In any case, whatever space you request, the GC will give you at LEAST that much. -Steve
May 07 2013
prev sibling next sibling parent "Maxim Fomin" <maxim maxim-fomin.ru> writes:
On Tuesday, 7 May 2013 at 10:29:44 UTC, Namal wrote:
 Hello,

 I am new to D. According to that power of 2 rule I want to
 reserve 2 sized chunks to my array, But how do I do that in a
 struct?
You can set some kind of statically allocated array size: struct S { int[] a; this(size_t size) { a.length = size; } } enum SE : S { A = S(2) } static assert(SE.init.a.length is 2); void main() { SE se; assert(se.a.length is 2); } Unfortunately this does not work with array reserve.
May 07 2013
prev sibling parent "Diggory" <diggsey googlemail.com> writes:
You could allocate space inside a class itself with something 
like this:
class Base {
     int[] slice;
}

template Derived(size_t N) {
     class Derived : Base {
         int[N] array;

         this() {
             slice = array;
         }
     }
}

Base b = new Derived!32();

A bit pointless though...
May 07 2013