void pointer question

Regarding void pointers, what is their usefulness?
E.g.
1
2
3
4
5
6
7
8
9
10
11
12
13
14

#include<iostream>

int main()
{
	int n = 5;
	void * n_ptr = &n;

	int * i_ptr = static_cast<int*>(n_ptr);
	std::cout<<*i_ptr;
	//n_ptr = n;
	return 0;
}
Using void pointers are untyped, I use them in places where I need to have type flexibility- but don't want the burden of extra allocation.

One example of potential usage could be part of a system where you need to have callback data- you can include a void* member of your event structure and using that void* access any additional data you need, passing in a function pointer that takes three params (int,int,void*) which then gets forwarded the data you need. This allows you to use the existing function instead of having to create a specific function for the type you want to pass in (int,int,bubkiss*)

(you could also use templates in this case, of course)

I found void pointers useful for making functions that can return a variety of data types. For example, you are dealing with binary data from a file that, depending on the position, can be either unsigned short, unsigned long, or ASCII characters. Suppose I create a function that reads a certain number of bytes at a certain position and I want to return it as a proper data type. Of course, you can overload this function for working on different data types but I found that a more elegant solution is to:

1) First create a variable of the required type (for example, "unsigned long val = 0;") - and make sure it is initialized with a value (otherwise, this method produces garbage!);

2) Pass the address of this variable (&val) to the function as a void pointer;

3) In the function, use static_cast to convert the void* to char*, essentially creating a char array at the location of your target variable;

4) Place the binary data as needed into the created char array;

5) When the function completes, you end up with a proper value in val.

Since you cannot safely cast a void pointer you should avoid using it.
The C language lack's most of the toys of C++, but it has void*.

An example of a "generic algorithm" in the C standard library:
http://www.cplusplus.com/reference/cstdlib/qsort/

However, as coder777 implied, the C++ toys are much more safe and simple.


So when is void* useful? When you have to use it. When?
Let say that there is a third-party library that has been written in C, does the exact calculations that you need and uses void* in its interface. You have no choice. The real types that you do use are naturally not void, you merely pass the data incognito.
Wrote a small routine using vgr926's step wise assistance:
This will be as good as it gets until I need to use it. If ever.

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
#include<iostream>

void funct(void * ptr)
{
	
	char * var;
    var = static_cast <char*>(ptr);
    std::cout<<var<<std::endl;

}


int main()
{

	unsigned long val = 110;

	funct(&val);

	val = 99;

	funct(&val);

	return 0;

}



n
c

Topic archived. No new replies allowed.