Carter
Carter

Reputation: 143

Segmentation Fault - Large Array

I am having an odd error when I try to compile my code, which is written in C. The error says

 segmentation fault (core dumped)

In my code, I have a lot of really large double arrays (like of sizes close to 100,000 in length). I initialize one array of doubles and when I try to initialize an array immediately afterwards of the same size (roughly a 100,000 length) it gives me the segmentation fault error. Oddly, it depends on the size of the array. For example if I do

 double arr[70000];       

It gives me the segmentation error but

 double arr[60000];     

does not gives me the error. I am running my code on a linux machine if that helps. I really need many different very large double arrays. What is going on?

Upvotes: 0

Views: 1068

Answers (1)

spencer7593
spencer7593

Reputation: 108370

You've encountered a "Stack Overflow"; basically, you've exhausted the stack space available to your program.

If you allocate the arrays on the heap (in heap storage), you probably will be okay.

With C, you'd likely use the malloc instruction to allocate the memory.

And of course, you'll remember to use the free instruction to return the memory when you're done with it.

Upvotes: 3

Related Questions