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

 
 
paolo
Guest
Posts: n/a
 
      12-17-2011
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
 
 
 
 
paolo
Guest
Posts: n/a
 
      12-17-2011
**BUMP**

Anyone is in this room ???


On Sat, 17 Dec 2011 22:20:16 +0000, paolo 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?


 
Reply With Quote
 
 
 
 
Lew Pitcher
Guest
Posts: n/a
 
      12-17-2011
On December 17, 2011 17:30, in comp.lang.c, wrote:

> **BUMP**
>
> Anyone is in this room ???


You should be aware that usenet is a /very/ asynchronous process; it takes
appreciable time for posts to propogate from server to server (sometimes
days), and /even more/ time for people to pull posts down from their server
and read them. Often, the response will involve a lot of detail, which
takes time to collect and transcribe.

In other words, be patient, little one.

>
> On Sat, 17 Dec 2011 22:20:16 +0000, paolo 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?



The C standard requires void pointers (that is, pointer to void) in a number
of places where the logic requires that such pointer refer to one of a
number of different types, depending on other indicators. (the argument
pointers passed to printf() and scanf() come to mind here). When prototyped
functions accept void pointers, the compiler converts from typed pointer to
void pointer as part of the function call sequence. Similarly, when
prototyped functions return a void pointer, the compiler converts to typed
pointer as part of the function return sequence.

AFAIK, there is no requirement in the standard that the programmer
explicitly cast typed pointers to void pointers. I may be wrong here, and I
expect those with better knowledge of the standard to correct me.

However, other languages (read C++) /do/ have requirements for explicit
casts of typed pointers to void pointers. Programmers used to working with
those languages often mistakenly cast pointers to void* in their C code.

Perhaps, this is the behaviour that you have noticed.

HTH
--
Lew Pitcher

 
Reply With Quote
 
Lew Pitcher
Guest
Posts: n/a
 
      12-17-2011
On December 17, 2011 17:47, in comp.lang.c, wrote:

> On December 17, 2011 17:30, in comp.lang.c,
> wrote:

[snip]
>>> 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?

>

[snip]
> Similarly, when prototyped functions return a void pointer, the compiler
> converts to typed pointer as part of the function return sequence.


I should correct this; the conversion happens on assignment, not on function
return.

Thus, in
double *mynumber;
mynumber = malloc(sizeof *mynumber);
the conversion of the void pointer (void *) returned by malloc() happens
when the value of that pointer is assigned to the double pointer (double *)
mynumber.

--
Lew Pitcher

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-18-2011
paolo <> writes:
> **BUMP**
>
> Anyone is in this room ???
>
>
> On Sat, 17 Dec 2011 22:20:16 +0000, paolo wrote:

[snip]

This is not a "room". This is Usenet. Messages do not appear
immediately because there is no single server; rather, messages are
forwarded across a network of servers. It's not reasonable to expect to
get answers within 10 minutes of posting. If you don't get a response
within a couple of days, it *might* be time to start being concerned.

Another note: top-posting is discouraged. Suggested reading:
http://www.caliburn.nl/topposting.html
http://www.cpax.org.uk/prg/writings/topposting.php

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-18-2011
paolo <> writes:
> 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?


It's a conversion, not a "typecast". A cast is an explicit
operator, consisting of a type name in parentheses, that specifies a
conversion. A conversion can be either explicit (by means of a cast)
or implicit. ("Typecasting" is something that happens to actors.)

The reason for the implicit conversion is that, as you say, free()
takes an argument of type void*. It cannot operate directly on,
for example, a pointer of type int*. The language supports implicit
conversions from most pointer types to void* (and vice versa) for
the sake of convenience. The language additionally guarantees
that converting a pointer to void* and then back again yields
the original value.

(None of this applies to pointer-to-function types.)

void* is effectively a generic pointer type.

In the absence of the implicit conversion, you'd have to do it
explicitly:

int *ptr = (int*)malloc(sizeof *ptr);
...
free((void*)ptr);

as opposed to the simpler:

int *ptr = malloc(sizeof *ptr);
...
free(ptr);

There's no real type safety issue in these particular cases, so the
increased convenience is worthwhile.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
BartC
Guest
Posts: n/a
 
      12-18-2011
"Keith Thompson" <kst-> wrote in message
news:...
> paolo <> writes:
>> 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?

>
> It's a conversion, not a "typecast". A cast is an explicit
> operator, consisting of a type name in parentheses, that specifies a
> conversion. A conversion can be either explicit (by means of a cast)
> or implicit. ("Typecasting" is something that happens to actors.)


"In computer science, type conversion, typecasting, and coercion are
different ways of, implicitly or explicitly, changing an entity of one data
type into another."

(http://en.wikipedia.org/wiki/Type_conversion)

It may not appear in the C standard, but since this isn't a showbiz forum,
everyone knows what is meant.

--
Bartc

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      12-18-2011
On 12/18/2011 8:59 AM, BartC wrote:
>
> "In computer science, type conversion, typecasting, and coercion are
> different ways of, implicitly or explicitly, changing an entity of one
> data type into another."
>
> (http://en.wikipedia.org/wiki/Type_conversion)
>
> It may not appear in the C standard, but since this isn't a showbiz
> forum, everyone knows what is meant.


"The free encyclopedia that anyone can edit," whether he knows
anything or not.

--
Eric Sosman
d
 
Reply With Quote
 
Bill Reid
Guest
Posts: n/a
 
      12-18-2011
On Dec 18, 6:08*am, Eric Sosman <esos...@ieee-dot-org.invalid> wrote:
> On 12/18/2011 8:59 AM, BartC wrote:
>
>
>
> > "In computer science, type conversion, typecasting, and coercion are
> > different ways of, implicitly or explicitly, changing an entity of one
> > data type into another."

>
> > (http://en.wikipedia.org/wiki/Type_conversion)

>
> > It may not appear in the C standard, but since this isn't a showbiz
> > forum, everyone knows what is meant.

>
> * * *"The free encyclopedia that anyone can edit," whether he knows
> anything or not.
>

The same concept that Usenet is based on!

---
William Ernest Reid
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      12-18-2011
On 12/17/2011 05:47 PM, Lew Pitcher wrote:
....
> AFAIK, there is no requirement in the standard that the programmer
> explicitly cast typed pointers to void pointers.


That's correct; the conversion is implicit in an assignment expression,
and therefore also when a pointer is passed as one of the arguments
listed in a function prototype that is in scope at the point where the
function is called, and also in the expression part of a return
statement: in both cases the behavior is defined by cross-referencing
the rule for simple assignment.
--
James Kuyper
 
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
free free 100 dollors free 4days only FRee REGISTER ONLy h Computer Support 0 04-17-2008 07:43 AM
free free 100 dollors free 4days only FRee REGISTER ONLy h Digital Photography 0 04-17-2008 07:40 AM
regarding free function in c library sam_cit@yahoo.co.in C Programming 19 01-03-2007 07:41 PM
Regarding free function Vijay C Programming 23 06-02-2006 10:19 PM
I got a good news here. Free guest book, free URL direct, free counter, free minipoll and much more!!! Why don't you join? cyx.thunderfoot@gmail.com HTML 1 07-06-2005 12:25 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57