Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding an offset to a pointer

Tags:

c++

pointers

If I have a pointer to an object and I want to get a pointer to an object that is say 16 bytes after the pointer how do I add the 16 byte offset to the pointer?

Also, memory addresses in 32bit systems look like this 0x00000000. If I change an address like 0x00000001 to 0x00000002 how many bytes are skipped?

like image 260
Tiago Costa Avatar asked Aug 27 '11 01:08

Tiago Costa


People also ask

What does adding to a pointer do C++?

Pointers count bytes, so to point at the next byte you would need to change a pointer's value by 1. Pointer arithmetic, however, counts the objects pointed to by the pointer, and incrementing a pointer increases its value by the size of its pointee type.

Is subtraction allowed on pointers?

The subtraction of two pointers is possible only when they have the same data type. The result is generated by calculating the difference between the addresses of the two pointers and calculating how many bits of data it is according to the pointer data type.

What is offset notation?

Offset binary, also referred to as excess-K, excess-N, excess-e, excess code or biased representation, is a method for signed number representation where a signed number n is represented by the bit pattern corresponding to the unsigned number n + K , K being the biasing value or offset.

Can you printf a pointer?

You can print a pointer value using printf with the %p format specifier. To do so, you should convert the pointer to type void * first using a cast (see below for void * pointers), although on machines that don't have different representations for different pointer types, this may not be necessary.


3 Answers

Pointers count bytes, so to point at the next byte you would need to change a pointer's value by 1. Pointer arithmetic, however, counts the objects pointed to by the pointer, and incrementing a pointer increases its value by the size of its pointee type. If you want to point at bytes, use a char pointer, since char has size 1 by definition, and pointer arithmetic on char pointers is lets you point at bytes:

T * p  = get_pointer();

char * cp = reinterpret_cast<char*>(p);

cp += 16;

Casting pointers to and from char types does not constitute type punning and is explicitly allowed by the standard. However, you must not use the resulting pointer to access any objects that aren't actually at that address.

like image 50
Kerrek SB Avatar answered Oct 22 '22 21:10

Kerrek SB


If I have a pointer to an object and I want to get a pointer to an object that is say 16 bytes after the pointer how do I add the 16 byte offset to the pointer?

Casting through char* will work, but this may be considered bad practice depending on the details of your scenario:

T *ptr = initialize_ptr(); // Do whatever initialization you need to.

ptr = (T*)(((char*)ptr) + 16);

Also, memory addresses in 32bit systems look like this 0x00000000. If I change an address like 0x00000001 to 0x00000002 how many bytes are skipped?

The difference between 2 and 1 is 1 -- exactly one byte would be skipped.

like image 31
cdhowie Avatar answered Oct 22 '22 23:10

cdhowie


You would do this :

char *arr = (char*)((void*) ptrToSomeObject);
&arr[15]

Whats happening under the hood

  1. Any ptr can be converted to 'void*'
  2. 'void *' can be converted to any type of ptr.
  3. arr[15] == (arr+15bytes)
like image 1
Ajeet Ganga Avatar answered Oct 22 '22 23:10

Ajeet Ganga