473,785 Members | 2,738 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

whats the significance of void * here

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?
Nov 14 '05 #1
5 2834
kernel.lover wrote:
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.
Nov 14 '05 #2
kernel.lover wrote:
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
Nov 14 '05 #3
kernel.lover wrote:
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-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #4
kernel.lover wrote:
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_u nder_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(4 2);' 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_f ield_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.

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 14 '05 #5
On Wed, 09 Mar 2005 14:32:25 +0100, jacob navia wrote:
kernel.lover wrote:
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.


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
Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1789
by: Alfonso Morra | last post by:
I have a class that contains a nested class. The outer class is called outer, and the nested class is called inner. When I try to compile the following code, I get a number of errors. It is not obvious to me, where I'm going wrong (the compiler messages do not seem to make much sense). here is the code: outer class declared as ff in "outer.h":
4
2589
by: Prem Mallappa | last post by:
Hello can anybody please tell me what is the significance of 0; 1; or something like 100;
4
1266
by: Frank Rizzo | last post by:
In some examples with inheriting, I see the overloaded function always call its base. For instance, in this case where the ListView control is being extended. public class ListViewEx : System.Windows.Forms.ListView { protected override void OnColumnClick(ColumnClickEventArgs e) { ...various code to extend the control
2
1139
by: Alfonso Morra | last post by:
I have a class that contains a nested class. The outer class is called outer, and the nested class is called inner. When I try to compile the following code, I get a number of errors. It is not obvious to me, where I'm going wrong (the compiler messages do not seem to make much sense). here is the code: outer class declared as ff in "outer.h":
20
2473
by: Snis Pilbor | last post by:
Whats the point of making functions which take arguments of a form like "const char *x"? It appears that this has no effect on the function actually working and doing its job, ie, if the function doesn't write to x, then it doesnt seem like the compiler could care less whether I specify the const part. Quite the opposite, if one uses const liberally and then later goes back and changes the functions, headaches will inevitably occur as...
10
2419
by: pauldepstein | last post by:
I have read about exception handling in a few books and websites but am still confused on a basic point. I understand the try ... catch syntax. However, I have seen examples of using throw where the throw-command is not in a try-block. What is the difference in meaning between throwing in a try block and throwing outside a try block?
3
366
by: sam | last post by:
HI, The code is as follows:- #include <stdio.h> 1 2 void * work(void * ptr) 3 { 4 int i; 5 for (i = 0; i < 10; i++) 6 {
3
2009
by: sam | last post by:
Hello whats the use of void pointers? and when they are useful (on which conditions?) Please give me example code with some explanation. Thanks in advance.
26
1541
by: joe | last post by:
My experiments show that the random number generator in Microsoft's VC++6 compiler is a statistical RNG with a significance level 1.0%. Statistical testing at SL >1.0% (for example 1.001%) passes the test, but 1.0% does not pass... Can anybody confirm this finding? The RNG function of the various SW products can be analyzed and classified better using its significance level
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10319
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10087
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.