40

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?

Ziezi
  • 6,375
  • 3
  • 39
  • 49
aCuria
  • 6,935
  • 14
  • 53
  • 89
  • I have asked a question with wider scope of what i am trying to do here. I would like to know if there is a better way of doing it, the idea of casting the void* to a char seems like a hack to me. http://stackoverflow.com/questions/3378090/custom-memory-manager – aCuria Jul 31 '10 at 12:35
  • aCuria I shared the feeling. Hence the 2 questions that already exist about this, that I've linked in my answer. – Matt Joiner Jul 31 '10 at 13:13
  • If you have a mostly C program that just happens to use some C++ features, the compiler will complain about pretty much every void* in your program. In my opinion, the compiler is then doing something wrong, not the programmer. So a workaround is necessary, not a complete program redesign. – Minthos Jul 06 '13 at 12:38

7 Answers7

26

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!)

Krevan
  • 1,611
  • 1
  • 14
  • 12
  • 2
    As you suggested, i have posted another question here http://stackoverflow.com/questions/3378090/custom-memory-manager – aCuria Jul 31 '10 at 12:33
3

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

Community
  • 1
  • 1
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
3

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*.

jalf
  • 243,077
  • 51
  • 345
  • 550
2

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.

Jaka
  • 1,205
  • 12
  • 19
  • 1
    It could just assume sizeof(void) = 1 like the C compiler does. But of course that would be too easy. – Minthos Jul 06 '13 at 12:43
0

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

-1

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);
Enamul Hassan
  • 5,266
  • 23
  • 39
  • 56
-2

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.

Boeckm
  • 3,264
  • 4
  • 36
  • 41
rsk
  • 1
  • 2
    Arithmetic on void pointers is not defined in C++. `void` is an incomplete type, and pointer arithmetic only works for completely defined types. – Mat Nov 03 '12 at 15:25