Find the answer to your Linux question:
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just 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.

  2. #2
    Linux Guru lakerdonald's Avatar
    Join Date
    Jun 2004
    Location
    St. Petersburg, FL
    Posts
    5,035
    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)

  3. #3
    Just Joined!
    Join Date
    Jan 2005
    Posts
    25
    Well, malloc does not throw that excetion, but it fails returning a null pointer.

  4. #4
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    What does ulimit give you ?

  5. #5
    Just 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

  6. #6
    Linux Engineer Giro's Avatar
    Join Date
    Jul 2003
    Location
    England
    Posts
    1,219
    Read the docs malloc returns a NULL pointer when there is no more memory left http://www.gnu.org/software/libc/man...c%20Allocation .

  7. #7
    Linux 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 probably
    Code:
    ulimit -aH

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •