Thursday, February 08, 2007

Stack Overflow: How to make the stack bigger?

Hi, fellas!

It has been a long time since I have posted something really technical. So here I am again.

We have been asked to implement a quicksort-based selection algorithm and another one based on random sampling to compare them to each other in one of the lectures. But wait. I am not going to tell about these algorithms. What I want to say here is more general, more system related.

Ok, by looking at the algorithms, everyone can tell that the quicksort-based method, because of its simplicity, runs faster in a usual case when the target array size is small. So, in order to be able to say that "the random sampling based method performs better when the size of the data set is large", I have to make sure the sample size is large. And here the problem begins!!!

When you run a program, the operating system puts your program into the RAM. At the same time, it also allocates some spaces for the consumption by the program, in case it needs it. There are two types of allocated memory, stack and heap. I don't really remember but I think that the stack is used for function calls and variables, whereas the heap is used when you allocate memory dynamically by calling malloc().

Stack overflow occurs when your program needs more memory than allocated to it. In my case, I need a lot of space to make sure the data set is large enough to see the difference between the algorithms, so the default stack size allocated by MinGW gcc is not enough for me. I later found out also that stack size allocated to a program is handled by the operating system it is run on, so there is no sure way to turn around this problem. Luckily I came upon this file. It provides exactly what I wanted. I link to it here in case I will need it again some day, and maybe you would find it useful, too.

It is kind of late now so I have got to go.

Good night.

No comments: