Re: regarding free function in c library



Jack Klein wrote:
On 15 Dec 2006 00:38:54 -0800, "Zealot Zuo" <zealotzuo@xxxxxxxxx>
wrote in comp.lang.c:


sam_cit@xxxxxxxxxxx wrote:
Hi Everyone,

It is known that function free() of c library expects parameter of
type void* and when we invoke them with pointers to any type, compiler
automatically performs the typecast, can anyone let me know as to why
pointers are typecasted to void* in many places before performing
operation on them?

Hi, firstly :)
As for void*s, these pointers are pointers with "common" properties
which would accept any kind of data. For example, you can use malloc()
to allocate a memory which returns a void* pointer, and you could save
the pointer to any pointer.
So the following declaration is right (Though malloc() returns a
void*):

int* a = malloc(10*sizeof(int));

Note that C supports implicit casts from pointers to pointers, where

No, C does not support "implicit casts", no such thing exists in the
language. All casts in C are the result of a cast operator. C does
support conversions, some of which are automatic and some of which

C++ doesn't. To use malloc in C++, you must use explicit typecast, such
as (int*), (void*), etc.

"explicit cast" is a redundant term, all casts in C are explicit
because they use a cast operator.

It is easy to know why these pointers should be re-typecasted to void*
calling free() on them. Both free(int*) and free(float*) and others
should be compiled, but C doesn't support polymorphism. Actually,
polymorphism is not required because freeing an allocated memory
doesn't care the type allocated.

You are just flat out wrong above. Free does care about the value it
receives, which must be a pointer to void. Fortunately, if there is a
prototype of free() in scope, the conversion from pointer to any
object type to pointer to void is automatic, and has nothing to do
with casts.

So we use a void* pointer to receive pointers to any type. Again,
please notice that C++ doesn't support implicit pointer-cast.

C doesn't support "implicit pointer-cast" either.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html

The fact is the following code compiles as well as I am here:

#include <stdio.h>
#include <malloc.h>

main() {
int* k = malloc(sizeof(int)*10);
k[1] = 10;
k[2] = 10;
printf("%d %d\n", k[1], k[2]);
free(k);
}

.



Relevant Pages

  • Re: sizeof(ptr) = ?
    ... A void * has the same representation as a char *. ... Char pointers which require additional data ... iff the default offset is 0. ...
    (comp.lang.c)
  • Re: Malloc code
    ... int xxx; ... As for not using the void pointer, I will have to do some further testing ... I just needed some insight on passing arrays of pointers. ... struct MCB *r1; ...
    (microsoft.public.vc.language)
  • Re: void pointer arithmetic
    ... As for doing pointer arithmetic on the results: If all the pointers ... static int str_qcmp(const void *a, ... If casts are not no-ops, ... receives struct foo pointers, so it doesn't convert them from void* to ...
    (comp.lang.c)
  • Re: polymorphic design approach
    ... > polymorphism. ... * 'Device' pointers are stored so as to allow polymorphic ... virtual void handleMessage ...
    (alt.comp.lang.learn.c-cpp)
  • Re: On the creation of an API for containers in C
    ... pointers, and these dynamic vectors. ... where 'dyt' was basically a wrapper for an anonymous void pointer. ... cost of this approach), although, if one makes use of the dynamic-type ... typed void pointers' and 'tagged references' (where the value is essentially ...
    (comp.lang.c)