Welcome to Linux Forums! With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.
Find the answer to your Linux question:
New to Linux Forums? Register here for free!
    Linux Forums > GNU Linux Zone > Linux Programming & Scripting > How can I allocate virtual memory?

Forgot Password?
 Linux Programming & Scripting   C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Site Navigation
Linux Articles
Linux Forums
Linux Downloads
Linux Hosting
Free Magazines
Job Board
IRC Chat
RSS Feeds


Linux Forum Topics
Linux Forums
Your Distro
Linux Resources
GNU Linux Zone
The Community
Reply
 
Thread Tools Display Modes
Old 06-01-2005   #1 (permalink)
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.
neo_in_matrix is offline  


Reply With Quote
Old 06-01-2005   #2 (permalink)
Linux Guru
 
lakerdonald's Avatar
 
Join Date: Jun 2004
Location: St. Petersburg, FL
Posts: 5,039
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)
__________________
the lost art of found sound
lakerdonald is offline   Reply With Quote
Old 06-02-2005   #3 (permalink)
Just Joined!
 
Join Date: Jan 2005
Posts: 25
Well, malloc does not throw that excetion, but it fails returning a null pointer.
neo_in_matrix is offline   Reply With Quote
Old 06-02-2005   #4 (permalink)
Linux Enthusiast
 
Join Date: Jan 2005
Posts: 575
What does ulimit give you ?
Santa's little helper is offline   Reply With Quote
Old 06-02-2005   #5 (permalink)
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
neo_in_matrix is offline   Reply With Quote
Old 06-02-2005   #6 (permalink)
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 .
Giro is offline   Reply With Quote
Old 06-03-2005   #7 (permalink)
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
jim mcnamara is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Free Magazines
Run Your Own Web Server Using Linux & Apache - Free 191 Page Preview
Learn about everything you'll need to build and maintain your Linux servers, and to deploy Web applications to them.
subscribe
Open Source Security Myths Dispelled
Dispel the five major myths surrounding Open Source Security and gain the tools necessary to make a truly informed decision for your IT organization
subscribe
InformationWeek
InformationWeek is the only newsweekly you'll need to stay on top of the latest developments in information technology.
subscribe



All times are GMT. The time now is 10:55 AM.






© 2000 - 2009 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.3.0 RC2