Macro is working in version 5.34 but not 6.06 version

I have macro called energy_estimation.C which is given below,
void energy_estimation()
{
const int n=281877;
double part[n] ,energy[n],coredist[n] ,theta[n];
cout<< “hi”<< endl;
}

It is not working in root version 6.06 (crashing), however it works in version 5.34 of root and gives the output ‘hi’.
Interestingly,the macro works in 6.06 if I remove the array theta[n] :blush:

Don’t use variable length arrays (VLAs). It’s a bad idea!
You are allocating 281877 * 4 variables * sizeof(double) = 9,020,064, i.e. 9 MB of memory. The variables will be allocated on the stack - and that will most likely overflow. To me, the bug is in your code.

Use std::vector instead. Or use unique_ptr.

auto part = vector<double>(n);
// or if you dont want a vector:
auto part = unique_ptr<double[]>(new double[n]);
//or, as soon as you can use C++14:
auto part = make_unique<double[]>(n);

Hi,

You allocate to much memory on the stack, which is limited. Try to do the same on the heap:

[code]void energy_estimation()
{
const int n=281877;
double *part = new double[n];
double *energy = new double[n];
double *coredist = new double[n];
double *theta = new double[n];

cout << “hi” << endl;

delete [] part;
delete [] energy;
delete [] coredist;
delete [] theta;
}
[/code]
Cheers, Bertrand.

[quote=“bellenot”]Hi,

You allocate to much memory on the stack, which is limited. Try to do the same on the heap:

[code]void energy_estimation()
{
const int n=281877;
double *part = new double[n];
double *energy = new double[n];
double *coredist = new double[n];
double *theta = new double[n];

cout << “hi” << endl;

delete [] part;
delete [] energy;
delete [] coredist;
delete [] theta;
}
[/code]
Cheers, Bertrand.[/quote]

Thank you, bellenot for help. Now its fine :smiley: