dynamic mem allocation, how to change heap size?
In my program, I was trying to allocate a large chunk
memory and then assign values to the array as: int *array = new(MAX); for (....) *(array+i) = some value; but segmentation fault occurred and I figured out that it was because heap size was not enough. It always exit when i = 33790 or if the array is double, then i=33790/2 = 16895. So, I want to know if there is a way to increase the system heap size while compiling the program in gcc. And I guess probably by using vector can avoid the problem. But I am curious to know if indeed vector works out perfectly, how does it handle dynamic memory allocation? In other words, what is a probable to avoid segmentation fault? BTW, if I use array directly such as int array[MAX]; I can claim larger array, but still when MAX = 4*80k, segmentation fault happened. Here, 4*80K is not necessarily the threshold value like 33790 in the above case. How to figure out system heap size, data memory size, stack size and so on? Thanks very much. |
Do you really need an array this big ? I'd be interested to know if it works with a vector btw, as it shouldn't (a priori)..
As for resizing the heap size, I don't know either :s |
Quote:
So, a large array is necessary to show the difference. I knew STL vector works but there are cases where STL is not available/no needed. And before STL came to this world, how would people sort a large array? |
Code:
//this doesn't allocate an array, Quote:
|
Quote:
more details on ulimit? tried to run ulimit -s 'much bigger number than ulimit -a shows' but seemed no difference. |
Hi -
I totally disagree with your conclusion that the problem is necessarily insufficient stack space or insufficient heap. You simply haven't given enough information to determine what the actual problem is yet. Please do the following: 1. Give specific OS and compiler versions. 2. Run "ulimit" and cut/paste the results 3. Try this test program (or cut/paste the equivalent code) Code:
#include <iostream> Quote:
|
what's the purpose of includeing: #include<new>?
Other than that, I have already figured out what I did wrong. Many thanks. Quote:
|
Quote:
Code:
$ cat array.cc Note that I used char arrays so that MAX would be in bytes. I believe #include<new> declares the bad_alloc exception class. |
got it.
Thanks very much for everybody's help. Quote:
|
All times are GMT -5. The time now is 04:13 PM. |