473,769 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7922
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...@g mail.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
7133
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 the new XML document. I succeed in transforming, also in parsing with the XPathEvaluator. But I can not get any nodeValue, getNodeValue() returns NULL.
2
3415
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 successfully". So I'm not sure what the deal is. CreateWindow doesn't have to be called from the main function does it? WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0, 0, GetModuleHandle(NULL), NULL, NULL, NULL, NULL, ...
7
11199
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 ListBox.SelectedItem. And it returns null, even when I select an item. Even when I try to read the value when I get the SelectedIndexChanged event, it still doesn't give me the correct answer.
5
8386
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 code works. But when there are NULL, I get the following error System.InvalidCastException: Specified cast is not valid
11
1913
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 MyVariant to be Null. But it is not. Its value is still reported as 152. I'm able to confirm its value is not Null by running IsNull(MyVariant) immediately after line #491. 238 MyVariant = Forms!frmUSPSReturnReceipts!RROLAXchewsurBox.Column(3)...
1
14520
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 record. It returns null for all other conditions. Does anyone know why? Does anyone know how to get the output value? Thanks. ------ C# ----- aryParams = {'10/24/2006 2pm', '10/26/2006 3pm', 2821077, null}; object oRtnObject = null;...
2
14677
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 response. A very strange effect (and I can't find where I've done something wrong) is that regexp matches in this function fail on every second call.
9
2719
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): System.Collections.Specialized.NameValueCollection SectionIndex = (System.Collections.Specialized.NameValueCollection)System.Configuration.ConfigurationSettings.GetConfig("Rewrite.NET/Index"); The value always returns me null, and just to check it was...
6
2080
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
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10211
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10045
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9994
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3958
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.