Click to See Complete Forum and Search --> : Incrementing a Void Pointer


j_stanford
July 30th, 2002, 02:34 PM
Is there any way to increment a void pointer? I know you can't do it directly. I was trying something like this, but it didn't work:

void *ptr = &var;
char *c;
c = ptr; //This is where I got an error
c++;
ptr = c;

Does anyone have an idea of how to do this?

John Stanford

jfaust
July 30th, 2002, 02:42 PM
This should compile:

void *ptr = &var;
char *c;
c = (char*)ptr;
c++;
ptr = c;



I give no guarantee that it will work, however.

Jeff

Alexey B
July 30th, 2002, 02:51 PM
Why do you want to increment the void pointer?

stober
July 30th, 2002, 10:01 PM
void *ptr = &var;
char *c;
c = (char*)ptr;
c++;
ptr = c;


That may compile, but as you said it probably won't work because we do not know what kind of thing "var" is. For example, this will not work:


long array[] = {1,2,3,4,5,6};
long *var = array;
void *ptr = (void *)var;
char *c = (char *)ptr;
c++;
ptr = (void *)c;


The reason the above will not work is that the variable c is incremented by only one byte, while we need it to be increment by sizeof(long) (normally 4 bytes).

JMS
July 31st, 2002, 03:10 AM
The reason why you can't increment a void pointer is because the idea is by definition eronious..

for a char pointer incrementing the pointer moves the pointer address one byte.

For a struct pointer where the structure is 100 bytes incrementing the pointer moves the address 100 bytes..

A void pointer which by definition does not have a predefined size so the compiler can't know how far to move the address!! Thus it is not valid to increment a void pointer as part of the language definition. If you know that the void * is actually another type of pointer then by all means caste it to that pointer type and increment away. If you don't know what type the void * actually is, then you must ask yourself...."What the **** am I thinking!!".......

Graham
July 31st, 2002, 03:46 AM
JMS: I would probably take it even further: If you are considering typing the characters 'v', 'o', 'i', 'd' and '*' (in that order) in your code at any point, stop and ask yourself "What the **** am I thinking!!".......

Paul McKenzie
July 31st, 2002, 04:47 AM
Originally posted by Graham
JMS: I would probably take it even further: If you are considering typing the characters 'v', 'o', 'i', 'd' and '*' (in that order) in your code at any point, stop and ask yourself "What the **** am I thinking!!"....... What if you use qsort? (Hint, hint) ;)

Regards,

Paul McKenzie

Graham
July 31st, 2002, 05:16 AM
Don't you start! :)

AnthonyMai
July 31st, 2002, 10:03 AM
Graham said:

JMS: I would probably take it even further: If you are considering typing the characters 'v', 'o', 'i', 'd' and '*' (in that order) in your code at any point, stop and ask yourself "What the **** am I thinking!!".......


Really? Have you not used void* at all in your code? Be honest. Tell us! Have you used void* at all?

Graham, unless you are kidding, [B]you've got to say that to the faces of whoever write the standard C++ library. Because, my gosh, aren't they crazy that they use void* throughout the source code of standard C++ library?

Have you used operator "new". Don't use it any more!!! Look at it. Gosh!!! How terrible it is written! There are at least two void* in just three lines of code: (!!!)

void * operator new( unsigned int cb )
{
void *res = _nh_malloc( cb, 1 );

return res;
}


:-)

Graham
July 31st, 2002, 10:40 AM
You're doing it again, aren't you? Did I say "never"? Point to the bit where I said "never use void* ever in any program that you write".

And, to answer your question, when I used to write C, I used void* quite frequently. In the early days, when I was still coming to grips with C++, I used void* occasionally. These days? Well, not for the past couple of years, and I can't see that I'll need to in the future.

Oh, and the jibe about "new" is irrelevant and innacurate. "Throughout"? I found about 30 occurrences, most of which were of the form "(void *)0" passed to an allocator - and that's a redundant use. there are less than a dozen genuine void*s in there. Even if there were more, it's still irrelevant, since I'm not in the business of writing standard libraries: my code tends to have a well-specified purpose that a standard library cannot have. That extra purpose usually means that I can avoid the use of void*.

Oh, why on earth am I bothering..... forget it.

AnthonyMai
July 31st, 2002, 01:45 PM
Graham said:

You're doing it again, aren't you? Did I say "never"? Point to the bit where I said "never use void* ever in any program that you write".


Graham, you are doing it again, aren't you? Did I used or otherwise meantioned the word never? Did I in any way suggested that you used the word never? Point to the bit in my last message where I said you said "never use void*".

You did not say "never user void*" in the exact word. But you expressed basically the same opinion:

If you are considering typing the characters 'v', 'o', 'i', 'd' and '*' (in that order) in your code at any point, ......

Notice where I highlight the word "ANY". By using ANY, You are suggesting that each and every usage of "void*" indicates a serious problem in design that is worth cursing youself using four letter words. Didn't you?

So you now admit there are at least SOME justified usage of void*, right? So before the OP disclose more detail, I have to assume that the OP probably has his own legitimate reason to use void*, that we may or may not know. I am not going to speculate further. And that's the end of discussion.

AnthonyMai
July 31st, 2002, 02:02 PM
And, graham, you are not telling the truth:

And, to answer your question, when I used to write C, I used void* quite frequently. In the early days, when I was still coming to grips with C++, I used void* occasionally. These days? Well, not for the past couple of years, and I can't see that I'll need to in the future.


Have you not used Win32 APIs laterly. Have you not used any SDK or any third party library laterly? If you are doing ANY useful programming at all, you have used some variety of void*, i.e., pointer to a data type unknown to you.

Have you used a HANDLE or SOCKET or HBITMAP, or something similar? Those are all data types unknown to you. Most of them are actually a void pointer, which points to some internal data structure you don't know and don't need to know. The underneath API functions know what they point to, and can cast them into proper pointers to proper object types. The application code has no idea what they are, But application code can store those void pointers, and pass them back to API.

That shows where usage of void* is great. To application code, the pointer is void*, so it can not do any thing with it except for passing the same pointer back to APIs, to the API function, it is a pointer that points to legitimate objects. The interface and implementation detail are completely separated by the use of void*.

Graham
July 31st, 2002, 04:04 PM
Speak to the hand, the face just can't be bothered listening any more.