views:

643

answers:

5

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.. :)

+7  A: 

Hi,

"++" 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.

avp
Oh, I see.. Good good, I think that may be where I am running out of memory haha..By the way, what happens if I typecast the pointer? what size is used? the new cast size?
krebstar
So what if I memcpy(pChar,pInt,2)? What happens? will it be 2 bytes copied from the start of pInt or..?
krebstar
memcpy takes void* parameters, meaning there is no type information, so yes, 2 bytes.
Ryan Fox
Good thanks Ryan
krebstar
+3  A: 

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
1800 INFORMATION
+5  A: 
Vilx-
Not really, I thought things worked like my second example, apparently, not.. So I guess I need to rephrase the question..
krebstar
BTW, which C++ compiler implements "real" references? I mean, not via pointers?
avp
What would be the difference?
Vilx-
Btw, Sorry I think I misled you, my question was "what is the point of pointer types", and not "what is the point of pointers"..
krebstar
Helpful too, but I think avp / Ryan Fox summed it up alraedy.. :) +1 anwyay :)
krebstar
@Vilx- Re: Added 2, I thought much the same way, I checked and double checked that I wasn't reading or writing past the bounds, but for some reason I am getting an access violation on some of my code.. Thanks, will investigate some more..
krebstar
2krebstar: if you don't need random access to elements, try list or map. All these containers are good for adding/removing elements comparing to vector (which is very close to the idea of pointer). And read about iterators. Anyway, pointers are very, very unsafe.
avp
@avp: Oh right! I forgot about those.. :P However, I think it might not be applicable to the current project I am working on.. Plus, vectors/lists have some form of overhead doesnt it? I'm doing something on image processing.. I could be wrong though.. But yeah, I don't need random access..
krebstar
A: 

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

Daniel Paull
For example, dynamic_cast<> may return a different address from that passed in. A typed pointer points to the vtable entries for that type. The cast to void* points you at the start of the vtable, so to compare identity of two abstract objects, one should cast to void* to compare pointer values.
Daniel Paull
+1  A: 

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

avp
Yes, this was what I was precisely trying to do.. However my problem was that the pointers were both pointing to exactly the same size of memory (lets say 100), but the memcpy was incrementing both pointers in different ways.. :) I guess it was all just a matter of precedence.. :)
krebstar