vBulletin Search Engine Optimization
| |||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| Is NULL guaranteed to evaluate 'not true', e.g. is it completely safe and portable to write: char *pFoo = malloc(1024); if (pFoo) { /* use pFoo */ free(pFoo); } Or must I check against NULL explicitly as in: if (pFoo != NULL) ... Thanks. -- - Mark -> -- |
| |||
| "Mark A. Odell" <nospam@embeddedfw.com> wrote in news:Xns94018E057A0BDCopyrightMarkOdell@130.133.1. 4: I meant to post this in comp.lang.c. I ended up doing so and I'll answer my post here with the answers received there. > Is NULL guaranteed to evaluate 'not true', e.g. is it completely safe > and portable to write: Yes. > char *pFoo = malloc(1024); > if (pFoo) > { This is safe. > /* use pFoo > */ > free(pFoo); > } > > Or must I check against NULL explicitly as in: > > if (pFoo != NULL) ... No, they are equivalent. -- - Mark -> -- |
| |||
| Mark A. Odell wrote: > I meant to post this in comp.lang.c. the "c programming language" (k&r book) can be a nice addition to the bookshelf .. -- /// Michael J. Tobler: motorcyclist, surfer, skydiver, \\\ \\\ and author: "Inside Linux", "C++ HowTo", "C++ Unleashed" /// \\\ http://pages.sbcglobal.net/mtobler/mjt_linux_page.html /// Weiler's Law: Nothing is impossible for the man who doesn't have to do it himself. |
| |||
| mjt <mjtobler@removethis_consultant.com> wrote in news:TLPcb.519$J53.355@newssvr22.news.prodigy.com: > Mark A. Odell wrote: > >> I meant to post this in comp.lang.c. > > the "c programming language" (k&r book) can be a nice addition > to the bookshelf Yes, it look fine there too. I have both K&R1 and 2. Just not at work. -- - Mark -> -- |
| ||||
| In article <Xns94018E057A0BDCopyrightMarkOdell@130.133.1.4> , Mark A. Odell <nospam@embeddedfw.com> wrote: >Is NULL guaranteed to evaluate 'not true', e.g. is it completely safe and >portable to write: > >char *pFoo = malloc(1024); >if (pFoo) >{ > /* use pFoo > */ > free(pFoo); >} > >Or must I check against NULL explicitly as in: > >if (pFoo != NULL) ... Yes, what you want to do is safe, but it's important to differentiate between NULL (a pre-processor macro) and a null pointer. I think the question which you meant to ask was "Is a null pointer guaranteed to evaluate 'not true'?", to which the answer is yes. Some (including me) would argue that it's better style to include the explicit comparison with NULL, because it makes the code more self-documenting. HTH John -- The Linux Emporium - the source for Linux in the UK See http://www.linuxemporium.co.uk/ We had a woodhenge here once but it rotted. |