never cast malloc? 
Author Message
 never cast malloc?

Are there situations when malloc should be casted?


Sat, 23 Jul 2005 05:17:57 GMT  
 never cast malloc?

Quote:

> Are there situations when malloc should be casted?

I could invent one or two hypothetical cases where the cast is
necessary, but I can't say that any of them has ever come up for
me in practice.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
 \n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}


Sat, 23 Jul 2005 05:16:45 GMT  
 never cast malloc?

Quote:
> Are there situations when malloc should be casted?

I don't think so, but 'never' is a long time. :-)

If you encounter a situation where you feel it might
be necessary, post a message describing it (best with code),
and the gurus will give you (pardon the pun) pointers.

-Mike



Sat, 23 Jul 2005 05:41:28 GMT  
 never cast malloc?

Quote:


>> Are there situations when malloc should be casted?

> I could invent one or two hypothetical cases where the cast is
> necessary, but I can't say that any of them has ever come up for
> me in practice.

Thanks, i suppose my question now is, what kind of error
would the following code mask? ( that seems to be
the reason casting the return of malloc is bad. )

struct sorted_author_rec *s;
s = (struct sorted_author_rec *) malloc(members * sizeof(*s));



Sat, 23 Jul 2005 05:37:11 GMT  
 never cast malloc?

Quote:

> Are there situations when malloc should be casted?

I don't think so. But it could quiet some 'suspicious pointer assignment'
or other warnings.


Sat, 23 Jul 2005 05:39:17 GMT  
 never cast malloc?

Quote:

> Thanks, i suppose my question now is, what kind of error
> would the following code mask? ( that seems to be
> the reason casting the return of malloc is bad. )

> struct sorted_author_rec *s;
> s = (struct sorted_author_rec *) malloc(members * sizeof(*s));

Casting malloc() most commonly masks a failure to #include
<stdlib.h>.
--
Just another C hacker.


Sat, 23 Jul 2005 05:37:39 GMT  
 never cast malloc?

Quote:


>>> Are there situations when malloc should be casted?

>> I could invent one or two hypothetical cases where the cast is
>> necessary, but I can't say that any of them has ever come up for
>> me in practice.
> Thanks, i suppose my question now is, what kind of error
> would the following code mask? ( that seems to be
> the reason casting the return of malloc is bad. )
> struct sorted_author_rec *s;
> s = (struct sorted_author_rec *) malloc(members * sizeof(*s));

Suppose you haven't #included <stdlib.h> or prototyped malloc()
yourself. C now doesn't know malloc()'s prototype so it's obliged to
think malloc() returns int.
Now malloc() allocates some memory somewhere and puts its address in
the place where address values go. (This might be a register in the
CPU, for example.) But no one says address values and int values have
to live in the same place (the same register in our example).
So, because C thinks malloc() returns int, it reads an int from the
place where ints live, which now contains garbage, because malloc()
didn't write anything there.
The cast treats the int as it were a pointer to struct
sorted_author_rec. But it doesn't help - the int is garbage, and no
amount of casting is going to make anything useful out of it. It's
the GIGO principle.
The compiler would try to warn you about the assignment - "I have an
int, but you say you want a struct sorted_author_rec *? That makes no
sense!" But with the cast, you are telling the compiler - "Shut up. I
know what I'm doing." So the compiler trusts you, and generates code
that will happily fail.

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"Roses are red, violets are blue, I'm a schitzophrenic and so am I."
   - Bob Wiley



Sat, 23 Jul 2005 05:51:54 GMT  
 never cast malloc?

Quote:

> Thanks, i suppose my question now is, what kind of error
> would the following code mask? ( that seems to be
> the reason casting the return of malloc is bad. )

> struct sorted_author_rec *s;
> s = (struct sorted_author_rec *) malloc(members * sizeof(*s));

The reason often given here is that if you forget to include <stdlib.h>,
the statement invokes undefined behavior and you won't notice, because
with the cast the compiler is required to issue a diagnostic.

