Categories > Coding > C++ >

C++ Intermediate Lessons EP: 2 : Memory

Posts: 283

Threads: 48

Joined: May, 2022

Reputation: -4

Posted

Last lesson, we learned how pointers work and the basics of memory, now you may be wondering: Why are pointers so important? Well to answer this question you need to know one fundamental fact:

Everything. Is. Memory.

 

Let's say you're playing some offline game, how does the game keep track of your health? Games like CS:GO use the server for this and that's why you'll never really see a infinite health hack for CS:GO. Offline games for example, keep track of your health in memory, this means that, your health, is somewhere in memory and we can access our health through that memory address: this is where pointers come in.

 

As you know, pointers allow you to directly access specific memory addresses and not only read the values at that memory address, but also change them.

 

Let's say you know the absolute address to your health, an assumption you should make is that if its a 32bit game, it's likely a 32 bit integer, that means in order to get the correct value of our health we need to read exactly 32 bits aka 4 bytes.

 

Health address: 0xA029DE

 

How can we write the next 4 bytes starting from that address? It's quite simple:

 

std::uintptr_t* health = reinterpret_cast< std::uintptr_t* >(  0xA029DE );

*health = 9999;

 

 

( do note that the uintptr_t type IS NOT A POINTER BY ITSELF. The reason it's called ptr_t is because its size is equivalent to the size of a pointer on the current architecture, which will be 4 bytes on 32 bit processes and 8 bytes on 64 bit processes. It's equivalent to std::uint32_t on 32bit and std::uint64_t on 64 bit. )

 

So, let me explain this. You already know how to initialize and dereference pointers but you may be confused about how we are initializing a pointer with the integer 0xA029DE:

What we're doing is type casting a integer to: a pointer to a 32 bit integer.

If you had issues understanding this, read up on C++ casts, you'll be using them often!

 

Note: The reason I'm being so specific about bits and bytes is that in the future when overwriting or reading at memory addresses, you need to know the data type/size of what you're reading. If you want to read a 64-bit number in a 64-bit process, but are making a pointer to that number which it THINKS it points to a 32bit number, you'll only read/overwrite 32 bits out of 64. This is why it's so important to know the size of what you're overwriting/reading. You will get no errors or warnings when game hacking regarding the data types your pointers point to, you need to know that yourself.

  • 2

https://media.discordapp.net/attachments/1044764388546068510/1051935933836050482/Signature_4.png

Alternate

stop take my rice

vip

Posts: 712

Threads: 113

Joined: Mar, 2022

Reputation: 40

Replied

Great tutorial, please make more lol

  • 0

we are dead

eb_

Formally known as Shade

vip

Posts: 1045

Threads: 4

Joined: Jun, 2020

Reputation: 47

Replied

Funny c++ didn't understand a thing but cool. (I read up to csgo inf health)

  • 0

https://media.discordapp.net/attachments/1010636204225601659/1012865624797610044/sKQybOLT.gif

Users viewing this thread:

( Members: 0, Guests: 1, Total: 1 )