When to check the return value of malloc
- From: Marty James <mart@xxxxxxxxxx>
- Date: Fri, 18 Jan 2008 22:21:46 +0100 (CET)
Howdy,
I was reflecting recently on malloc.
Obviously, for tiny allocations like 20 bytes to strcpy a filename or
something, there's no point putting in a check on the return value of
malloc.
OTOH, if you're allocating a gigabyte for a large array, this might
fail, so you should definitely check for a NULL return.
So somewhere in between these extremes, there must be a point where you
stop ignoring malloc's return value, and start checking it.
Where do people draw this line? I guess it depends on the likely system
the program will be deployed on, but are there any good rule-of-thumbs?
Rgds,
MJ
.
- Follow-Ups:
- Re: When to check the return value of malloc
- From: Syren Baran
- Re: When to check the return value of malloc
- From: Gordon Burditt
- Re: When to check the return value of malloc
- From: Rob Hiddink
- Re: When to check the return value of malloc
- From: gw7rib
- Re: When to check the return value of malloc
- From: Malcolm McLean
- Re: When to check the return value of malloc
- From: Bart C
- Re: When to check the return value of malloc
- From: Ulrich Eckhardt
- Re: When to check the return value of malloc
- From: user923005
- Re: When to check the return value of malloc
- From: CBFalconer
- Re: When to check the return value of malloc
- From: Richard Tobin
- Re: When to check the return value of malloc
- From: jameskuyper
- Re: When to check the return value of malloc
- From: Mark McIntyre
- Re: When to check the return value of malloc
- From: Eric Sosman
- Re: When to check the return value of malloc
- From: Paul Hsieh
- Re: When to check the return value of malloc
- From: Kaz Kylheku
- Re: When to check the return value of malloc
- From: Ian Collins
- Re: When to check the return value of malloc
- From: Randy Howard
- Re: When to check the return value of malloc
- From: fnegroni
- Re: When to check the return value of malloc
- From: Default User
- Re: When to check the return value of malloc
- Prev by Date: Re: Is Chris Hills a troll?
- Next by Date: Re: When to check the return value of malloc
- Previous by thread: Re: See your home on your computer through google earth
- Next by thread: Re: When to check the return value of malloc
- Index(es):
Relevant Pages
|