Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > regarding free function in c library

Reply
Thread Tools

regarding free function in c library

 
 
sam_cit@yahoo.co.in
Guest
Posts: n/a
 
      12-15-2006
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?

 
Reply With Quote
 
 
 
 
Zealot Zuo
Guest
Posts: n/a
 
      12-15-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) 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.

Sincerely,
Z.Z.

 
Reply With Quote
 
 
 
 
Nick Keighley
Guest
Posts: n/a
 
      12-15-2006
(E-Mail Removed) 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

 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      12-15-2006
(E-Mail Removed) 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/

 
Reply With Quote
 
sam_cit@yahoo.co.in
Guest
Posts: n/a
 
      12-15-2006

>
> 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/


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?

 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      12-15-2006

Zealot Zuo wrote:

> [...] C doesn't support polymorphism.


int i, j, k;
double f, g, h;

i = j + k;
f = g + h;

--
Nick Keighley

 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      12-15-2006
(E-Mail Removed) 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?


I don't understand. Variadic functions don't perform conversions on
pointers, %p requires a void*, therefore an explicit conversion (cast)
must be used.


--
Nick Keighley

 
Reply With Quote
 
Zealot Zuo
Guest
Posts: n/a
 
      12-15-2006

Nick Keighley wrote:
> Zealot Zuo wrote:
>
> > [...] C doesn't support polymorphism.

>
> int i, j, k;
> double f, g, h;
>
> i = j + k;
> f = g + h;
>
> --
> Nick Keighley


....well, well, well, well, at least not for built-in type operators...

Sincerely,
Z.Z.

 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      12-15-2006
(E-Mail Removed) 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.]

--
Chris "Perikles triumphant" Dollin
"Reaching out for mirrors hidden in the web." - Renaissance, /Running Hard/

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      12-15-2006
Zealot Zuo said:

>
> (E-Mail Removed) 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().

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
regarding free function in c library paolo C Programming 23 12-19-2011 08:41 PM
Regarding free function Vijay C Programming 23 06-02-2006 10:19 PM



Advertisments