Re: regarding free function in c library
- From: "Zealot Zuo" <zealotzuo@xxxxxxxxx>
- Date: 15 Dec 2006 22:23:26 -0800
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);
}
.
- Follow-Ups:
- Re: regarding free function in c library
- From: Richard Heathfield
- Re: regarding free function in c library
- References:
- regarding free function in c library
- From: sam_cit
- Re: regarding free function in c library
- From: Zealot Zuo
- Re: regarding free function in c library
- From: Jack Klein
- regarding free function in c library
- Prev by Date: Re: tricky array problem !
- Next by Date: Re: regarding free function in c library
- Previous by thread: Re: regarding free function in c library
- Next by thread: Re: regarding free function in c library
- Index(es):
Relevant Pages
|