januw a
januw a

Reputation: 2248

Calculate the offset of the pointer in C++

I am a c++ novice

After I calculated the pointer of the Player structure, the result was beyond my surprise

struct Player
{
    const char* Name = "ab";
    uintptr_t Health = 6;
    uintptr_t Coins  = 3;
} player;



std::cout << &player << std::endl; // 0100C000

uintptr_t* playerBaseAddress = (uintptr_t*)&player;

std::cout << playerBaseAddress << std::endl; // 0100C000
std::cout << (playerBaseAddress + 4) << std::endl; // 0100C010

0100C000+4 How do I get 0100C004 instead of 0100C010

Can someone explain that, please?

Upvotes: 0

Views: 2080

Answers (4)

Olaf Dietsche
Olaf Dietsche

Reputation: 74028

Calculating the offset into a struct can be done by using offsetof, e.g.

#include <cstddef>
#include <iostream>

struct Player {
    const char *name = "ab";
    uintptr_t health = 6;
    uintptr_t coins  = 3;
};

int main()
{
    size_t off = offsetof(Player, health);
    std::cout << "off=" << off << '\n';
}

Will show 4 or 8, depending on the architecture and size of the structure's elements.

Upvotes: 0

freakish
freakish

Reputation: 56467

playerBaseAddress is of type uintptr_t* which is a pointer. Presumably uintptr_t takes 4 bytes in your environment. Now this piece

playerBaseAddress + 4

involves the pointer arithmetic: you move the pointer 4*sizeof(uintptr_t)=4*4=16 bytes forward. 16 in hex is 10. Hence your result.

Note that uintptr_t* playerBaseAddress = (uintptr_t*)&player; is UB anyway. I assume you meant uintptr_t playerBaseAddress = (uintptr_t)&player; instead.

Upvotes: 1

john
john

Reputation: 87959

Like this

uintptr_t playerBaseAddress = (uintptr_t)&player;

In your version you have a pointer, so when you added 4 to your pointer the result was multiplied by the size of the object being pointed at. Clearly on your platform uintptr_t has size 4, so you got 0100C000 + 4*4 which is 0100C010.

This would also work

char* playerBaseAddress = (char*)&player;

because here the size of char is 1. so you get 0100C000 + 1*4 which equals 0100C004.

Upvotes: 2

CoderCharmander
CoderCharmander

Reputation: 1910

In pointer arithmetics, the sizes of the operations are multiplied by the pointed type's size.

This way it's easy to reference data right next to each other in memory.

For example:

int* ptr = new int[5];
ptr[3] = 4;
std::cout << *(ptr+3) << endl; // 4
delete[] ptr;

You could add four bytes to it by converting it to a pointer type which has the size of one byte, for example char*.

Upvotes: 1

Related Questions