1,105,625 Community Members

run out of stack

Member Avatar
habib_parisa
Newbie Poster
16 posts since Mar 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
jephthah
Posting Maven
2,572 posts since Feb 2008
Reputation Points: 1,888 [?]
Q&As Helped to Solve: 196 [?]
Skill Endorsements: 5 [?]
 
0
 

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.

Member Avatar
nezachem
Practically a Posting Shark
896 posts since Dec 2009
Reputation Points: 616 [?]
Q&As Helped to Solve: 197 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
jephthah
Posting Maven
2,572 posts since Feb 2008
Reputation Points: 1,888 [?]
Q&As Helped to Solve: 196 [?]
Skill Endorsements: 5 [?]
 
1
 

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.


.

Member Avatar
habib_parisa
Newbie Poster
16 posts since Mar 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks I use stdlib.h

Member Avatar
habib_parisa
Newbie Poster
16 posts since Mar 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
jephthah
Posting Maven
2,572 posts since Feb 2008
Reputation Points: 1,888 [?]
Q&As Helped to Solve: 196 [?]
Skill Endorsements: 5 [?]
 
1
 

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.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: