64 bit apps in visual studio 2005

Hi,

I was hoping someone could help me with a problem I've been having. I can't seem to get 64 bit apps in visual studio 2005 (standard, academic on win xp pro x64, 6GB RAM) to allocate more that 2GB of RAM. I've written a test app:

int main(){
float test[1024][1024][1024];
return 1;
}

and tried compiling it with the Visual Studio 2005 x64 Win64 Command Prompt:

but I get:

> cl test.cpp

Microsoft (R) C/C++ Optimizing Compiler Version 14.00.50727.42 for x64
Copyright (C) Microsoft Corporation. All rights reserved.

test.cpp
test.cpp(3) : error C2148: total size of array must not exceed 0x7fffffff bytes

Any Ideas??

Thanks.
[752 byte] By [njpackard] at [2007-11-20 11:46:32]
# 1 Re: 64 bit apps in visual studio 2005
You're allocating memory on the stack, which should be regarded as a fairly limited resource. Try the following instead:

int main(){
float * test = new float[1024*1024*1024];
delete [] test;
return 1;
}
Hermit at 2007-11-9 1:26:12 >
# 2 Re: 64 bit apps in visual studio 2005
Yeah, I've tried dynamically allocating the memory too, and I got a runtime error (just-in-time debugger reported an unknown error) Thanks.
njpackard at 2007-11-9 1:27:09 >
# 3 Re: 64 bit apps in visual studio 2005
You are trying to allocate roughly 4GB of RAM, assuming 4-byte float's. This won't really work very well. Consider using a mass-media storage method.

Sincerely, Chris.
dude_1967 at 2007-11-9 1:28:08 >
# 4 Re: 64 bit apps in visual studio 2005
The whole point of having 6GB of RAM and a 64-bit OS is so that I can allocate 4GB in an array. I'm trying to write some medical volume reconstruction code and it would be very nice to use that big of an array, and very inconvenient not to.
njpackard at 2007-11-9 1:29:08 >
# 5 Re: 64 bit apps in visual studio 2005
u are triying to aloocate 8 gigabyte
Mitsukai at 2007-11-9 1:30:17 >
# 6 Re: 64 bit apps in visual studio 2005
...which you can't do on the stack no matter how you try....

That MUST be done from dynamic allocation, but you must have the 8Gbytes to back it up (even if it's from VM), or there will be a runtime error (out of RAM).
JVene at 2007-11-9 1:31:14 >
# 7 Re: 64 bit apps in visual studio 2005
(1024^3)*4 = 4294967296 (4 Gb)
njpackard at 2007-11-9 1:32:18 >
# 8 Re: 64 bit apps in visual studio 2005
float 64 bit is 8 bytes?
Mitsukai at 2007-11-9 1:33:13 >
# 9 Re: 64 bit apps in visual studio 2005
I did a quick test to check:

#include <stdio.h>
int main(){
float* test;
test = new float[1024*1024*1024];

printf("SIZEOF(void*)=%d\n",sizeof(void*));
printf("SIZEOF(long long)=%d\n",sizeof(long long));
printf("SIZEOF(int)=%d\n",sizeof(int));
printf("SIZEOF(float)=%d\n",sizeof(float));

//for testing that we actually got the memory
for (long long i=0;i<1024*1024*1024;i++) test[i]=1;

return 1;
}

>test

SIZEOF(void*)=8
SIZEOF(long long)=8
SIZEOF(int)=4
SIZEOF(float)=4

if we comment out
//for (long long i=0;i<1024*1024*1024;i++) test[i]=1;
then we actually exit normally (which is something new)

but if it isn't commented out we get a runtime error:

Visual Studio Just-In-Time Debugger
An unhandled win32 exception occurred in test.exe [(some number that changes)]

Interestingly, the error is win32, does that mean that the app is not truely 64 bit? When we compile it claims to be 64 bit (as shown in my original post) and I tested the program on a 32-bit system and it doesn't work.
njpackard at 2007-11-9 1:34:21 >
# 10 Re: 64 bit apps in visual studio 2005
Thanks for your help.

I just tried it with 1024*1024*1023, and it works, I guess I was right at the limit of the hardware or something, anyway that should do for my purposes, although it would be nice to be able to allocate even more if its available.
njpackard at 2007-11-9 1:35:14 >
# 11 Re: 64 bit apps in visual studio 2005
first long long is not C++

second you overwrote the memory buffer. that is why it was crashing
Mitsukai at 2007-11-9 1:36:23 >
# 12 Re: 64 bit apps in visual studio 2005
The whole point of having 6GB of RAM and a 64-bit OS is so that I can allocate 4GB in an array.
NJ,

Whether it's 2 or 4 or 8 GB or whatever, this is really not the right line of thinking. Although you might be able to crack some kinds of memory allocation constraints with huge amounts of hardware, you must be more careful to design your software systems in a sensible fashon.

Some systems might actually have this much memory. However it is still not a wise decision to expect to use it all in one single program chunk. You must consider that there is an OS running and possibly hundreds of other processes. And, get real, the bulk of the OS writers and other contracters working on Vista tagged that RAM memory for their greedy applications long before the memory even sniffed its production in the silicon foundry. In addition, any sensible kind of VM manager will seriously hack such a large allocation into tiny tidbits.

Even in the age of over-hardwareing a problem, a sensible memory design is crucial for stabile programming.

You should try to keep your allocations down to a few tens of MB or possibly up to 500MB.

Sincerely, Chris.
dude_1967 at 2007-11-9 1:37:21 >