Alocating memory depending of a variable value INT variable
Ali Çehreli
acehreli at yahoo.com
Tue Nov 19 15:34:47 PST 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
More information about the Digitalmars-d-learn
mailing list