malloc() failure
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > malloc() failure




Pages (2): [1] 2 »   Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    malloc() failure  
Roman Mashak


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-23-05 01:49 AM

Hello, All!

It's considered a right way to check result of malloc(). If 'malloc' failed
(that's returned NULL) - is there a general way to deal with it or it
strictly depends on application design? On the other hand, the reason memory
wasn't allocated might be far beyond of my program's concern, it may be OS
faulty or similar.

In my case, I just invoke 'exit(EXIT_FAILURE)'.

Anyway, I'd appreciate your comments, thanks.

With best regards, Roman Mashak.  E-mail: mrv@tusur.ru







[ Post a follow-up to this message ]



    Re: malloc() failure  
Fletcher Glenn


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-23-05 07:48 AM


"Roman Mashak" <mrv@tusur.ru> wrote in message
news:dedu2v$ph0$1@relay.tomsk.ru...
> Hello, All!
>
> It's considered a right way to check result of malloc(). If 'malloc'
> failed (that's returned NULL) - is there a general way to deal with it or
> it strictly depends on application design? On the other hand, the reason
> memory wasn't allocated might be far beyond of my program's concern, it
> may be OS faulty or similar.
>
> In my case, I just invoke 'exit(EXIT_FAILURE)'.
>
> Anyway, I'd appreciate your comments, thanks.
>
> With best regards, Roman Mashak.  E-mail: mrv@tusur.ru
>

Even though your program may not be at fault, you can get a malloc
error if the system runs out of swap space and all of real memory is
full.  The only way you can deal with this from within the program
is to either re-use some memory you have already got, or give up.

--

Fletcher Glenn







[ Post a follow-up to this message ]



    Re: malloc() failure  
Gordon Burditt


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-23-05 07:48 AM

>It's considered a right way to check result of malloc(). If 'malloc' failed
>(that's returned NULL) - is there a general way to deal with it or it
>strictly depends on application design? On the other hand, the reason memor
y
>wasn't allocated might be far beyond of my program's concern, it may be OS
>faulty or similar.
>
>In my case, I just invoke 'exit(EXIT_FAILURE)'.

There are several things you might consider doing first:

- Print an error message (if possible).
- If you've got temporary files open, close them and delete them.
This comes under the general heading of "clean up your mess".
- If it's an interactive program like an editor, consider aborting
the command and either let the user try another command, or give
the user a chance to save his work (which hopefully doesn't require
more memory to handle).
- Log the failure, including how much memory you tried to allocate.
Some failures to allocate memory happen because the amount of memory
to allocate is very silly (random bits or (unsigned long)-1 or
something like that).

Gordon L. Burditt





[ Post a follow-up to this message ]



    Re: malloc() failure  
Pascal Bourguignon


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-23-05 07:48 AM

"Roman Mashak" <mrv@tusur.ru> writes:
> It's considered a right way to check result of malloc(). If 'malloc' faile
d
> (that's returned NULL) - is there a general way to deal with it or it
> strictly depends on application design?

Time to call the garbage collector!

> On the other hand, the reason memory
> wasn't allocated might be far beyond of my program's concern, it may be OS
> faulty or similar.
>
> In my case, I just invoke 'exit(EXIT_FAILURE)'.

--
__Pascal Bourguignon__                     http://www.informatimago.com/
Kitty like plastic.
Confuses for litter box.
Don't leave tarp around.





[ Post a follow-up to this message ]



    Re: malloc() failure  
Roman Mashak


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-23-05 07:48 AM

Hello, Pascal!
You wrote  on Tue, 23 Aug 2005 06:54:25 +0200:

??>> failed (that's returned NULL) - is there a general way to deal with it
??>> or it strictly depends on application design?

PB> Time to call the garbage collector!
Is it some type of utility?
??>> On the other hand, the reason memory

With best regards, Roman Mashak.  E-mail: mrv@tusur.ru







[ Post a follow-up to this message ]



    Re: malloc() failure  
Maxim Yegorushkin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-23-05 12:54 PM


Roman Mashak wrote:
> Hello, All!
>
> It's considered a right way to check result of malloc(). If 'malloc' faile
d
> (that's returned NULL) - is there a general way to deal with it or it
> strictly depends on application design? On the other hand, the reason memo
ry
> wasn't allocated might be far beyond of my program's concern, it may be OS
> faulty or similar.
>
> In my case, I just invoke 'exit(EXIT_FAILURE)'.

There are two different cases with malloc.

The first is when RLIMIT_DATA for your process is not RLIM_INFINITY. In
this case malloc can return 0 when brk() or mmap() fails.

The second is when RLIMIT_DATA == RLIM_INFINITY. In this case man
malloc:

BUGS
By default, Linux follows an  optimistic  memory  allocation
strategy.
This  means  that  when malloc() returns non-NULL there is no
guarantee
that the memory really is available. This is a really bad bug.
In case
it  turns  out  that the system is out of memory, one or more
processes
will be killed by the infamous OOM killer.  In case Linux  is
employed
under  circumstances  where it would be less desirable to
suddenly lose
some randomly picked processes, and moreover the kernel version
is suf-
ficiently recent, one can switch off this overcommitting
behavior using
a command like
# echo 2 > /proc/sys/vm/overcommit_memory
See also  the  kernel  Documentation  directory,  files
vm/overcommit-
accounting and sysctl/vm.txt.






[ Post a follow-up to this message ]



    Re: malloc() failure  
Ulrich Hobelmann


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-23-05 12:54 PM

Pascal Bourguignon wrote:
> "Roman Mashak" <mrv@tusur.ru> writes: 
>
> Time to call the garbage collector!

Maybe that's why some Java programs constantly grow in their memory
usage -- they don't invoke the GC until the whole system runs out of
memory ;)

--
I believe in Karma.  That means I can do bad things to people
all day long and I assume they deserve it.
Dogbert





[ Post a follow-up to this message ]



    Re: malloc() failure  
Jonathan Bartlett


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-23-05 10:56 PM

>  PB> Time to call the garbage collector!
> Is it some type of utility?

For more info on memory management and garbage collection, see:

http://www-128.ibm.com/developerwor...brary/l-memory/

Jon
----
Learn to program using Linux assembly language
http://www.cafeshops.com/bartlettpublish.8640017





[ Post a follow-up to this message ]



    Re: malloc() failure  
John Gordon


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-24-05 11:11 PM

In <dedu2v$ph0$1@relay.tomsk.ru> "Roman Mashak" <mrv@tusur.ru> writes:

> Hello, All!

> It's considered a right way to check result of malloc(). If 'malloc' faile
d
> (that's returned NULL) - is there a general way to deal with it or it
> strictly depends on application design? On the other hand, the reason memo
ry
> wasn't allocated might be far beyond of my program's concern, it may be OS
> faulty or similar.

> In my case, I just invoke 'exit(EXIT_FAILURE)'.

> Anyway, I'd appreciate your comments, thanks.

It depends on the program and its intended use.

For small unpolished programs, exiting would be perfectly fine.  But for
larger programs intended for general use, exiting would be very bad.  I'd
be upset if Microsoft Word exited on a malloc failure after I'd just typed
in my entire graduate thesis, for example.

In general, any module that requests the allocation of resources should
be able to gracefully handle failure, and pass an error code back to
its caller.

--
John Gordon            "It's certainly uncontaminated by cheese."
gordon@panix.com






[ Post a follow-up to this message ]



    Re: malloc() failure  
Rich Teer


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-24-05 11:11 PM

On Wed, 24 Aug 2005, John Gordon wrote:

> be upset if Microsoft Word exited on a malloc failure after I'd just typed
> in my entire graduate thesis, for example.

More fool you for using Word for such an important document!  ;-)

--
Rich Teer, SCNA, SCSA, OpenSolaris CAB member

President,
Rite Online Inc.

Voice: +1 (250) 979-1638
URL: http://www.rite-group.com/rich





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 06:11 AM.      Post New Thread    Post A Reply      
Pages (2): [1] 2 »   Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register