digitalmars.D.learn - static array crashes my program
- ref2401 (13/13) Dec 05 2015 I want to create a static array large enough to store 1MB of
- Olivier Pisano (6/19) Dec 05 2015 I suppose you overflow the stack.
- John Colvin (19/32) Dec 05 2015 The default stack size is probably 1MB, which means your 1MB
- Steven Schveighoffer (7/40) Dec 05 2015 T[N]* heapStaticArray(T, size_t N)()
I want to create a static array large enough to store 1MB of float values. What am I doing wrong? Here is a sample code with notes: void main(string[] args) { enum size_t COUNT = 1024 * 512 / float.sizeof; // works OK :) //enum size_t COUNT = 1024 * 512 * 2 / float.sizeof; // constantly crashes :( float[COUNT] arr; writeln(arr.length); } DMD: 2069.2 OS: Win 8.1 Pro
Dec 05 2015
On Saturday, 5 December 2015 at 09:49:06 UTC, ref2401 wrote:I want to create a static array large enough to store 1MB of float values. What am I doing wrong? Here is a sample code with notes: void main(string[] args) { enum size_t COUNT = 1024 * 512 / float.sizeof; // works OK :) //enum size_t COUNT = 1024 * 512 * 2 / float.sizeof; // constantly crashes :( float[COUNT] arr; writeln(arr.length); } DMD: 2069.2 OS: Win 8.1 ProI suppose you overflow the stack. I am not a Windows dev, but I suppose there is a linker option to increase the stack size. Or you can try to use a global __gshared variable or allocate your array on the heap.
Dec 05 2015
On Saturday, 5 December 2015 at 09:49:06 UTC, ref2401 wrote:I want to create a static array large enough to store 1MB of float values. What am I doing wrong? Here is a sample code with notes: void main(string[] args) { enum size_t COUNT = 1024 * 512 / float.sizeof; // works OK :) //enum size_t COUNT = 1024 * 512 * 2 / float.sizeof; // constantly crashes :( float[COUNT] arr; writeln(arr.length); } DMD: 2069.2 OS: Win 8.1 ProThe default stack size is probably 1MB, which means your 1MB array plus a few local variables is too much. Arrays that large should be allocated on the heap in most circumstances. Watch out for this: static assert(is(typeof(new float[3]) == float[])); because `new T[n]` is a special case in the grammar. If you really must have a static array on the heap (as opposed to a dynamic array / slice T[]), you can use something like this, but i wouldn't recommend it: T[N]* heapStaticArray(T, size_t N)() { return cast(T[N]*)((new T[N]).ptr); } void main() { int[4]* a = heapStaticArray!(int, 4)(); (*a)[] = 3; }
Dec 05 2015
On 12/5/15 8:09 AM, John Colvin wrote:On Saturday, 5 December 2015 at 09:49:06 UTC, ref2401 wrote:T[N]* heapStaticArray(T, size_t N)() { auto arr = new T[N][1]; return &arr[0]; } -SteveI want to create a static array large enough to store 1MB of float values. What am I doing wrong? Here is a sample code with notes: void main(string[] args) { enum size_t COUNT = 1024 * 512 / float.sizeof; // works OK :) //enum size_t COUNT = 1024 * 512 * 2 / float.sizeof; // constantly crashes :( float[COUNT] arr; writeln(arr.length); } DMD: 2069.2 OS: Win 8.1 ProThe default stack size is probably 1MB, which means your 1MB array plus a few local variables is too much. Arrays that large should be allocated on the heap in most circumstances. Watch out for this: static assert(is(typeof(new float[3]) == float[])); because `new T[n]` is a special case in the grammar. If you really must have a static array on the heap (as opposed to a dynamic array / slice T[]), you can use something like this, but i wouldn't recommend it: T[N]* heapStaticArray(T, size_t N)() { return cast(T[N]*)((new T[N]).ptr); } void main() { int[4]* a = heapStaticArray!(int, 4)(); (*a)[] = 3; }
Dec 05 2015