regarding free function in c library

S

sam_cit

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?
 
Z

Zealot Zuo

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.
 
N

Nick Keighley

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
 
C

Chris Dollin

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.
 
S

sam_cit

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?
 
N

Nick Keighley

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.
 
C

Chris Dollin

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.]
 
R

Richard Heathfield

Zealot Zuo said:
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().
 
R

Richard Heathfield

Nick Keighley said:
1. people are stupid

OR

2. people are using a C++ compiler

but I repeat myself :)

Fortunately for my keyboard, I was not drinking coffee.
 
C

CBFalconer

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*).
 
D

Default User

Nick said:
(e-mail address removed) wrote:
2. people are using a C++ compiler


Actually, C++ doesn't require a cast from object pointer to void
pointer, only the other direction. So malloc() requires a cast, but not
free().




Brian
 
M

Mark McIntyre

[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
 
J

Jack Klein

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.
 
J

Jack Klein

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.
 
Z

Zealot Zuo

Jack said:
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


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


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.


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

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);
}
 
R

Richard Heathfield

Zealot Zuo said:

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

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

Non-standard header. Any compiler is entitled to reject this program right
there.
main() {
int* k = malloc(sizeof(int)*10);

Better: int *k = malloc(10 * sizeof *k);
k[1] = 10;

That's unwise. k could be NULL.
k[2] = 10;
printf("%d %d\n", k[1], k[2]);
free(k);
}

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.
 
C

Chris Dollin

Mark said:
[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.
 
D

Dave Thompson

[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."

In C99. In C90 as asked it was 6.1.2.5.

- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top