Thread overview
Alocating memory depending of a variable value INT variable
Nov 19, 2013
Carlos
Nov 19, 2013
Ali Çehreli
Nov 20, 2013
bearophile
Nov 20, 2013
Namespace
Nov 20, 2013
Carlos
November 19, 2013
Well in C I just declared an array during execution with an array with a multiplied variable for the size of the array.

Since I only need two spaces in the array for each line of process it was multiplied by two.

so it was like this :

scanf("%d", &Num);
int array[Num*2];

When I tried to do this on D, well it says the variable cannot be read at compile time.

I don't know how the C language implements this but the main thing is that it does.

So how can I manage memory depending on the user's input in D ?


Thank you for your attention.
November 19, 2013
On 11/19/2013 03:16 PM, Carlos wrote:> Well in C I just declared an array during execution with an array with a
> multiplied variable for the size of the array.
>
> Since I only need two spaces in the array for each line of process it
> was multiplied by two.
>
> so it was like this :
>
> scanf("%d", &Num);
> int array[Num*2];

That is a VLA.

In D, you would normally use a slice. Below, 'a' and 'b' are two ways of having a slice with valid elements. On the other hand, 'c' is merely reserving space for that many elements:

import std.stdio;

void main()
{
    size_t num;
    write("How many? ");
    readf(" %s", &num);

    auto a = new int[num * 2];

    int[] b;
    b.length = num * 2;

    int[] c;
    c.reserve(num * 2);
}

Ali

November 20, 2013
Ali Çehreli:

> That is a VLA.

That are currently not present in D. The most common and safe alternatives in D are allocating the memory on the heap with 'new', or over-allocating on the stack a fixed size and then slicing.


If the OP really wants to allocate on the stack, there is the alloca() function (untested, I don't remember the correct name for the memory error):


auto ptr = cast(int*)alloca(num * 2 * int.sizeof);
if (ptr == null)
    throw new outOfMemoryError("...");
auto array = ptr[0 .. num * 2];


Bye,
bearophile
November 20, 2013
On Tuesday, 19 November 2013 at 23:34:48 UTC, Ali Çehreli wrote:
> On 11/19/2013 03:16 PM, Carlos wrote:> Well in C I just declared an array during execution with an array with a
> > multiplied variable for the size of the array.
> >
> > Since I only need two spaces in the array for each line of
> process it
> > was multiplied by two.
> >
> > so it was like this :
> >
> > scanf("%d", &Num);
> > int array[Num*2];
>
> That is a VLA.
>
> In D, you would normally use a slice. Below, 'a' and 'b' are two ways of having a slice with valid elements. On the other hand, 'c' is merely reserving space for that many elements:
>
> import std.stdio;
>
> void main()
> {
>     size_t num;
>     write("How many? ");
>     readf(" %s", &num);
>
>     auto a = new int[num * 2];
>
>     int[] b;
>     b.length = num * 2;
>
>     int[] c;
>     c.reserve(num * 2);
> }
>
> Ali


auto array = new int[Num * 2];

Well that works just perfect. Thanks a lot!
November 20, 2013
On Wednesday, 20 November 2013 at 00:02:42 UTC, bearophile wrote:
> Ali Çehreli:
>
>> That is a VLA.
>
> That are currently not present in D. The most common and safe alternatives in D are allocating the memory on the heap with 'new', or over-allocating on the stack a fixed size and then slicing.

That's why I use a Stack and a Heap struct in combination with an Array struct:
Heap heap;
Stack stack;
byte[] arr = Array!byte(&stack, &heap).of(Num);
or even more naturally:
byte[] arr = Array!byte(&stack, &heap)[Num];

Stack has a buffer of e.g. 4096 and tries to allocate there, if it fails, it returns null. If this happens, the Heap struct allocates on the GC or C heap.
And if the Stack / Heap struct gets destroyed, the stored memory is freed.

But VLA's were really desirable. :(