473,326 Members | 2,012 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

what to do when malloc returns NULL?

Suppose I have implemented a language with garbage collection in C. I
have wrapped malloc in my own C function. If malloc returns NULL then
I can run the garbage collector and then try malloc again. What do I
do if malloc returns NULL again? Can a C program ask the operating
system for more space and then I could try malloc a third time?

Thanks,
Peter
Mar 25 '08 #1
6 7870
On Mon, 24 Mar 2008 22:39:25 -0700, Peter Michaux wrote:
Suppose I have implemented a language with garbage collection in C. I
have wrapped malloc in my own C function. If malloc returns NULL then I
can run the garbage collector and then try malloc again. What do I do if
malloc returns NULL again? Can a C program ask the operating system for
more space and then I could try malloc a third time?
No, or at least, not portably, and I'm not aware of any system-specific
ways. If malloc returns NULL, and you've freed everything that can be
freed, there just isn't enough memory available. One way to handle this
is abort (either the program, or the current operation), and show the
user an error. Another is to pause, asking the user to shut down other
applications. But in the second case, that's not something that the OS
should do automatically.
Mar 25 '08 #2
Pausing for some time and trying to allocate memory could work, if in
the mean time some memory gets freed.

~Jack
-------------------------------------
http://programmingsite.googlepages.com
On Mar 25, 10:39*am, Peter Michaux <petermich...@gmail.comwrote:
Suppose I have implemented a language with garbage collection in C. *I
have wrapped malloc in my own C function. If malloc returns NULL then
I can run the garbage collector and then try malloc again. What do I
do if malloc returns NULL again? Can a C program ask the operating
system for more space and then I could try malloc a third time?

Thanks,
Peter
Mar 25 '08 #3
On Mar 24, 10:46 pm, Harald van D©¦k <true...@gmail.comwrote:
On Mon, 24 Mar 2008 22:39:25 -0700, Peter Michaux wrote:
Suppose I have implemented a language with garbage collection in C. I
have wrapped malloc in my own C function. If malloc returns NULL then I
can run the garbage collector and then try malloc again. What do I do if
malloc returns NULL again? Can a C program ask the operating system for
more space and then I could try malloc a third time?

No, or at least, not portably, and I'm not aware of any system-specific
ways.
Well I suppose that is good news in some way. If it is not possible
then don't have to implement it ;-)

Thanks,
Peter
Mar 25 '08 #4
Peter Michaux wrote:
Suppose I have implemented a language with garbage collection in C. I
have wrapped malloc in my own C function.
If malloc returns NULL then
I can run the garbage collector and then try malloc again. What do I
do if malloc returns NULL again? Can a C program ask the operating
system for more space and then I could try malloc a third time?
The only way that a portable C program can ask the operating system for
more space for dynamic memory is through the *alloc functions. So you
_could_ keep trying malloc a predefined number of times before bombing
out, but IMHO it's better, in a new language, to use the APIs provided
by the platform for memory management. Often they provide control
beyond what ISO C malloc does.

Mar 25 '08 #5
santosh wrote:
Peter Michaux wrote:
>Suppose I have implemented a language with garbage collection in C.
I have wrapped malloc in my own C function.
If malloc returns NULL then
I can run the garbage collector and then try malloc again. What do I
do if malloc returns NULL again? Can a C program ask the operating
system for more space and then I could try malloc a third time?

The only way that a portable C program can ask the operating system
for more space for dynamic memory is through the *alloc functions. So
you _could_ keep trying malloc a predefined number of times before
bombing out, but IMHO it's better, in a new language, to use the APIs
provided by the platform for memory management. Often they provide
control beyond what ISO C malloc does.
OK, seeing Harald's reply I see that I misunderstood your question.
Please excuse.

Mar 25 '08 #6
Peter Michaux wrote:
Harald van D©¦k <true...@gmail.comwrote:
>Peter Michaux wrote:
>>Suppose I have implemented a language with garbage collection in
C. I have wrapped malloc in my own C function. If malloc returns
NULL then I can run the garbage collector and then try malloc
again. What do I do if malloc returns NULL again? Can a C program
ask the operating system for more space and then I could try
malloc a third time?

No, or at least, not portably, and I'm not aware of any
system-specific ways.

Well I suppose that is good news in some way. If it is not possible
then don't have to implement it ;-)
This illustrates the problems involved in using garbage collection,
which is not what the C system was built to use. There is no way
it can be as efficient as normal use of malloc/free. Which means
you have to pay attention.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Mar 26 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Rene van Hoek | last post by:
Hi, I am using Xalan 1.8.0 and Xerces 2.6.0 in C++. I have an XML document which I first transform into an other XML document using an XSL styelsheet. Then I want to parse with XPathEvaluator...
2
by: Jeff Adams | last post by:
I am using MSVC .NET to create a C program. I am having trouble creating a window. The createwindow returns NULL however no error is caught. The GetLastError() returns "operation completed...
7
by: Ohad Asor | last post by:
Hello all, I have an ASP.NET page I've written using VS.NET2003, which have a ListBox in it. When I press a button in the form, I try to get the selected item in the list by calling...
5
by: Web learner | last post by:
while (dr.Read()) //dr is an instance of sqlDataReader { double minAirTemp = (double)(Single)dr; // minAirTemp is column in SQLExpress table (data type: real, allows nulls) } The above...
11
by: MLH | last post by:
I have 2 lines in a procedure that assign MyVariant a value - line #238 and line #491. When line #238 runs, the value is 152. When line #491 runs, the DLookup function returns Null. I would expect...
1
by: js | last post by:
I am using the following C# code and T-SQL to get result object from a SQL Server database. When my application runs, the ExecuteScalar returns "10/24/2006 2:00:00 PM" if inserting a duplicated...
2
by: Uldis Bojars | last post by:
Hi All, I have encountered problems with JS RegExp.exec() and can't find what is the problem. Could you help me? formRequest is a function that extracts some information from XMLHTTPRequest...
9
by: Milsnips | last post by:
Hi all. i'm tryng to implement the Rewrite.NET url rewritining functionality into a test project i've created, however i am hitting a problem at this line (direct from the web example): ...
6
by: chubbs | last post by:
Hi, Please can you help me? I have the following code: int** adjacencyOneMode; int *dim = 3800; int iv=0;
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.