473,471 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

(void*)0 : what is this cast?

alexis4
113 New Member
Hello!

The below sample code is from string.h

Expand|Select|Wrap|Line Numbers
  1. #ifndef NULL
  2. #define NULL    ((void*)0)
  3. #endif

What is this void* cast? Under what circumstances can it be used? Can you please give an example or a link reference?

I thought that I recently saw passing an expression like that as an argument to a function. Is this true?

Thanks!
Oct 15 '11 #1

✓ answered by donbock

The advantage of using a void* for information hiding is more obvious when the object type is a structure. In that case, use of the void* prevents the user from accessing the structure members directly. That leaves you free to delete or rename members as the library evolves.

There are advantages even when the object type is an int. By hiding access to the int you force the user to use the access functions in your library. That leaves you free to alter the scaling or units of the object type. Your access functions can also implement business rules to ensure consistency within and among objects.

Among the disadvantages of this approach is the performance penalty of making a function call instead of accessing the object directly. Not only does the function call and return add overhead, but diverting to another function interferes with the optimizer. Another disadvantage is the function namespace pollution caused by creating names for all those access functions.

11 26478
weaknessforcats
9,208 Recognized Expert Moderator Expert
NULL is used in C as a pointer with a value of 0. That is, a null pointer. void* is a pointer to any type, so (void*)0 is a cast of the integer value 0 to a pointer to any type.

NULL is not used in C++.
Oct 15 '11 #2
donbock
2,426 Recognized Expert Top Contributor
The void* type has many legitimate uses in C and C++ beyond this one example that you noted.

The void* type declares a generic pointer variable. Since the compiler doesn't know what you intend to point at with such a variable, it won't let you dereference the pointer. However, you can pass such a pointer to and from functions.

A C program can use void* pointers to approximate polymorphism and information hiding. C++ probably has better ways to achieve these goals.
Oct 16 '11 #3
alexis4
113 New Member
Thank you both for your answers.

The void* type has many legitimate uses in C and C++ beyond this one example that you noted.
Can you please give me examples of using this or give me references to study this by myself? I am interested on how a type like this can be used as parameter passing. And what is the usage of passing such parameters in functions?

Please note that I don't use C++, so examples on C is my main interest.

Thanks again.
Oct 16 '11 #4
weaknessforcats
9,208 Recognized Expert Moderator Expert
A void pointer is used in C to make up for the lack of no function overloading. Like this:

Expand|Select|Wrap|Line Numbers
  1. void Display(char* c, int number toDsplay);
  2. void Display(int* c, int number toDsplay);
  3. void Display(UserData* c, int number toDsplay);
  4. etc...
You can have only one Display function so you have to do something like this:
Expand|Select|Wrap|Line Numbers
  1. void DisplayChar(char* c, int number toDsplay);
  2. void DisplayInt(int* c, int number toDsplay);
  3. void DisplayUserData(UserData* c, int number toDsplay);
  4. etc...
which is ugly. So you use a void* argument:
Expand|Select|Wrap|Line Numbers
  1. void Display(void* c, int number toDsplay, unsigned int typeid);
  2.  
You have one function again but you now have to pass to the function an additional argument that contains a value the function can use to typecast the void* back to the correct type. The big downside is that every time you add a new type to dsplay you need to revise the Display function.
Oct 16 '11 #5
donbock
2,426 Recognized Expert Top Contributor
FYI, the first code snippet is either what you wish you could accomplish or perhaps it is legal C++ code. Only the last two code snippets are legal C code.
Oct 17 '11 #6
donbock
2,426 Recognized Expert Top Contributor
The following header file illustrates how a void* can be used for information hiding. Suppose you create a library of functions in C for dealing with an object (in the OO sense of the word). Your user needs to pass the object to your library functions. You implement the object type to accomplish its user-scope purposes, but also to make your library as efficient as possible. You don't want those pesky users to fool around directly with your object type. Use void* like this and they can't.

Expand|Select|Wrap|Line Numbers
  1. void *fooConstructor(void);
  2. void fooMethod1(void *p, ...);
  3. void fooMethod2(void *p, ...);
  4. int fooAttribute1(void *p);
  5. float fooAttribute2(void *p);
  6. ... 
  7. void fooDestructor(void *p);
Your library functions cast the void* argument to and from a pointer to the actual object type.
Oct 17 '11 #7
alexis4
113 New Member
OK thank you both again, that made sence... So I wrote the below peace of dummy code:

