stack overflow

Pages: 12
Mar 10, 2011 at 5:19pm
hi,

I'm having a stack overflow problem in my code. I am declaring a few arrays of size 10000 and get a stack overflow error but when I reduce the size to 5000 there is no problem. I understand that getting a stack overflow means either I have too many recursions or the program has exceeded its limit. There are 1000s of lines of code and I don't know how to locate the problem efficiently so I wanted to make sure that my arrays aren't too large. Does anyone know how much memory the program can take up? I have like 50 arrays or so with size 10000.
Mar 10, 2011 at 5:32pm
I think you need allocate arrays on heap ( using operator new )
Mar 10, 2011 at 5:41pm
Your arrays are too large. Have you considered using vectors instead?
Mar 10, 2011 at 5:44pm
ahh, vectors are the solution then I'm screwed I'll have to rework the whole program.
So if I have 50 arrays, should I use 50 vectors?
Last edited on Mar 10, 2011 at 5:45pm
Mar 10, 2011 at 5:45pm
Could it mean you don't have enough RAM?
Mar 10, 2011 at 5:48pm
@ Johnnyboy: Nope, if you run out of RAM the OS caches stuff to disk into what is called the pagefile; and it does this without telling you or your process. A Stack Overflow is usually exactly what it sounds like, the size of something went over the boundry of the Stack.
Mar 10, 2011 at 5:54pm
@null

but doesn't the heap take it from the stack anyway? like if there isn't enough memory overall taking memory and reserving it wouldn't create more memory right?
Mar 10, 2011 at 6:03pm
No and no. The heap doesn't take memory from the stack, and the stack has a size limit.
Mar 10, 2011 at 6:18pm
ok so what null suggested should solve my problem right? :)
or isn't there something I can do to increase stack size?
Last edited on Mar 10, 2011 at 6:19pm
Mar 10, 2011 at 6:20pm
Yes, both Null or PanGalactic gave valid solutions. Also remember when faced with 1000+ lines of code Cntrl+F is one of our best friends :)
Mar 10, 2011 at 6:45pm
ok thanks alot everyone. I will use new or vectors :)
Mar 10, 2011 at 7:10pm
I'm thinking of using dynamic memory allocation since all I need to do is
1
2
double* rho = new double [100000];
delete [] rho;


and the rest of the code can remain the same.

whereas if I use vectors then I have to traverse the entire code and add in the .at() function to use the vectors elements.

But I guess if I were to start a new program its better to use vectors? since you don't have to worry about deallocation and such?

am I correct about this?
Last edited on Mar 10, 2011 at 7:11pm
Mar 10, 2011 at 7:12pm
whereas if I use vectors then I have to traverse the entire code and add in the .at() function to use the vectors elements.

No, you don't. You can access a vector's elements through subscript notation just like you do with arrays. The at() mehod only provides range checking.
Last edited on Mar 10, 2011 at 7:13pm
Mar 10, 2011 at 7:26pm
yes I was wrong about that thank you filipe
Mar 10, 2011 at 7:28pm
When you use ".push_back" with a vector you are using a type of dynamically allocated memory. A "vector" is just a class with tools built up around it to make working with dynamically sized array's MUCH easier. Also you shouldn't be addressing ANY kind of array, dynamic or static, by iterating through it's contents with hard code, that's just weak programming.
Mar 10, 2011 at 7:30pm
Is it possible you were thinking about std::lists?

I'd suggest using vectors over dynamically-allocated arrays simply because they're so easy to use (resize, in particular). :)

-Albatross
Mar 10, 2011 at 8:26pm
ok vectors it is then :)

"by iterating through it's contents with hard code, that's just weak programming."

Yeah that's not really my style, I didn't want to be bugged for information, I mean to clean it up once I get the simulation working properly for set values.
Last edited on Mar 10, 2011 at 8:27pm
Mar 10, 2011 at 8:35pm
Also you shouldn't be addressing ANY kind of array, dynamic or static, by iterating through it's contents with hard code, that's just weak programming.

Did I miss something? Where does this come from?
Mar 10, 2011 at 8:38pm
I assumed that is what he was using "at()" for, since it's the same as the "[]" operator except it has a built in bounds check.
Mar 10, 2011 at 8:51pm
Why do you assume that implies hard coding?
Pages: 12