Categories > Coding > C++ >

C++ Intermediate Lessons: EP 1 : Memory


luxiferrwoo

exprssn#6408

Posts: 233

Threads: 42

Joined: May, 2022

Reputation: 16

  • 0

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.

14 | [c, cpp, lua, python]

! exprssn.sentiment#9999 | https://github.com/expressiongz

 

pepsi

PePsIDeveloper

vip

Posts: 317

Threads: 4

Joined: Apr, 2021

Reputation: 8

  • 0

Replied

Vouch underrated sigma

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

SeizureSalad

i love femboys

Posts: 964

Threads: 58

Joined: Mar, 2021

Reputation: 24

  • 0

Replied

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

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

https://cdn.discordapp.com/attachments/908778288426651739/928478435066642492/siggy_1.png

luxiferrwoo

exprssn#6408

Posts: 233

Threads: 42

Joined: May, 2022

Reputation: 16

  • 0

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

14 | [c, cpp, lua, python]

! exprssn.sentiment#9999 | https://github.com/expressiongz

 

SeizureSalad

i love femboys

Posts: 964

Threads: 58

Joined: Mar, 2021

Reputation: 24

  • 0

Replied

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

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

https://cdn.discordapp.com/attachments/908778288426651739/928478435066642492/siggy_1.png

luxiferrwoo

exprssn#6408

Posts: 233

Threads: 42

Joined: May, 2022

Reputation: 16

  • 0

Replied

@SeizureSalad

 

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

14 | [c, cpp, lua, python]

! exprssn.sentiment#9999 | https://github.com/expressiongz

 

Whoman

#3561

Posts: 1004

Threads: 49

Joined: May, 2022

Reputation: 11

  • 0

Replied

bro I dont know indian

https://i.imgur.com/21V9TOF.png

luxiferrwoo

exprssn#6408

Posts: 233

Threads: 42

Joined: May, 2022

Reputation: 16

  • 0

Replied

@Whoman

 

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

14 | [c, cpp, lua, python]

! exprssn.sentiment#9999 | https://github.com/expressiongz

 

ThatPhoenix

Dopemine Addict

Posts: 2926

Threads: 63

Joined: May, 2020

Reputation: 26

  • 0

Replied

@luxiferrwoo

Hes saying that because theres a lot of indian guys on youtube teaching valuable stuff, and ur teaching valuable stuff...

 

p.s very hot lesson 

I have left WRD,

I'm always available at Astronemi#8419

(yes that's my new username)

Whoman

#3561

Posts: 1004

Threads: 49

Joined: May, 2022

Reputation: 11

  • 0

Replied

@ThatPhoenix

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

https://i.imgur.com/21V9TOF.png

luxiferrwoo

exprssn#6408

Posts: 233

Threads: 42

Joined: May, 2022

Reputation: 16

  • 0

Replied

@atari

already posted!

14 | [c, cpp, lua, python]

! exprssn.sentiment#9999 | https://github.com/expressiongz

 

ThatPhoenix

Dopemine Addict

Posts: 2926

Threads: 63

Joined: May, 2020

Reputation: 26

  • 0

Replied

@Whoman

should of said chinese lol

I have left WRD,

I'm always available at Astronemi#8419

(yes that's my new username)

SeizureSalad

i love femboys

Posts: 964

Threads: 58

Joined: Mar, 2021

Reputation: 24

  • 0

Replied

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

  • 0

Added

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

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

https://cdn.discordapp.com/attachments/908778288426651739/928478435066642492/siggy_1.png

luxiferrwoo

exprssn#6408

Posts: 233

Threads: 42

Joined: May, 2022

Reputation: 16

  • 0

Replied

@SeizureSalad

 

yes, and to access member variables aswell.

14 | [c, cpp, lua, python]

! exprssn.sentiment#9999 | https://github.com/expressiongz

 


Next >>>

Users viewing this thread:


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