stack overflow

Pages: 12
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.
I think you need allocate arrays on heap ( using operator new )
Your arrays are too large. Have you considered using vectors instead?
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
Could it mean you don't have enough RAM?
@ 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.
@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?
No and no. The heap doesn't take memory from the stack, and the stack has a size limit.
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
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 :)
ok thanks alot everyone. I will use new or vectors :)
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
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
yes I was wrong about that thank you filipe
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.
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
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
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?
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.
Why do you assume that implies hard coding?
Pages: 12