malloc: to cast or not to cast 
Author Message
 malloc: to cast or not to cast

Greetings.

I have a question about apparent inconsistencies between the FAQ questions
6.16 and 7.7; and between FAQ question 7.7 and K&R2 Section 7.8.5.

FAQ question 7.7, regarding casting the return value of malloc says:

        "Under ANSI/ISO Standard C, these casts are no longer necessary,
        and in fact modern practice discourages them, since they can
        camouflage important warnings which would otherwise be generated
        if malloc() happened not to be declared correctly..."

In FAQ question 6.16 the example of allocating an array of pointers consistently
uses casts for malloc's return values:

             int **array1 = (int **)malloc(nrows * sizeof(int *));
             for(i = 0; i < nrows; i++)
                     array1[i] = (int *)malloc(ncolumns * sizeof(int));
and

             int *array3 = (int *)malloc(nrows * ncolumns * sizeof(int));

K&R2 Section 7.8.5 says:

   "The pointer returned by malloc or calloc has the proper alignment
   for the object in question, but it must be cast into the appropriate
   type..."

and gives an example:

   int *ip;

   ip = (int *) calloc(n, sizeof(int));

My question?  Which is correct?

I look forward to your replies.

Martin



Tue, 15 Aug 2000 03:00:00 GMT  
 malloc: to cast or not to cast

Quote:

>FAQ question 7.7, regarding casting the return value of malloc says:

>        "Under ANSI/ISO Standard C, these casts are no longer necessary,
>    and in fact modern practice discourages them, [...]

>In FAQ question 6.16 the example of allocating an array of pointers consistently
>uses casts for malloc's return values:

This has been mentioned before, and I believe Steve said it's on his list
to change some time.  (My apologies if I've misremembered.  I'm sure he
knows about it, though.)

Quote:
>K&R2 Section 7.8.5 says:

>   "The pointer returned by malloc or calloc has the proper alignment
>   for the object in question, but it must be cast into the appropriate
>   type..."

The definitive reference is, of course, the ISO Standard, which defines
'malloc' and 'calloc' as returning a 'void *' and also states that a
'void *' can be assigned to a pointer to any object type without a cast.
So FAQ question 7.7 is correct.

The casts are often discouraged because they can hide the error of
forgetting to include <stdlib.h>.  Without this include, 'malloc' will
be assumed to return 'int'.  If you don't cast the result, the assignment
to a pointer type will generate a diagnostic (error or warning); but, if
you cast the result, this will suppress the warning and hide the mistake.

The opinion that the cast is bad style is not quite universal in this
group, and we have occasional flamewars on the subject, but the discussion
never really goes anywhere (hint, hint, everyone).  :-)

Cheers,
Richard
--
Richard Stamp
Churchill College, Cambridge



Tue, 15 Aug 2000 03:00:00 GMT  
 malloc: to cast or not to cast

Quote:

>Greetings.

>I have a question about apparent inconsistencies between the FAQ questions
>6.16 and 7.7; and between FAQ question 7.7 and K&R2 Section 7.8.5.

Notice first, the Copyright date of K&R2 - 1988.  The ANSI/ISO spec was
ratified in 1989, though there was a completed spec in 1988.

Quote:
>FAQ question 7.7, regarding casting the return value of malloc says:

>        "Under ANSI/ISO Standard C, these casts are no longer necessary,
> and in fact modern practice discourages them, since they can
> camouflage important warnings which would otherwise be generated
>        if malloc() happened not to be declared correctly..."

>In FAQ question 6.16 the example of allocating an array of pointers
consistently
>uses casts for malloc's return values:

>             int **array1 = (int **)malloc(nrows * sizeof(int *));
>             for(i = 0; i < nrows; i++)
>                     array1[i] = (int *)malloc(ncolumns * sizeof(int));
>and

>             int *array3 = (int *)malloc(nrows * ncolumns * sizeof(int));

They are discouraged.  There are some arguments that they are not harmful.
Compatibility with C++ is sometimes given as an argument in their favor [but
in real-life you should use new/delete, new[]/delete[] with C++].  I would not
bother getting rid of them in old, debugged, working code.  But you will
notice the quote: "modern practice discourages them" and this I agree with.
They are not disallowed, just discouraged.  The biggest danger is that you
will shoot yourself in the foot if you eliminate the prototypes somehow.  At
any rate, with a modern compiler and the warnings turned up to maximum, you
are probably not going to experience problems with either technique.  If your
prototype is missing, you will get a warning.

Quote:
>K&R2 Section 7.8.5 says:

>   "The pointer returned by malloc or calloc has the proper alignment
>   for the object in question, but it must be cast into the appropriate
>   type..."

>and gives an example:

>   int *ip;

>   ip = (int *) calloc(n, sizeof(int));

>My question?  Which is correct?

The FAQ.  The invention of void pointers [which is what calloc()/malloc()
return] did away with the need for casts.
--
Hypertext C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-FAQ ftp: ftp://rtfm.mit.edu, C-FAQ Book: ISBN 0-201-84519-9
Try "C Programming: A Modern Approach" ISBN 0-393-96945-2
Want Software?  Algorithms?  Pubs? http://www.infoseek.com


Tue, 15 Aug 2000 03:00:00 GMT  
 malloc: to cast or not to cast

Quote:

>   ip = (int *) calloc(n, sizeof(int));

>My question?  Which is correct?

They are both correct.

In C, it is good style to write only those casts which are necessary to
perform a necessary conversion that would otherwise not take place.

In a few rare situations, casts can enhance type safety, but they
are often detrimental to it, because they are commonly used to
perform less than portable inter-pointer conversions.



Wed, 16 Aug 2000 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. to cast or not to cast?

2. Discarded Function Values (To Cast or Not to Cast)

3. When is a cast not a cast?

4. Why not cast the rerurn from malloc()?

5. Why not cast malloc?

6. old style casts vs new style casts

7. casting of struct element void pointer casting

8. Cast...always cast...

9. Need help with reinterpret_cast, a C-style cast or function-style cast

10. old style casts vs new style casts

11. Need help with reinterpret_cast, a C-style cast or function-style cast

12. CString cast to LPCTSTR cast error

 

 
Powered by phpBB® Forum Software