Expand|Select|Wrap|Line Numbers
  1. void function1 (void *ptr);
  2. uint8 a = 10;
  3. uint8 *p;
  4.  
  5. void main (void)
  6. {
  7.   p = &a;    
  8.   function1(p);
  9. }
  10.  
  11. void function1 (void *ptr)
  12. {
  13.   uint8 *pp;
  14.   pp = (uint8 *)ptr;
  15.   (*pp)++;
  16.   pp = NULL;
  17. }
The result at the end of function1() was:
*p = a = 11, and pointer pp dereferenced (if this terminology is correct but you get the idea).

So about my last "dark spot". I understood about function overloading, but regarding information hiding:

The following header file illustrates how a void* can be used for information hiding ... Use void* like this and they can't.
You gave your explanation donbock, but I didn't quite understand it (and I am terribly sorry about that). When you say "object" in C, you mean structures with function pointers in its members, correct? What must be protected? I mean take your own example:

Expand|Select|Wrap|Line Numbers
  1. void fooMethod1(void *p, ...);
If I know for sure that p is int type (this means that there is no need for function overloading), then why wouldn't I write it as

Expand|Select|Wrap|Line Numbers
  1. void fooMethod1(int *p, ...);
What am I protecting with the first way of function declaration?

If my dummy code isn't dummy enough to explain, feel free to expand this example.

Thanks!
Oct 17 '11 #8
donbock
2,426 Recognized Expert Top Contributor
When I said object I meant ... some arbitrary type that you have decided to only access through functions. It need not contain function pointers.

What must you protect? Anything and everything that you, as designer, have decided should not be directly accessed by the users. If you, as designer, don't care if the users access the object type directly then nothing needs to be hidden.
Oct 17 '11 #9
alexis4
113 New Member
Thanks once more donbock. I got the idea, but I missed the technical explanation. How can

void fooMethod1(void *p, ...);

protect my data accessed inside fooMethod1, while

void fooMethod1(int *p, ...);

cannot?
Oct 17 '11 #10
donbock
2,426 Recognized Expert Top Contributor
The advantage of using a void* for information hiding is more obvious when the object type is a structure. In that case, use of the void* prevents the user from accessing the structure members directly. That leaves you free to delete or rename members as the library evolves.

There are advantages even when the object type is an int. By hiding access to the int you force the user to use the access functions in your library. That leaves you free to alter the scaling or units of the object type. Your access functions can also implement business rules to ensure consistency within and among objects.

Among the disadvantages of this approach is the performance penalty of making a function call instead of accessing the object directly. Not only does the function call and return add overhead, but diverting to another function interferes with the optimizer. Another disadvantage is the function namespace pollution caused by creating names for all those access functions.
Oct 18 '11 #11
alexis4
113 New Member
Thank you donbock, all seems much more clear now!
Oct 18 '11 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: William Payne | last post by:
In the following code, should the code compile without casting &n to void* ? My compiler accepts it, but should it? void foo(void*) { } struct bar { int n;
3
by: Andreas Müller | last post by:
i have a problem with threads and casts, I created a thread pthread_create(&grab_thread, NULL, grab_image, (void*)&camera); and my function is like this: void *grab_image(void *camera) {..} ...
5
by: verec | last post by:
I just do not understand this error. Am I misusing dynamic_cast ? What I want to do is to have a single template construct (with no optional argument) so that it works for whatever T I want to...
15
by: Stig Brautaset | last post by:
Hi group, I'm playing with a little generic linked list/stack library, and have a little problem with the interface of the pop() function. If I used a struct like this it would be simple: ...
29
by: whatluo | last post by:
Hi, c.l.cs I noticed that someone like add (void) before the printf call, like: (void) printf("Timeout\n"); while the others does't. So can someone tell me whether there any gains in adding...
53
by: subramanian100in | last post by:
I saw this question from www.brainbench.com void *ptr; myStruct myArray; ptr = myArray; Which of the following is the correct way to increment the variable "ptr"? Choice 1 ptr = ptr +...
8
by: brad2000 | last post by:
I was doing a little bit of reading in the ISO C spec. about typecasting to a void type. This caused me to have a question. In particular, I'm curious to know about section 6.3.2.2 where the specs...
1
by: alex | last post by:
The following function is quoted from Judy source code: // **************************************************************************** // J U D Y F R E E void JudyFree( void * PWord,...
9
by: jason.cipriani | last post by:
All right, I'm in this weird situation that's hard to explain but I've put together a small example program that represents it. Please bear with the fact that some of the stuff in the example seems...
5
by: zeeshan708 | last post by:
What is the use of a void* pointer? It points to a thing that is void ? So does that mean it is undefined or what? and also if someone can guide me on the following? from Dietel fiftth...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.