I have to sort a large array of numbers wherein the numbers are random and range from 1 to 9,99,999...till the time the length of array is less than 2,00,000, all sorts work fine (i tried selection, insertion and quick sort)...but when the length of array is increased to 3,00,000 , the console crashes...what actually caused this problem...?...I don't want to use merge sort algorithm...is there any way I can sort the array keeping it single dimension...I'm sorry if this has been asked before....Thanx

Here's my system specs :
2 GB RAM
Intel T4300 @2.1 Ghz
32 bit win 7 OS

That depends on how you defined/allocated your array. Most likely you defined it as an automatic variable in main(or any other function), which gets allocated in the stack, which is a limited resource. It's called a stack overflow.

int main(){
 int thearray[SIZE];

 sort(thearray);
 
 return 0;
}

What you could do is to either define this array in global scope, which gets allocated on a different memory space, or allocate an array via calloc() function which goes to the heap which is very much plentiful.

#include <stdlib.h>

int first_array[SIZE];

int main(){
 int *second_array = calloc(SIZE, sizeof(int));

 return 0;
}

Edited 4 Years Ago by asrockw7: n/a

That depends on how you defined/allocated your array. Most likely you defined it as an automatic variable in main(or any other function), which gets allocated in the stack, which is a limited resource. It's called a stack overflow.

ya it did the job...thanx...as far as my knowledge is concerned, a program has three types of memories :
main memory - This is where the program itself resides including the global variables.
stack memory - this is where the functions and local variables reside
heap memory - this is where the dyn mem alloc. happens
am i right at that....?...in general how large are each of these(consider my system here)...thx...

Stack is system defined as far as I know. It depends on what OS you are on and does not expand the same way your program heap does. If you're short on stack, then you're finished. Though you could expand it using some compiler options. Same way with the heap in that the OS defines an initial size for both, however the heap can be expanded by the OS.

As for their exact sizes, I don't know. As a rule of thumb though, be cautious with using stack as running out is catastrophic. If you would do the math though, if int is 4 byte and the threshold is at 2 million, you get 8 million bytes or 8,000 kb or 8mb. Mine in gcc is 4mb. As for the heap, well I would say just look at your RAM.(Not necessarily true as you are not allocated the whole pool at once, but since it can expand...)

This article has been dead for over six months. Start a new discussion instead.