regarding free function in c library

Discussion in 'C Programming' started by sam_cit@yahoo.co.in, Dec 15, 2006.

Guest

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?

Advertisements

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
C++ doesn't. To use malloc in C++, you must use explicit typecast, such
as (int*), (void*), etc.
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.
So we use a void* pointer to receive pointers to any type. Again,
please notice that C++ doesn't support implicit pointer-cast.

Advertisements

wrote:
> 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,

....performs an implicit conversion,
> can anyone let me know as to why
> pointers are typecasted to void* in many places before performing
> operation on them?

1. people are stupid

OR

2. people are using a C++ compiler

but I repeat myself

--
Nick Keighley

Quantum Boggum Sort:
Q1. use a source of quantum noise (eg. radioactive decay) to
randomly permutate an array.
Q2. if the array is not ordered, destroy the universe (*)
Q3. if you reached this step your universe has sorted the array
in O(n) time.
(*) [100] this is left as an exercise

wrote:
> 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,

It does not. It performs a /conversion/. A cast (not a "typecast", just
a cast) specifically refers to the syntactic construct `(type) expression`.
> can anyone let me know as to why
> pointers are typecasted to void* in many places before performing
> operation on them?

They're not (mostly because there aren't many operations you can
/do/ on a `void*`). Pointers are /converted/ to `void*`s when the
context expects one and a pointer is provided, and the reason for
doing so is that you can convert a `T*` pointer value to `void*`
and back without loss: `void*` is the "generic pointer type" of C.

Perhaps you should show us an example of what you're asking about.

--
Chris "Perikles triumphant" Dollin
"Who do you serve, and who do you trust?" /Crusade/

wrote:
> > Perhaps you should show us an example of what you're asking about.
>
> Well, i was talking about an example of printing pointers where a cast
> to void* is done explicitly,
>
> char *p;
> printf("address in pointer is %p",(void*)p);
>
> Now, using %p how many bytes would printf try to access?

as many as are in a void* address
> and has the cast to void* got to do anything with it?

wrote:
>> Perhaps you should show us an example of what you're asking about.
>
> Well, i was talking about an example of printing pointers where a cast
> to void* is done explicitly,
>
> char *p;
> printf("address in pointer is %p",(void*)p);

[Note: as written this has undefined behaviour, since `p` hasn't been
given any value.]
> Now, using %p how many bytes would printf try to access? and has the
> cast to void* got to do anything with it?

%p would try to access a void* value, because that's what it's defined
to accept, so it would access as many bytes as a void* has.

The cast to void* is required because that's what %p wants, and %p
wants it because void* is the generic object pointer type.

[I'm sure I remember that void* and char* are required to have the
same representation, but I can't find it in my paper C90 draft.
Bagger.]

Zealot Zuo said:
>
> 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,

No, it doesn't; it performs implicit conversions. A cast is an explicit
conversion.
> It is easy to know why these pointers should be re-typecasted to void*
> calling free() on them.

Note that the term is "cast", not "typecast". Note further that the cast is
not in fact required when passing a pointer value to free().

Nick Keighley said:
> wrote:
>
>> can anyone let me know as to why
>> pointers are typecasted to void* in many places before performing
>> operation on them?
>
> 1. people are stupid
>
> OR
>
> 2. people are using a C++ compiler
>
> but I repeat myself

wrote:
>
>> Perhaps you should show us an example of what you're asking about.
>
> Well, i was talking about an example of printing pointers where a
> cast to void* is done explicitly,
>
> char *p;
> printf("address in pointer is %p",(void*)p);
>
> Now, using %p how many bytes would printf try to access? and has
> the cast to void* got to do anything with it?

Please don't remove attributes for material you quote. But please
do snip signatures, which is everything following the "-- " marker
line.

The %p specifier attempts to access the number of bytes contained
in a void*. This is not necessarily the same as some other pointer
type (but, for backwards compatibility reasons, is the same as in a
char*).

On Fri, 15 Dec 2006 11:35:02 +0000, in comp.lang.c , Chris Dollin
<> wrote:
>
>[I'm sure I remember that void* and char* are required to have the
> same representation, but I can't find it in my paper C90 draft.

6.2.5 p26
"A pointer to void shall have the same representation and alignment
requirements as a pointer to a character type."
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan

On 15 Dec 2006 00:18:30 -0800, wrote in
comp.lang.c:
> 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

There is no such thing as an "automatic cast" in C. The ONLY thing in
C that performs a cast is a cast operator.

There are conversions in C, some of them automatic and some of them
that are only caused by a cast operator.
> pointers are typecasted to void* in many places before performing
> operation on them?

If there is a prototype in scope, there is never a need to cast a
pointer to any object type to pointer to void when calling a
non-variadic function that requires a pointer to void as a parameter.

On 15 Dec 2006 00:38:54 -0800, "Zealot Zuo" <>
wrote in comp.lang.c:
>
> 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.

Jack Klein wrote:
> On 15 Dec 2006 00:38:54 -0800, "Zealot Zuo" <>
> wrote in comp.lang.c:
>
> >
> > 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

You forgot to return a value from main. But if you fix that, add the NULL
check, and replace malloc.h with stdlib.h, you have a fine program there.
Er, so what? Nothing you have written here contradicts what Jack told you.

Mark McIntyre wrote:
> On Fri, 15 Dec 2006 11:35:02 +0000, in comp.lang.c , Chris Dollin
> <> wrote:
>
>>
>>[I'm sure I remember that void* and char* are required to have the
>> same representation, but I can't find it in my paper C90 draft.
>
> 6.2.5 p26
> "A pointer to void shall have the same representation and alignment
> requirements as a pointer to a character type."

Thanks, Mark. Nice to know I wasn't hallucinating.

--
Chris "HO. HO. HO." Dollin
The "good old days" used to be much better.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!