Segmentation fault (core dumped)

I defined the class
1
2
3
4
5
6
7
class sample
{
public:
    static const unsigned int d = 5;
    static const unsigned long int M = 10000;
    double val[d][M];
};

In main, I created an array for object of type sample.
1
2
3
4
int main(){
    const int N = 10;
    sample bm_sample[N];
}

The compile ($ g++ main.cpp free_bndry.cpp) goes through, but when I execute ./a.exe it gives me Segmentation fault (core dumped).
When I set const int N = 1; instead of 10, Segmentation fault (core dumped) goes away.
My machine has 8G of memory but windows is using 70% of it.

What is the issue?
How much memory do I need to make M 1 million, and N 40?
Last edited on
Since you are putting that on the stack, it might be too large.

I would first try to allocate bm_sample with the new operator to put it on the heap.

1
2
3
4
5
int main()
{
    const int N = 10;
    sample * bm_sample = new sample[10]();
}
Your bm_sample array will take almost 2 MB on stack. Default stack size on MSVC and gcc from MinGW is 1 MB: stack owerflow here.
You can:
1) Increase stack size (not recommended)
2) Create objects in memory like bradw proposed (recommended)
3) Learn why the heck Windows devour 6 GB of memory alone.
And I just noticed that I didn't even use 'N' in my source. :)

Thanks for the detail on the size of the stack, MiiNiPaa!



Thank you bardw and MiiNiPaa.
The program I am writing needs much larger arrays, let's say an array of 5*1,000,000*40. The challenge here is that I can not delete them to free the memory. To start the program I have to keep all variables. But, later on I can delete them little by little. The time also matters so I can not write them in a file and call them according to my memory capacity.
I would be able to free the memory from windows occupation to 6GB. Is there any chance I can use at lease 4 GB of it if I follow bradw's suggestion?
An array of 200,000,000 of floats is going to be 800MB. An array of ten of those would take 8GB of memory.
Thank you for all your help, guys.
To conclude:
I terminated as much application as possible from windows to free 6GB of memory. Then I changed declaration inside my class to
1
2
3
4
5
6
7
8
9
class sample
{
public:
    static const unsigned int d = 5;
    static const unsigned long int M = 10000000;
    double** val = new double* [d];
	for (int i = 0; i<d; ++i)
	double* val[i] = new double [M];
};

and my program to
1
2
3
4
5
6
int main(){
   static const unsigned int d = 5;
    static const unsigned long int M = 100000;
    const int N = 10;
    sample* bm_sample = new sample[N];
}

It even works with M 10 times as before.
Topic archived. No new replies allowed.