Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #5402

Re: Questions Regarding Null and Casting

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: Questions Regarding Null and Casting
Date 2011-06-03 17:24 -0700
Organization None to speak of
Message-ID <lnlixiigti.fsf@nuthaus.mib.org> (permalink)
References <isbl3d$lvh$1@speranza.aioe.org> <slrniuir6v.4ok.angel+news@pearlgates.net>

Show all headers | View raw


Angel <angel+news@spamcop.net> writes:
> On 2011-06-03, BigChuck19 <bc19@mailinator.com> wrote:
>> My textbook asserts that when checking if sufficient memory was
>> allocated for a dynamic variable using malloc(), a test should be
>> made to determine whether sufficient memory was actually allocated.
>> I understand the concept of checking if the memory was allocated; the
>> code confuses me, however.  E.g., a fragment of code from the book
>> is:
>>
>> grades = (int *) malloc(numgrades * sizeof(int));
>> if (grades == (int *) NULL) {
>>    printf("Failed to allocate grades array");
>>    exit(1);
>>
>> }
>
> Must be a very old book, this is a code example from the time when there
> was no "void *" type and malloc() returned a value of type "char *".

Not necessarily.  The code still works, it's just poor style.
Even K&R2 says to cast the result of malloc, though the errata page
says this needs to be rewritten.

[...]

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Questions Regarding Null and Casting BigChuck19 <bc19@mailinator.com> - 2011-06-03 21:52 +0000
  Re: Questions Regarding Null and Casting Ian Collins <ian-news@hotmail.com> - 2011-06-04 10:13 +1200
    Re: Questions Regarding Null and Casting Ian Collins <ian-news@hotmail.com> - 2011-06-04 10:16 +1200
  Re: Questions Regarding Null and Casting Keith Thompson <kst-u@mib.org> - 2011-06-03 15:25 -0700
    Re: Questions Regarding Null and Casting Tim Rentsch <txr@alumni.caltech.edu> - 2011-06-03 20:45 -0700
  Re: Questions Regarding Null and Casting pete <pfiland@mindspring.com> - 2011-06-03 18:38 -0400
  Re: Questions Regarding Null and Casting Angel <angel+news@spamcop.net> - 2011-06-03 23:23 +0000
    Re: Questions Regarding Null and Casting Keith Thompson <kst-u@mib.org> - 2011-06-03 17:24 -0700
  Re: Questions Regarding Null and Casting "J. J. Farrell" <jjf@bcs.org.uk> - 2011-06-04 00:45 +0100
  Re: Questions Regarding Null and Casting Keith Thompson <kst-u@mib.org> - 2011-06-03 17:27 -0700
  Re: Questions Regarding Null and Casting pete <pfiland@mindspring.com> - 2011-06-03 21:12 -0400
  Re: Questions Regarding Null and Casting Shao Miller <sha0.miller@gmail.com> - 2011-06-06 20:11 -0500

csiph-web