Alocating memory depending of a variable value INT variable

Carlos checoimg at gmail.com
Tue Nov 19 17:29:32 PST 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!


More information about the Digitalmars-d-learn mailing list