Categories > Coding > C++ >

C++ Intermediate Lessons: EP 1 : Memory

Posts: 283

Threads: 48

Joined: May, 2022

Reputation: -4

Posted

This is episode 1 to my Intermediate C++ lessons, these are intermediate due to the fact I want to be teaching people who are having a tough time grasping concepts such as how memory works and pointers as I understand it can be confusing for some, therefore I won't be teaching the absolute basics such as functions and variables and such.

 

( Please do note this DOES require you to have some basic C++ knowledge )

 

https://media.discordapp.net/attachments/959333771247779860/1014658530244046848/pm.png

 

This is a visual representation of an array of integers, an array is a contiguous block of memory of homogeneous objects, as you ( should ) already know, you can access an array using the subscript operator ( [ ] ). 

 

[b]int [/b]variables are 4 bytes, this does not change whether or not you're in x64 or x86. This applies to MSVC and CLANG, if you're on a different compiler or different operating system ( I'm on windows ), I cannot guarantee this.

 

Now that you know that int variables are 4 bytes, you can then deduce that the size of an array of integers is n * sizeof( int ), n being how many integers you decided to allocate ( e.g if you have an array of 3 integers, the calculating will be 3 * sizeof( int ) ). 

 

Before we get in too deep, let's define a few things:

A byte is made up of 8 bits, a bit is a binary value aka it can only be 1 or 0, a bit that is set to 0 means that bit is unset, and a bit that is set to 1 means that bit is set.

 

4 bytes correspond to 32 bits( 4 * 8 ), 8 bytes correspond to 64 bits ( 8 * 8 ) and so on.

 

Memory addresses are simply numbers which tell you the address ( picture it as a home address. Your home address defines where you live ) of a value

 

Moving forward:

 

In C++ there is a feature called: Pointers

Pointers are variables which store ( point to) the memory address of a value and allow you to access that value through said memory address, pointers can bring performance gains ( in C++ you'll often be using references for this specifically but that is a topic for another day ), and are especially useful in our community, entire game cheats essentially revolve around pointers.

 

The difference between a pointer and a integer variable which stores an address, is the fact that the compiler will treat a pointer AS A POINTER. This means the compiler will allow you to perform operations on a pointer that you cannot perform on a normal variable such as: dereferencing:

 

                      // hexadecimal number.

int some_int = 0x1892A;

*some_int; // will give you some compiler error saying the dereference operator cannot be used on a nonpointer variable.

 

So, how can we declare and define a pointer? It's quite simple:

 

int some_int = 10;

int* some_pointer = &some_int;

*some_pointer = 20;

 

 

Let's me explain this line by line:

In line one, we are defining a integer variable called some_int which holds the value 10. 

In line two, we are defining a pointer variable called some_pointer which holds the address of some_int ( & is the address of operator when used like that, right behind a variable name ), and right after declaring the data type, we add a asterisk to indicate to the compiler that some_pointer is a pointer.

 

In line 3 we tell the compiler using the asterisk again ( only works on pointer types when used like that ) right behind the variable name to dereference the memory address pointed to by the pointer, and set the next 4 bytes starting at that memory address to the 4 bytes that the number 20 consists of. ( Note that I said it will set the next 4 bytes at that memory address because integers are by default 4 bytes )

 

If you output the value of some_int after that, you will see that it changed to 20. 

 

Double pointers are just as simple as normal pointers, they're just pointers that point to pointers and you can keep adding on, triple pointers, quadruple pointers, quintuple pointers etc. whatever your heart desires.

 

If you enjoyed this lesson let me know and ask me any questions in the comments! Next lesson we'll be diving deeper into how we can use pointers to our advantage.

  • 0

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

pepsi

PePsIDeveloper

vip

Posts: 309

Threads: 6

Joined: Apr, 2021

Reputation: 16

Replied

Vouch underrated sigma

  • 0

https://cdn.discordapp.com/attachments/661621789591470090/1013919752294498314/Untitled_1366_768_px_1546_202_px_2.gif

SeizureSalad

i love femboys

Posts: 1159

Threads: 79

Joined: Mar, 2021

Reputation: 40

Replied

thanks this actually helped. can you explain what the -> operator in c++ does because i am stupid

  • 0

"Questionable intellegence, but I like the mystery" - CubeFaces

https://cdn.discordapp.com/attachments/1136067487847415848/1138948596679589898/sig.png

Posts: 283

Threads: 48

Joined: May, 2022

Reputation: -4

Replied

@SeizureSalad

 

When you have a pointer to a class object, C++ let's you use the -> operator to dereference the pointer and then access it like normal, it's the equivalent of doing (*class_ptr).member

  • 0

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

SeizureSalad

i love femboys

Posts: 1159

Threads: 79

Joined: Mar, 2021

Reputation: 40

Replied

@luxiferrwoo so basically a shortcut to use a pointer like normal i see

  • 0

"Questionable intellegence, but I like the mystery" - CubeFaces

https://cdn.discordapp.com/attachments/1136067487847415848/1138948596679589898/sig.png

Posts: 283

Threads: 48

Joined: May, 2022

Reputation: -4

Replied

@SeizureSalad

 

a pointer to a class object specifically, do not forget that.

  • 0

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

Posts: 1430

Threads: 71

Joined: May, 2022

Reputation: 20

Replied

bro I dont know indian

  • 0

i use arch btw

Posts: 283

Threads: 48

Joined: May, 2022

Reputation: -4

Replied

@Whoman

 

what lol ( if this is a joke that's wtv but i aint indian lmao )

  • 0

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

Posts: 1430

Threads: 71

Joined: May, 2022

Reputation: 20

Replied

@Astronemi

no im saying that because I dont know indian and this is so hard to understand it seems like another language (indian) I was gonna come up with something else but I was lazy

  • 0

i use arch btw

Posts: 283

Threads: 48

Joined: May, 2022

Reputation: -4

Replied

@atari

already posted!

  • 0

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

SeizureSalad

i love femboys

Posts: 1159

Threads: 79

Joined: Mar, 2021

Reputation: 40

Replied

@luxiferrwoo ok so like if I have a pointer to a class I can use -> to access the functions

  • 0

Added

@Astronemi 你好我觉得你是很傻的一个人。 我凯迪拉克时代惠风和畅上班的时候我也是醉了😵‍💫,我们的生活方式是什么时候回去才能找到自己的生活了,我们的生活方式是什么的最多会让别人感到痛苦时有很多东西可以理解你的想法和好。jjsploit c++ roblox

  • 0

"Questionable intellegence, but I like the mystery" - CubeFaces

https://cdn.discordapp.com/attachments/1136067487847415848/1138948596679589898/sig.png

Posts: 283

Threads: 48

Joined: May, 2022

Reputation: -4

Replied

@SeizureSalad

 

yes, and to access member variables aswell.

  • 0

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

Posts: 1430

Threads: 71

Joined: May, 2022

Reputation: 20

Replied

@Astronemi

yeah.. https://cdn.discordapp.com/attachments/978292534767919134/995354326954426488/funi.png

  • 0

i use arch btw

Posts: 2014

Threads: 198

Joined: Apr, 2021

Reputation: 16

Replied

Vouch, I'm pretty sure you could even do a YT channel about these things. Big daddy luxifer :hot:

  • 0

Random quote here...

Next >>>

Users viewing this thread:

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