aCuria
aCuria

Reputation: 7205

Void Pointer Arithmetic

Given a void pointer, if I want to make the void pointer point to x bytes ahead, how will this be best done? Is there a better way than casting to a char pointer?

Upvotes: 41

Views: 22163

Answers (7)

Mohamed Hanafy
Mohamed Hanafy

Reputation: 3

you can convert the pointer to normal integer , do the increment or whatever operation , then you can assign the integer to that pointer , hope it helps

Upvotes: 0

Siddhartha Saif
Siddhartha Saif

Reputation: 69

Here was my problem

void *inputAudioBuffer;

c++ was not letting me do this

UInt16 *inputBuffer = (UInt16 *)(inputAudioBuffer + inputBufferOffset);

after doing a type cast I was able to do it

UInt16 *inputBuffer = (UInt16 *)( (UInt16 *) inputAudioBuffer + inputBufferOffset);

Upvotes: -1

rsk
rsk

Reputation: 1

In difference to the other people answering this question, it seems to me as if a simple +x (no casting at all) is sufficient to access the address x bytes ahead of your void pointer. The compiler might not like it, but at least in the cases I have used this, it has worked. I'm using g++ (gcc)...

So as long as you know what you're doing, no probs.

Upvotes: -2

Jaka
Jaka

Reputation: 1209

When doing pointer arithmetic compiler wants to take into account what it knows about the type that the pointer points to.

For example if you have a int *myint. Then these two statements actually do the same thing:

int a = *(myint+5);

and

int a = myint[5];

in this case myint+5 does not mean "the address of myint plus 5" but "the address of myint plus 5*sizeof(int))"

So in case of a void * the compiler can't make any assumptions what void * + 5 should mean. So before you use a void * it kind of forces you to specify how you want to use it.

Upvotes: 2

Stack Overflow is garbage
Stack Overflow is garbage

Reputation: 247969

Given a void pointer, if I want to make the void pointer point to x bytes ahead, how will this be best done? Is there a better way than casting to a char pointer?

If you have a void*, you don't know that "x bytes ahead" is a valid address. You don't know that creating such a pointer won't crash your program.

And that is why it can't be done with void*.

You can only perform pointer arithmetics on pointers into an array. And if you have a pointer into an array, you know the type of the array, and can use the equivalent pointer type.

If you want some kind of abstract "byte pointer" (say, if you're implementing a memory pool and need to point to a specific offset into a buffer), you should use char* or unsigned char*, not void*.

Upvotes: 3

Krevan
Krevan

Reputation: 1681

Is there a better way than casting to a char pointer?

No (except having a char * instead of a void * to begin with, so you don't have to cast it at all).

If this is not desirable or possible, then the only way is:

ptr = static_cast<char *>(ptr) + offset;

(Note: if you are doing this sort of stuff in C++, usually there is a much better solution. Unless you are an expert and you already ruled out every other alternative, I suggest you post a new question asking if there is a better way to do what you're trying to do!)

Upvotes: 26

Matt Joiner
Matt Joiner

Reputation: 118520

Take a look at this question, and this question. To summarise, the answer is to cast to char * for arithmetic at a byte level.

Upvotes: 4

Related Questions