Categories > Coding > C++ >
C++ Intermediate Lessons: EP 1 : Memory
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.
https://media.discordapp.net/attachments/1044764388546068510/1051935933836050482/Signature_4.png
Replied
Vouch underrated sigma
Cancel
Post
https://cdn.discordapp.com/attachments/661621789591470090/1013919752294498314/Untitled_1366_768_px_1546_202_px_2.gif
Replied
thanks this actually helped. can you explain what the -> operator in c++ does because i am stupid
Cancel
Post
"Questionable intellegence, but I like the mystery" - CubeFaces
https://cdn.discordapp.com/attachments/1136067487847415848/1138948596679589898/sig.png
Replied
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
Cancel
Post
https://media.discordapp.net/attachments/1044764388546068510/1051935933836050482/Signature_4.png
"Questionable intellegence, but I like the mystery" - CubeFaces
https://cdn.discordapp.com/attachments/1136067487847415848/1138948596679589898/sig.png
https://media.discordapp.net/attachments/1044764388546068510/1051935933836050482/Signature_4.png
https://cdn.discordapp.com/attachments/978212409145708565/1119225665649115167/WRD_Siggy.png
"Your code looks like a decompiled roblox script" - Me
https://media.discordapp.net/attachments/1044764388546068510/1051935933836050482/Signature_4.png
Replied
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
Cancel
Post
https://cdn.discordapp.com/attachments/978212409145708565/1119225665649115167/WRD_Siggy.png
"Your code looks like a decompiled roblox script" - Me
https://media.discordapp.net/attachments/1044764388546068510/1051935933836050482/Signature_4.png
Replied
@luxiferrwoo ok so like if I have a pointer to a class I can use -> to access the functions
Cancel
Post
Added
@Astronemi ä½ å¥½æˆ‘è§‰å¾—ä½ æ˜¯å¾ˆå‚»çš„ä¸€ä¸ªäººã€‚ æˆ‘å‡¯è¿ªæ‹‰å…‹æ—¶ä»£æƒ é£Žå’Œç•…ä¸Šç的时候我也是醉了😵â€ðŸ’«ï¼Œæˆ‘ä»¬çš„ç”Ÿæ´»æ–¹å¼æ˜¯ä»€ä¹ˆæ—¶å€™å›žåŽ»æ‰èƒ½æ‰¾åˆ°è‡ªå·±çš„ç”Ÿæ´»äº†ï¼Œæˆ‘ä»¬çš„ç”Ÿæ´»æ–¹å¼æ˜¯ä»€ä¹ˆçš„æœ€å¤šä¼šè®©åˆ«äººæ„Ÿåˆ°ç—›è‹¦æ—¶æœ‰å¾ˆå¤šä¸œè¥¿å¯ä»¥ç†è§£ä½ 的想法和好。jjsploit c++ roblox
Cancel
Post
"Questionable intellegence, but I like the mystery" - CubeFaces
https://cdn.discordapp.com/attachments/1136067487847415848/1138948596679589898/sig.png
https://media.discordapp.net/attachments/1044764388546068510/1051935933836050482/Signature_4.png
Replied
yeah.. https://cdn.discordapp.com/attachments/978292534767919134/995354326954426488/funi.png
Cancel
Post
https://cdn.discordapp.com/attachments/978212409145708565/1119225665649115167/WRD_Siggy.png
"Your code looks like a decompiled roblox script" - Me
Replied
Vouch, I'm pretty sure you could even do a YT channel about these things. Big daddy luxifer :hot:
Cancel
Post
Random quote here...
Users viewing this thread:
( Members: 0, Guests: 1, Total: 1 )
Cancel
Post