Structure Memory Layout?

I'm playing around with a personal project and I had a question about a struct layout in memory. I have a specific structure:

1
2
3
4
5
6
struct Player
{
	int health, mana, experience;
	char playername[20];
	short ID;
};


Now say I have direct access to the a target process' memory and this process keeps the structures side by side.

Can I be guaranteed that the layout of this struct will compile to be:
1
2
1 2 3 4    5 6 7 8    9 10 11 12       13-32       33 34
health      mana      experience    playername      ID


If not, is there any way to make sure the structure is layed out like this?

I basically want to increment a structure pointer and read/write the values.
Can I be guaranteed that the layout of this struct will compile to be:

If not, is there any way to make sure the structure is layed out like this?


no and no.

I basically want to increment a structure pointer and read/write the values.


You can do that without requiring the memory layout be exactly so. Just use a Player* and access its members.
Last edited on
Hmm, let me try to be a little clearer. In my theoretical example; I have a game which contains two players.

1
2
Memory Location: 1 2 3 4    5 6 7 8     9 10 11 12       13-32         33 34
Member:          health      mana      experience    playername        ID

1
2
Memory Location: 35 36 37 38    39 40 41 42     43 44 45 46       47-66         67 68
Member:           health           mana          experience    playername         ID


Now, pretend I inject (gain access to game's address space) this code to execute:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
struct Player
{
	int health, mana, experience;
	char playername[20];
	short ID;
};

void EditHealth(int PlayerNumber) // can be 1 or 2 since there are only 2 players
{
	if ( PlayerNumber != 1 || 2 )
		return;

	Player* pPlayer = (Player*)(1 * (sizeof(Player)*PlayerNumber));

	pPlayer->health = 9001;
	pPlayer->experience = 1000;
	// ...
}
are you talking about editing a memory structure in a seperate program?
Yes

I would like to use my the struct way (My example above).

I know I could just use a void* and calculate offsets but that isn't as pretty :(
afaik there's no way to do this safely with a simple struct. Especially since the struct is aligned oddly (typically they're padded to multiples of 4 bytes, but apparently this struct isn't).

You could put this in a class...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Player
{
public:
    // if you want to go the public member route
    int& health;
    int& mana;
    int& experience;
    char* playername;
    short& ID;

    // ctor
    Player(char* memoryptr)
     : health(     *(int*)(memoryptr +  0 ) )
     , mana(       *(int*)(memoryptr +  4 ) )
     , experience( *(int*)(memoryptr +  8 ) )
     , playername(         memoryptr + 12   )
     , ID(       *(short*)(memoryptr + 32 ) )
    {}

    // assignment operator needs to be rewritten
    Player& operator = (const Player& r)
    {
        health = r.health;
        mana = r.mana;
        experience = r.experience;
        std::copy(r.playername,r.playername + 20,playername);
        ID = r.ID;
    }
     
private:
    // no copy ctor
    Player(const Player&);
};


since this uses references it simulates struct syntax. Example:

1
2
Player player( pointer_to_data );
player.health = 100;  // works as expected 
Hey I'm sorry that I wasn't able to reply faster. Thank you for taking all this time to help me out =D

Greatly appreciated,
- Mack
Ah, just found a way to do this using #pragma directives.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

#pragma pack(push)
#pragma pack(1)
struct x
{
	int i;
	char c[3];
};
#pragma pack(pop)

struct x2
{
	int i;
	char c[3];
};

int main(int argc, char* argv[])
{
	std::cout << "sizeof x = " << sizeof(x) << std::endl
		<< "sizeof x2 = " << sizeof(x2) << std::endl;

	std::cin.ignore();
	return 0;
}


sizeof(x) = 7
sizeof(x2) = 8

Now I have two ways =D
Last edited on
Topic archived. No new replies allowed.