Results 1 to 7 of 7
The following code
char* p = new char[1024 * 1024 * 512];
produces this error
terminate called after throwing an instance of 'std::bad_alloc'
what(): St9bad_alloc
Aborted
On Windows, if paging ...
- 06-01-2005 #1Just Joined!
- Join Date
- Jan 2005
- Posts
- 25
How can I allocate virtual memory?
The following code
char* p = new char[1024 * 1024 * 512];
produces this error
terminate called after throwing an instance of 'std::bad_alloc'
what(): St9bad_alloc
Aborted
On Windows, if paging file permits, the allocation request would succeed and the kernel would return a pointer to a block of memory that is mapped in pagefile. However, you can do operations on the returned memory block just like you would physical memory.
I want to know is there any VirtualAlloc similar APIs on Linux. Thanks.
- 06-01-2005 #2
That much memory should be malloc()'d, which returns a pointer for you anyway. (I'm assuming you're using C? I'm sure C++ has some sort of malloc() variant)
- 06-02-2005 #3Just Joined!
- Join Date
- Jan 2005
- Posts
- 25
Well, malloc does not throw that excetion, but it fails returning a null pointer.
- 06-02-2005 #4Linux Enthusiast
- Join Date
- Jan 2005
- Posts
- 575
What does ulimit give you ?
- 06-02-2005 #5Just Joined!
- Join Date
- Jan 2005
- Posts
- 25
ulimit gives me 'unlimited'.
However, if I try to malloc 1024 * 1024 * 128, it succeeds.
Maybe I am running out of disk space.
Here is df output:
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
3.5G 2.4G 928M 73% /
/dev/hda1 99M 9.8M 84M 11% /boot
none 94M 0 94M 0% /dev/shm
- 06-02-2005 #6
Read the docs malloc returns a NULL pointer when there is no more memory left http://www.gnu.org/software/libc/man...c%20Allocation .
- 06-03-2005 #7Linux Newbie
- Join Date
- Oct 2004
- Posts
- 158
You're allocating 512 MB.
Make sure the swap file is a LOT larger than 512MB first off.
Total available memory is: (swapfile size + MB of RAM) - current usage
If the system is set up reasonably well, you should have about 2GB+ of virtual memory available. Your response may be really poor if you try it, however.
The ulimit command you want is probablyCode:ulimit -aH


Reply With Quote
