whats the significance of void * here

K

kernel.lover

hello,
I want to know if a fuction say malloc is declared as void *malloc()
then whats the significance of void here.
Does void * is used when function has the flexibility to return any
type of value by casting that functions with appropriate data type?
 
T

Thomas Matthews

kernel.lover said:
hello,
I want to know if a fuction say malloc is declared as void *malloc()
then whats the significance of void here.
Does void * is used when function has the flexibility to return any
type of value by casting that functions with appropriate data type?
The "void *" (pointer to void) type is useful when
the type is not known or support for many types is
necessary.

The fundamental purpose of malloc is to allocate memory. It
returns a pointer to that memory. The memory has no type associated
with it; it is just memory. This is the main reason that
malloc's signature uses "void *".

Another handy use of "void *" is in linked lists. The node
contains a pointer to a node and a pointer to data, to be
generic. The issue is that the data can be of any type, so
one cannot use a typed pointer to point to arbitrary data.
Since the data can be of any type, a void pointer is used.
 
J

jacob navia

kernel.lover said:
hello,
I want to know if a fuction say malloc is declared as void *malloc()
then whats the significance of void here.
Does void * is used when function has the flexibility to return any
type of value by casting that functions with appropriate data type?
In C, void means none:

int main(void)

means main has no arguments.

BUT (in C too) void * means a pointer to "anything", an unspecified
value.

void *malloc(size_t);

means that malloc returns a pointer to *any* type.

In other languages like for instance Delphi, void * is implicit.

When a parameter to a function is without type, Delphi understands it as
a pointer to "anything", i.e. to *any* type. This is the same as in C
the void * construct.

It is not necessary to cast a void pointer when you assign it to another
pointer.

char *a = malloc(432);

It is NOT necessary to write:
char *a = (char *)malloc(49876); // Unneeded cast.

jacob
 
M

Michael Mair

kernel.lover said:
hello,
I want to know if a fuction say malloc is declared as void *malloc()
then whats the significance of void here.

First thing: "void *" is not a pointer to "void" but a generic
pointer type which can point to any object of any type.

Returning void * in the case of malloc() has the sense that you
can assign the return value (i.e. the address of the allocated
storage) to any pointer type.(*)

Apart from that, you use void * whenever you just need the address
or some kind of handle for an object but are not interested in
the actual type.

_____
(*) malloc() returns storage that is suitably aligned for any
object.

Does void * is used when function has the flexibility to return any
type of value by casting that functions with appropriate data type?

Functions cannot return arbitrarily typed values.
An additional use of void * is that you have to write algorithms
only once in an abstract way. See, for example, the qsort()
function of the standard library.

void qsort(void *base, size_t nelem, size_t size,
int (*cmp)(const void *e1, const void *e2));

It sorts an array of nelem elements of size size starting
at address base using a user supplied comparison function
pointed to by cmp which compares two elements at the addresses
e1 and e2 returning -1, 0, 1 if the element pointed to by e1
is less than, equal to or greater than the element pointed to
by e2, respectively.
You can sort char arrays, strings (or rather pointers to char),
arrays of int, struct yourstrangetype, ...


Cheers
Michael
 
E

Eric Sosman

kernel.lover said:
hello,
I want to know if a fuction say malloc is declared as void *malloc()
then whats the significance of void here.

`void*' is a data type, just like `int' or `double'.
It happens to be a pointer type, like `char*', but with
a special property that no other data pointer has: you
can convert any legitimate data pointer value to `void*'
and back again without loss of information and without
a cast or any such shenanigans.

This means that `void*' is useful as a "pointer to
anything," or more exactly as a pointer to some data object
whose type is irrelevant at the moment. malloc() cannot
know what data type you intend to store in the allocated
memory, but it doesn't need to: All it needs to know is how
many bytes you require. It finds the bytes and returns you
a pointer to them -- but what kind of pointer should it be?
`char*'? `double*'? `struct something_new_under_the_sun*'?
malloc() doesn't know your intent, so it returns `void*',
the "anything" pointer type.

You can't do very much with the `void*' pointer in its
original form as returned by malloc(), but you can easily
convert it to a pointer to the actual type you want to store:
just write `char *ptr = malloc(42);' or some such. You will
encounter code like `char *ptr = (char*)malloc(42);' but the
cast is unnecessary: when you see such things, you can guess
that they were written by someone whose understanding of
pointers may be uncertain, or by someone who learned his C in
the pre-Standard days when `void*' didn't exist, or by someone
who writes a lot of C++ (the C++ rules are different).

Going the other way, there are functions that operate
on data objects without caring about their type: fwrite(),
for example, doesn't care whether the bytes it writes came
from a `long double' or from an array of `unsigned short'. So
fwrite() accepts a `void*' argument to point to the data; you
can pass it any kind of data pointer and it will silently and
automatically convert to `void*':

long double down_the_left_field_line = 42.0;
fwrite (&down_the_left_field_line, /* etc */);
> Does void * is used when function has the flexibility to return any
> type of value by casting that functions with appropriate data type?

Almost. `void*' can carry any data pointer value, but
not "any type of value" -- a `void*' can carry a `double*'
value, but not a `double' value. Also, `void*' converts to
and from any data pointer type automatically, without any cast
required.
 
L

Lawrence Kirby

In C, void means none:

int main(void)

means main has no arguments.

BUT (in C too) void * means a pointer to "anything", an unspecified
value.

A void * pointer always has type void *, but it can be used to point to an
object of any type. However it doesn't know the type of the object being
pointed to. Functions that use void * e.g. memcpy() or fread() typically
treat the object as a sequence of bytes and neither know nor care about
the actual type. They do typically need to be told the number of bytes in
the object.
void *malloc(size_t);

means that malloc returns a pointer to *any* type.

This could be interpreted in the right way or the wrong way. malloc()
always returns a pointer of type void *. Unless it returns null that
pointer points to a sequence of bytes which may subsequently be treated as
an object of any type that will fit into the number of bytes requested.
In other languages like for instance Delphi, void * is implicit.

When a parameter to a function is without type, Delphi understands it as
a pointer to "anything", i.e. to *any* type. This is the same as in C
the void * construct.

Note that void * is not guaranteed to be able to hold pointers to
functions.
It is not necessary to cast a void pointer when you assign it to another
pointer.

char *a = malloc(432);

By doing a pointer conversion like this you are providing the type
information needed to access the allocated object as the type you want.

Lawrence
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top