Quote:
Originally Posted by charlitos
I was checking out some examples where I noticed there are a few void pointers.
like
then this variable is used to point to anything.
is it ok to do this?
|
It's so-so... It will work, but not recommended, unless there's a special reason. But if you to ask here, you don't have such a special reason.
Quote:
Originally Posted by charlitos
If i know that probably I will need the same variable to point to some reference or the other, I guess it is the right thing to do right?
|
I don't get what you mean by this question.
Quote:
Originally Posted by charlitos
Are there any other steps involved? Like should I try to allocate memory dynamically for this void pointer before I pass a reference to it? Or should I just do it straight forward as in the example.
|
No, you don't need to allocate memery for it if you use it to refer to the value of
data.
Quote:
Originally Posted by charlitos
Are void pointers even a good programming practice at all?
|
No, they are not. But sometimes there is no other way. Well, there actually would be another way. But in those special cases a
char * (or essentially any other type) could as well be used. But the keyword
void makes it more clear that it is a special case for a programmer that reads the code. So, essentially
void* only exist for clarity in cases we would rather not have..
Really, once in memory in the running program, a
pointer to int is no different that a pointer to a 1Mb string or a pointer-to-a-pointer-to-a-struct. They are just all 4-byte numbers (or 8-byte if you have such a fancy Itanium or AMD64 CPU and you are running a 64-bit OS on it). Those 4-byte number refer to an address in memory.
But to the C-compiler it does matter a bit what type a pointer will point to. Not because the compiler needs that, but for programmers it is convenient. If programmers make a mistake and a use a pointer-to-int where it should be a pointer to char for example. The compiler will issue a warning or an error, thus pointing the programmer to a mistake he (may have) made. I the programmer (and the libraries) only used pointers-to-void, the compiler can not know if it is a mistake. Such mistake can be easily made because using pointers can be tricky. So those errors/warnings are A Good Thing. And you only get them if you avoid
void*.
There's another reason for typed pointer ("typed" means: non-void, but int*,char*,...whatever): If you have more of an item (say,
int) in a row (i.e. like a dynamically allocated array), you will probably want to access them with an index, or loop over them some way. E.g.:
Code:
#define NUMBER_OF_INTS 18
int *mypointer, *temppointer;
int i;
mypointer = calloc(NUMBER_OF_INTS, sizeof(int));
temppointer = mypointer;
for (i=0; i<NUMBER_OF_INTS, ++i) {
*temppointer = 3 * i;
++mypointer;
}
In the
bold line the pointer is increase to point to place where the next
int can be stored.
This would go awfully wrong if you would have declared
temppointer as a
void*!
Why?
Because if it was a
void* the compiler would not know it was pointing to
int's and then
++mypointer would increase the pointer by one byte, while
int's are 4 bytes. So 3 bytes of the previous integer would be overwritten by bytes from the second. If you would later print out the int's stored, would get totally wrong numbers and it would be hard to imagine it was your own fault because the code looks deceptively correct. Such an bug in a real world program would be hard to find, or even go undetected until some user or customer complains about wrong and unpredictable results...
And this happened only because the programmer (me, in this case :-) used
void* instead of the correct
int *. With a pointer to
int, the compiler would silently, automatically and conveniently do the right thing and increase the pointer with 4 when it sees "++temppointer", and the code would be correct.
If you think: "hey, void-pointer can point to anything. That is nice! I can use them for anything!" then you are going to wrong way. So, just don't use them, unless... (but then you would know).
Recently I found video's of (C-) programming lectures by Richard Buckland. If you google him, you get the impression he's probably the most popular lecturer/teacher on earth.
Here's a lecture by him about pointers:
http://www.youtube.com/watch?v=Rxvv9krECNw
(not about void pointers though, but pointer in general).