how big array size does a visual c++ program allow ?
-
I have a program which has a total of array size of 94000 float(31 arrays). When I change 9 of the arrays (73737 size) into double, my program seems not working. Is the total array size too big ? How to fix the problem ? Thanks for any help.
Typically, when you need an array of more than a few hundred elements, it is best to allocate the array on the heap. This allows you to create arrays bounded only by memory (or 2GB if on w32). onwards and upwards...
-
Typically, when you need an array of more than a few hundred elements, it is best to allocate the array on the heap. This allows you to create arrays bounded only by memory (or 2GB if on w32). onwards and upwards...
-
Thanks for the advice. You mean I should using pointers ? I am a programer from Fortran/Java. I have not got used to pointers. Give me detailed or further advice please.
For the array, in order to use the heap memory, you can allocate your array like this
float* floatArray = NULL; floatArray = new float[100];
and when you finish using your array, you candelete [] floatArray; floatArray = NULL;
I wish it help! Nacho -
Typically, when you need an array of more than a few hundred elements, it is best to allocate the array on the heap. This allows you to create arrays bounded only by memory (or 2GB if on w32). onwards and upwards...
Thanks for the advice. You mean I should using pointers ? I am a programer from Fortran/Java. I have not got used to pointers. Give me detailed or further advice please. I particularly use six big arrays a[8200],b[8200],c[8200] Ideally I need to expand my program to a[16400],b[16400],c[16400]. However, I tried these expansion, the program stopped to work. message says: memery problem. -- modified at 15:27 Monday 24th April, 2006
-
Thanks for the advice. You mean I should using pointers ? I am a programer from Fortran/Java. I have not got used to pointers. Give me detailed or further advice please. I particularly use six big arrays a[8200],b[8200],c[8200] Ideally I need to expand my program to a[16400],b[16400],c[16400]. However, I tried these expansion, the program stopped to work. message says: memery problem. -- modified at 15:27 Monday 24th April, 2006
using pointers will not fix all your problems but will help you deal with memory problems. normally, when allocating memory on the heap ( with
new
) you will have a immediate result telling you that the allocation worked or not.
Maximilien Lincourt Your Head A Splode - Strong Bad
-
using pointers will not fix all your problems but will help you deal with memory problems. normally, when allocating memory on the heap ( with
new
) you will have a immediate result telling you that the allocation worked or not.
Maximilien Lincourt Your Head A Splode - Strong Bad
Thanks I need to expand the size of the arrays. How can I do this ? Ideally I should also use "double" instead of "float" to improve the accuracy of the calculations. However, I tried I can not do it now due to the memery limitations. Please help me to get a way to deal with this problem Thanks
-
For the array, in order to use the heap memory, you can allocate your array like this
float* floatArray = NULL; floatArray = new float[100];
and when you finish using your array, you candelete [] floatArray; floatArray = NULL;
I wish it help! Nacho -
I have a program which has a total of array size of 94000 float(31 arrays). When I change 9 of the arrays (73737 size) into double, my program seems not working. Is the total array size too big ? How to fix the problem ? Thanks for any help.
[Message Deleted]
-
[Message Deleted]
-
[Message Deleted]
-
mrby123 wrote:
would you please give me some code example segments for using GlobalAlloc()?
There's no reason to use
GlocalAlloc()
, unless you are maintaining old 16-bit code. Usenew
anddelete
instead.
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"We will be known forever by the tracks we leave." - Native American Proverb
-
I have a program which has a total of array size of 94000 float(31 arrays). When I change 9 of the arrays (73737 size) into double, my program seems not working. Is the total array size too big ? How to fix the problem ? Thanks for any help.
Using
new
anddelete []
orGlobalAlloc
will work but it is operating at an unnecessarily low level. Standard C++ has a container called avector
which will suit your needs and has many advantages including the fact that it automatically frees memory saving you from the burden of remembering to calldelete []
. To use it first include its header file like this:#include <vector>
Now you can whip up a vector offloat
s like this:std::vector<float> YourVectorOfFloats
Now you can use this as before but you don't need to calldelete []
and it has lots of member functions that you can use. For example, you want to expand it? No worries:YourVectorOfFloats.resize(400);
Unless he has highly specialized needs there is no good reason for a C++ programmer to usenew
anddelete []
or Win32 APIs such asGlobalAlloc
to make dynamic arrays - In general it is a mistake to do so. Steve -
[Message Deleted]
I would strongly sugest you not to use GlobalAlloc(). Why limit your code to Windows if you don't really need to? Standard ANSI should be fine for your problem. I assume, of course, that you are programming for Win32. If this is not the case then my comments don't apply. But if you avoid GlobalAlloc() you can easilly port to LINUX, which is very similar to Win32 but where GlobalAlloc() doesn't exist. The first thing you need to do is estimate how much memory you are demanding from the system. From your figures I assume the largest is 3 arrays of doubles with 16400 entries each. Since each double is 8 bytes on most platforms, this results in a total of 393600 bytes of RAM. That's peanuts by today's standards... I assume your computer is equipped with 16MB of RAM or more, so memory outage is surelly not the problem. I have 512 MB, and I once needed 1.3 GB for a simulation, and the virtual memory manager tolerated it just fine. So I'm sure memory outage is not a problem. There are no real limits on array sizes except those you declare on the stack. Stack is the only real limited resource. It is the space where you declare local variables, and the space the compiler uses to remember where to return to after a function call and where it places the parameters that you pass to functions. If you are not used to pointers then I would suggest you use a little class to wrap the pointer code. I will paste an example below. This will hide the complexities of allocation and will tolerate bad pointer coding practices. template class Array { protected: T* itsBuffer; long itsBufferCount; void Crash(void) { char* p=0; *p=0; } public: Array(long count) { itsBuffer=new T[count]; itsBufferCount=count; if (!itsBuffer) Crash(); } Array(Array& arr); // dummy ~Array() { if (itsBuffer) delete [] itsBuffer; } long GetCount(void) { return itsBufferCount; } T* Get(long index_0_based=0) { if (index_0_based<0) Crash(); if (index_0_based>=itsBufferCount) Crash(); return itsBuffer+index_0_based; } T& operator[](long index_0_based) { return *Get(index_0_based); } }; long main_2(Array arr) { return arr.GetCount(); } long main_3(Array& arr) { return arr.GetCount(); } void main(void) { long i; long arr_size=16400; Array arr_a(arr_size); Array arr_b(arr_size); Array arr_c(arr_size); #ifdef _DEBUG // slow protected implementation to detect software errors Array& a
-
Using
new
anddelete []
orGlobalAlloc
will work but it is operating at an unnecessarily low level. Standard C++ has a container called avector
which will suit your needs and has many advantages including the fact that it automatically frees memory saving you from the burden of remembering to calldelete []
. To use it first include its header file like this:#include <vector>
Now you can whip up a vector offloat
s like this:std::vector<float> YourVectorOfFloats
Now you can use this as before but you don't need to calldelete []
and it has lots of member functions that you can use. For example, you want to expand it? No worries:YourVectorOfFloats.resize(400);
Unless he has highly specialized needs there is no good reason for a C++ programmer to usenew
anddelete []
or Win32 APIs such asGlobalAlloc
to make dynamic arrays - In general it is a mistake to do so. Steve