digitalmars.D.learn - Alocating memory depending of a variable value INT variable
- Carlos (13/13) Nov 19 2013 Well in C I just declared an array during execution with an array
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (19/25) Nov 19 2013 That is a VLA.
- bearophile (14/15) Nov 19 2013 That are currently not present in D. The most common and safe
- Namespace (14/20) Nov 20 2013 That's why I use a Stack and a Heap struct in combination with an
- Carlos (3/32) Nov 19 2013 auto array = new int[Num * 2];
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.
Nov 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 amultiplied 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
Nov 19 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
Nov 19 2013
On Wednesday, 20 November 2013 at 00:02:42 UTC, bearophile wrote:Ali Çehreli: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. :(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.
Nov 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 aauto array = new int[Num * 2]; Well that works just perfect. Thanks a lot!multiplied variable for the size of the array. Since I only need two spaces in the array for each line ofprocess itwas 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
Nov 19 2013