I know STL vector works. But in case STL is not available,
what can one do to allocate large memory size in c++ through
operator "new"? For instance, I am writing a sort algorithm, and
here's part of the code:
const int range = 1000000;
int *array = new int(range);
if (array == 0) cout<<"null ptr returned"<<endl;
for (int i=0; i<range; i++)
*(array+i) = rand()%(10*range);
If 'range' is around 10000, the above code works.
But if it is large, then segmentation fault occurs after i=33790.
It happens every time after i reaches 33790. Or, if the array is double,
then seg fault will happen right after i=33790/2 = 16895.
What is wrong in the code?
it seems to me that the heap size available to my program is not
big enough. I used ulimit -a to check and it shows 10240 for stack size.
One more doubt is ulimit -s gives 10240 (man ulimit says in bytes)
but ulimit -a says kbytes. Which one is right?
I've also tried using array:
int array[1000000];
the code works for this particular size, but with the size increasing to
about 3M, seg fault happened too.
In general, how to deal with the need of using large chunk of memory in
c++ without using STL?