Question

Let's say I have some pointers called:

char * pChar;
int * pInt;

I know they both simply hold memory addresses that point to some other location, and that the types declare how big the memory location is pointed to by the particular pointer. So for example, a char might be the size of a byte on a system, while an int may be 4 bytes.. So when I do:

pChar++; // I am actually incrementing the address pointed to by pChar by 1 byte;
pInt++; // I am actually incrementing the address pointed to by pInt by 4 bytes;

But what if I do this:

pChar+2; // increment the address pointed to by pChar by 2 bytes?
pInt+2; // increment the address pointed to by pInt by 2 bytes? what happens to the other two bytes?

Thanks.. Would appreciate any clarification here.. Is the pointer type simply for the ++ operation?

EDIT: So avp answered my question fittingly, but I have a follow up question, what happens when I do:

memcpy(pChar,pInt,2);

Will it copy 2 bytes? or 4 bytes? Will I have an access violation?

EDIT: THe answer, according to Ryan Fox, is 2 bytes, because they are typecasted to a (void*). Thanks! CLOSED!

EDIT: Just so that future searchers may find this.. Another piece of info I discovered..

memcpy(pChar+5,pInt+5,2);

doesnt copy 2 bytes of the memory block pointed to by pInt+5bytelocations,to pChar+5bytelocations.. what happens is that 2 bytes are copied to pChar+5bytelocations from pInt(4*5)bytelocations.. no wonder I got access violations, I was trying to read off somewhere I wasn't supposed to be reading.. :)

Was it helpful?

Solution

"++" is just another name for X = X + 1;

For pointers it doesn't matter if you increment by 1 or by N. Anyway, sizeof(type)*N is used. In the case of 1 it will be just sizeof(type).

So, when you increment by 2 (your second case):
for char is 2*sizeof(char)=2*1=2 bytes,
for int will be 2*sizeof(int)=2*4=8 bytes.

OTHER TIPS

Ahh, now I understand. You should have asked - "What is the point of pointers having types?"

There are two points, actually:

  • Pointer arithmetics;
  • Dereferencing (getting the value back that is stored in the address that the pointer is pointing to).

Both would be impossible without knowing the type of the pointer.

Added: Read the documentation of memcpy. The last argument is number of bytes, because memcpy has no idea what the type of the pointer is. Both arguments to it are void pointers.

Added 2: Access violation - it depends. If you aren't going outside of the memory that you have allocated for these pointers, there will be no access violation. The copy operation will copy everything byte-by-byte and you will get your results just like you expect them (although it might not make much sense).

If you are going outside your allocated memory bounds then you might get an access violation, but you might as well just cross over into the memory that was allocated for another variable. It's pretty much impossible to tell what gets where when your program is executed, so doing this will lead to quite unpredictable results.


There are three main advantages of pointers:

  1. You can pass arguments to function "by reference". This used to be more of an issue in C, which didn't have real references like C++, but it's still very useful in many cases, like when you have to cooperate with external libraries. Also notice, that passing by reference is not only useful when you want the function to modify the variable you're passing. It's also very good for passing large data structures as parameters.
  2. For building all kinds of nifty dynamic data structures like trees, linked lists, etc. This would be impossible without pointers.
  3. For being able to re-allocate arrays to bigger/smaller ones as needed.

P.S. I understand that the question was about why pointers are good, using the arithmetics only as an example, right?

Pointer arithmetic doesn't work precisely that way. Your first example is correct, the second not so much.

pChar+2; // increment the address pointed to by pChar by 2 bytes
pInt+2; // increment the address pointed to by pInt by 8 bytes

For this part:

memcpy(pChar+5,pInt+5,2);

First, "+" is evaluated, then, typecast.

So in bytes:

pChar+5 here "5" is 5 bytes,
pInt+5 here "5" is 5 ints, so 5 * 4 = 20 bytes.
Then everything is cast to void* and two bytes copied.

If instead of "5" you use counter, like here:

for (int i = 0; i<100; i++)
    memcpy(pChar+i, pInt+i, 2);

Then for pChar you will be overwriting one copied byte (the second) with the next copy command. And for pInt you will be jumping 4 bytes each step (which is ok for array of ints though).

I would have said that the point of pointer types in C++ is to account for vtable offsets.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top