views:

9

answers:

1

Hello, My original code (following) gives a seg fault at the string array assignment at about num_atoms=150,000:

int num_atoms=dimension[0]*dimension[1]*dimension[2]*prim_lat.size();
double superlat[num_atoms][3];
string current_occ[num_atoms];

Thinking this was a first instance of me hitting a stack overflow issue, and thinking you can assign to the heap using a dynamic allocation, I tried:

int num_atoms=dimension[0]*dimension[1]*dimension[2]*prim_lat.size();
double superlat[num_atoms][3];
string *current_occ = new string[num_atoms];

This code gave a seg fault at about num_atoms=350,000. I'm not sure the workaround for this issue. Is this just a case where I need to increase the stack/heap limit? If so, can I do this as a gcc option?

Thank you, Paul

A: 

You're still storing superlat on the stack in your second example. You could move it over to the heap as well.

Chances are this is what's causing the problem, since in the 1st example, you're soring num_atoms x2 pointers on the stack (one pointer per string, one pointer per double-array) and in the second you're storing num_atoms x1 pointers on the stack (only one per double-array), so the number of atoms before segfault doubled.

Amber
That was it. Sweet! Thanks for catching my blunder.
Paul