I personally have never forgotten to include <stdlib.h> [*], but I still
perfer to omit the cast: It's not needed, and it makes the statement harder
to read.

[*] As a newbie, I have often included unneeded headers, though.

Martin



Sat, 23 Jul 2005 05:57:39 GMT  
 never cast malloc?

Quote:


> > Are there situations when malloc should be casted?

> I could invent one or two hypothetical cases where the cast is necessary,

Out of curiosity, what would such a hypothetical case be? (I'm asking
because I cannot think of /any/ case where the cast is necessary.)

Martin



Sat, 23 Jul 2005 06:00:03 GMT  
 never cast malloc?

Quote:



> > > Are there situations when malloc should be casted?

> > I could invent one or two hypothetical cases where the cast is necessary,

>  Out of curiosity, what would such a hypothetical case be? (I'm asking
>  because I cannot think of /any/ case where the cast is necessary.)

You might want to pass the return value of malloc directly to a
variadic function that expects the argument to be of another pointer
type.  The type conversions that apply to the parameters of a call to
a prototyped function don't occur in the case of values passed through
the ellipsis.  Even here the cast isn't strictly necessary; you can
use a temporary instead.

A more realistic example: an inline function declared in a header file
that can potentially be used in a C++ program.

Jeremy.



Sat, 23 Jul 2005 06:19:51 GMT  
 never cast malloc?

Quote:



> > > Are there situations when malloc should be casted?

> > I could invent one or two hypothetical cases where the cast is necessary,

> Out of curiosity, what would such a hypothetical case be? (I'm asking
> because I cannot think of /any/ case where the cast is necessary.)

Varargs argument where the type expected is not void *.


Sat, 23 Jul 2005 06:09:27 GMT  
 never cast malloc?

Quote:




> > > > Are there situations when malloc should be casted?

> > > I could invent one or two hypothetical cases where the cast is necessary,

> > Out of curiosity, what would such a hypothetical case be? (I'm asking
> > because I cannot think of /any/ case where the cast is necessary.)

> Varargs argument where the type expected is not void *.

Thanks!

Martin



Sat, 23 Jul 2005 06:30:32 GMT  
 never cast malloc?

Quote:




> > > > Are there situations when malloc should be casted?

> > > I could invent one or two hypothetical cases where the cast is necessary,

> > Out of curiosity, what would such a hypothetical case be? (I'm asking
> > because I cannot think of /any/ case where the cast is necessary.)

> Varargs argument where the type expected is not void *.

Oh, also a non-"void *" pointer argument to a function whose
declaration is not a prototype.  (But you'd be better off writing
out a prototype for it.)
--
"To get the best out of this book, I strongly recommend that you read it."
--Richard Heathfield


Sat, 23 Jul 2005 06:31:25 GMT  
 never cast malloc?

Quote:

> Are there situations when malloc should be casted?

Thanks all!


Sat, 23 Jul 2005 07:13:38 GMT  
 never cast malloc?

Quote:



>> > > Are there situations when malloc should be casted?

>> > I could invent one or two hypothetical cases where the cast is necessary,

>> Out of curiosity, what would such a hypothetical case be? (I'm asking
>> because I cannot think of /any/ case where the cast is necessary.)

>Varargs argument where the type expected is not void *.

Well, that's easily avoided

  expected *p = malloc(123);
  varargs_func(fixed_args, p);

One situation where a cast is required is when converting the pointer
to an integer, as allowed by C99

  intptr_t address = (intptr_t)malloc(666);

-- Mat.



Sat, 23 Jul 2005 07:45:07 GMT  
 
 [ 25 post ]  Go to page: [1] [2]

 Relevant Pages 

1. casting malloc question.

2. casting malloc question.

3. type casting malloc() to void *

4. More about casting malloc()...

5. About casting malloc()...

6. Type casting malloc??

7. Casting malloc pointer...

8. Casting malloc() and calloc()

9. Why not cast malloc?

10. Casting malloc() in ANSI C?

11. Why cast malloc's return?

12. malloc: to cast or not to cast

 

 
Powered by phpBB® Forum Software