type casting malloc() to void * 
Author Message
 type casting malloc() to void *

I've seen a little bit of the discussion regarding type casting vs. using
size_of() when calling malloc().

However, if all I want is a number of bytes of memory do I have any option
other than :

int main(void)
{

void *memory_ptr
memory_ptr=(void *)malloc(500);

Quote:
}

TIA
Ivan.


Tue, 18 Sep 2001 03:00:00 GMT  
 type casting malloc() to void *

Quote:
>I've seen a little bit of the discussion regarding type casting vs. using
>size_of() when calling malloc().

>However, if all I want is a number of bytes of memory do I have any option
>other than :

>int main(void)
>{

>void *memory_ptr
>memory_ptr=(void *)malloc(500);

The return value of malloc is already void *, so you should not need the cast
at all. If you are getting a compiler error when you remove the cast, it's
because you have forgotten to include the header which declares malloc, that is
<stdlib.h>. By putting in this cast to shut up the compiler, you have not made
the serious error go away, you have only masked it.

In C, one does not have to cast the return value of malloc; the void * type can
be implicitly converted to a pointer to another incomplete type or object type.



Tue, 18 Sep 2001 03:00:00 GMT  
 type casting malloc() to void *

Quote:

>I've seen a little bit of the discussion regarding type casting vs. using
>size_of() when calling malloc().

The two issues of casting the return value and using the sizeof operator in
the argument are entirely unrelated, there is no one vs. the other issue.

Quote:
>However, if all I want is a number of bytes of memory do I have any option
>other than :

>int main(void)
>{

>void *memory_ptr
>memory_ptr=(void *)malloc(500);

>}

This is simply wrong since you call malloc() without an appropriate
declaration in scope. Unfortunately since you put the cast in the compiler
will probably compile the code without generating any warnings. This
is why you should avoid casts (or at least pointer casts) whenever possible.
This is correct and should compile cleanly with an ANSI C compiler.

#include <stdlib.h>

int main(void)
{
    void *memory_ptr;
    memory_ptr = malloc(500);

    return 0;

Quote:
}

--
-----------------------------------------


-----------------------------------------


Tue, 18 Sep 2001 03:00:00 GMT  
 type casting malloc() to void *
Thank-you for the replies.

With the benefit of these replies I can see that some more checking would have
saved the question - ain't hindsight a wonderful thing :)

Even in the "real" programme I forgot to #include <stdlib.h> and then, of
course, just had to (void *)malloc() to shut the compiler up.

Wrong solution Ivan, you silly boy - when will you learn that compilers print
errors and warnings for reasons other than just for fun !

The second error was not checking the malloc() prototype - I'll let someone
else decide if this was an error or sheer {*filter*}y laziness :)

I have found that even the simplest programme that I write has to be compiled
at least twice to find the missing ;'s  :)

Thanks again.

Ivan.



Wed, 19 Sep 2001 03:00:00 GMT  
 type casting malloc() to void *

   I have found that even the simplest programme that I write has to be compiled
   at least twice to find the missing ;'s  :)

A good editor can fix problems like this.  For instance, I use GNU
Emacs, and it supplies a newline automatically when I press ;.  So if
I don't press ; then I don't get a new line, and it's obvious that
something's wrong as I type.
--
"I ran it on my DeathStation 9000 and demons flew out of my nose." --Kaz

Please: do not email me copies of your posts to comp.lang.c
        do not ask me C questions via email; post them instead



Wed, 19 Sep 2001 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. (void *) & casting the pointer returned by malloc

2. Purpose of casting a void type

3. casting a structure type on a void pointer in the Watch window

4. Type-cast from a void pointer

5. Type casting malloc??

6. casting of struct element void pointer casting

7. HELP WANTED with converting void* from malloc() to void far*

8. malloc: to cast or not to cast

9. Type Casting between Base and Derived Type (Serialization of objects with Type members)

10. void pointer (void *) : how to determine type?

11. from void (void*) to void (_cdecl*) (void*)

12. HRESULT return type turns into void when adding class from type library

 

 
Powered by phpBB® Forum Software