Dear all,

I am writting a code for high dimensional chebyshev interpolation. Since it is high dimensional, I MUST use extremely large arrays.
Something like this:

#define DIM         (int)2
#define TOTAL_STEPS (int)30

int main(){

double        *a_data;

long long     a_dim;                      /** length of matrix a_data **/

int                 n;                          /** # of given points **/
int                 grid_length;
  
grid_length =  TOTAL_STEPS;

n           =  pow( grid_length, DIM);  

/** Length of array is here **/  
  
a_dim = n * pow(n + 1, DIM);          /** which is  730620900 **/

/** Allocate memory **/

a_data = (double*)     malloc( a_dim * sizeof(double));
if(NULL == a_data)    printf("no memory");

return 0;
}

length of a_data is a_dim = 730620900, and the allocation does not return NULL.
But I run out of the stack and I get segmentation fault.
The first solution which comes in my mind is to increase the stack?
Do you know how to increase that?
Or maybe you have a better solution.

Got really stock, please help.

maybe i'm missing some details, but it seems to me that you're trying to allocate > 5GB of ram, above and beyond whatever your operating system and its various running processes require. it looks like you dont have the RAM, so your computer faults.

First of all, malloc does not take up a stack. The memory comes from the completely different area.
Second, you have to realize that in modern OSes malloc only reserves a range of virtual addresses. A successful malloc does not guarantee that there would be enough physical memory by the time this range is accessed.
Third, you really allocate a lot; if I am calculating right, it is close 6Gb. Does your system have that much?
My only recommendation is to modify your algorithm; do not assume that there's room for everything, and keep most of the data off-RAM. Maybe swap will let you do it transparently.

One other potential problem: If you're certain you have the physical memory, check to see if you forgot to

#include <stdlib.h>

?

if so, then the malloc doesnt have a prototype and thinks that it's returning a pointer to INT.

also you should not cast the return of malloc. malloc returns a void pointer and so the cast is redundant. to cast the return is bad practice and doing so can mask the cause of these sorts of malloc problems.


.

Thanks I use stdlib.h

Could you please explain a bit where the memory comes from for malloc?
How can I know that I have 6GB memory? Is the hard disk memory?
Sorry if it is stupid.

malloc memory is typically called the "heap". We can just say that the "heap" is in your RAM. Not a stupid question: some people will argue for days about what exactly constitutes the heap, but I'm not one of them (i argue for days about other things)

So the 6GB memory we're talking about, the RAM, is contained on cards inserted into slots on the motherboard. it is *not* the hard drive.

If you are working on your own PC with Windows OS, you can find the amount of RAM by going into MyComputer and right-click Properties.

But if you don't already know how much RAM you have, then you don't have 6GB of RAM. Because that's a lot. not a ridiculous amount, but enough that you'd know what you were buying when you paid extra for a high-end computer with extra memory

and this 6GB, this is above and beyond what is required to run your OS and normal background processes. if you have Windows Vista, for instance, you would need 2GB more on top of that, for a total of 8GB.

so yes, the answer as someone else mentioned, is that you need to come up with a more memory-efficient algorithm.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.