Couple of years ago, when I tried to understand this, I told my teacher at school: "So pointer is like a shortcut on a desktop". And he started to yell with excitement: "Thats the best analogy I have heard"
I agree, they talk about houses with addresses - yeah easy. But what we want is practical examples of real code using pointers and leave out the simple examples because they really are too easy.
@@user_375a82 Imagine having to move an entire house from one address to another... vs just telling some process/function to refer to a different house (address) instead. What do you think is going to be easier for the computer to do?
I would like to mention an interesting behavior of pointers. If a pointer is incremented, e.g. ptr++; , then the address it holds is not always incremented by one byte. Instead it is incremented by the size of its type. Thus, a 4 byte integer pointer 0x00000004, would become 0x00000008, if you type ptr++;
Of course it's incremented by the size of the type. After all that's what the types are for, to determine how many bytes are occupied by an object and that's one of the reasons why statically typed languages are faster :)
I'm crying in relief, I've been struggling to understand what are pointers and how they work for MONTHS because my C++ class is not the best for newbs like me, and you just clarified everything in less than 20 minutes!!! THANK YOU!!
*My takeaways:* 1. A pointer is an integer that stores a memory address 1:10 2. Void pointer 3:55 3. Dereferencing a pointer 9:25 4. A pointer to another pointer 14:15
I know this comment thread is kinda old but if anyone saw this please elaborate myself a little bit. After I finished the clip, I went on to try using a pointer that points to the address of a variable of string data type. But when I declare my pointer data type as integer, the compiler gave out warning that it cannot convert from string to int. I then change the pointer data type to string then it works. So I reckon either Cherno talking about int data type just in this context, but generally pointer data type should be the same with it's address variable's data type
I already took 2 semesters of c++ focused computer science classes at univ but I love the way you explain things it’s both entertaining and makes you think a bit different
One of my professors gave me your channel and said it was a huge help to him when he was first learning C++. This is the first video of yours I've watched (he told me to study up on pointers) and you explain things very well and thoroughly... I'll definitely watch more of your teaching videos. Thank you.
@@Gaelrenaultdu06 Yeah... that doesn't add up. Probably meant "instructor". Students often use the term professor when they mean instructor at a college. Really, a "professor" would have at least a doctorate, and a doctor of computer science would not have been introduced to C++ within the last 4 years.
@@bradleyfallon6847 Who knows... there are hundreds of programming languages, and someone with a doctorate isn't necessarily going to focus on learning a big part of them.
For anyone struggling with pointers, I made up an analogy that helped me learn. Hopefully it helps you understand! Let's say you have a bunch of warehouses in an industrial district, and each one of these warehouses (memory storage) has a different material stored in them (this is the variable). To find a warehouse with say, wood (a variable), you would need to search up the address (memory address) of where the actual WAREHOUSE that stores wood is. So you could go there and pick up the wood(the variable memory), and build a house with it (memory modification, or modification of where the variable data is stored).
Bullshit. You should get your overthinking head out from here. I’m sure you never did anything significant in c++ if that’s the thing you say to anyone struggling with pointers. Fuckn go away.
I'm watching this again 4 years later... still the best video on pointers, everything becomes clear and understandable. I was trying to learn Rust, but nobody can compare to The Cherno's teaching skills on youtube. May just end up sticking with C++ because he makes it so easy!
I really like how you state the use of your next explanation before the actual explanation. Ex. "Then how do we access the data?"...."We do ____." This preps my brain to make space for the answer because I know what its for. Too many teachers and professors will say: "we can do ____"..... "This lets us access the data." Simple difference, but its the difference between "oh this is the answer let me pay attention" and "oh sht that was important wait what did he say" Also, the way you keep iterating important points and continually connect previous points allow me to connect all the things Ive been listening to so I can organize them in my head. Thanks.
Just as memory is everything for programming. Your correct and precise pronunciation of words, as well as the proper cadence you use when speaking, is everything. Especially for those of us who are native English speakers. For which I thank you. I have been writing code in C++ for more than 10 years and I like to review, learn and above all... absorb new resources to teach. And in you I have found a great source. Greetings from Argentina. Pablo
As someone that hasn't programmed in 11 years(and that was in python) trying to learn c++ you have by far made the best course I've took so far and I've took quite a few on udemy....so thanks for breaking it down super simple 😄
@@_Omni I make a meal for my friends, try to make it delicious, try to keep it nutritious, make wonderful dishes. Nobody thinks about the way i feel, none of them compliment the meal!
Amazing work breaking down for us things like that. I love the way you are pushing us to play with memory addresses and kind of reverse engineer how stuff works rather than keeping it shallow like most other videos out there. Great work keep it up
Thanks for your excellent video! I’m just getting going in C++ using C++ Primer and your tutorials. They compliment each other well. Your explanation of pointers was clearer than all of the others I have read or heard. Cheers!
You are a good human :) I can understand pointers now! I see the point, it's like knowing where you left your keys, if you know where you left your keys you can access your car, and a pointer is where you left your keys.
Great video, just one thing: int var = 8; int* ptrJ = &var; // ptrJ point to the same memory address that contains 08 00 00 00 double* ptrD = (double*)&var; // ptrD point to the same memory address as the other pointer but contains some junk value
I think it's because when you cast to double, it doesn't stop to the first 4 bytes (08 00 00 00) as the int, but it forces to take 8 because the double data type is 8B long, so the same 4B as before plus some junk that was already there (es: 08 00 00 00 c6 8a 6f 37), which results in a seemingly completely unrelated decimal number when printed
I know you wanted to touch on it in a different video, but it would help to explain what type of memory you're talking about here: Cache/RAM vs Disk memory. This is an area of confusion for newcomers and I think you could explain it quickly. The simplest metaphor I ever heard was: "Your brain is the CPU (and has cache), your desk is RAM memory, and your bookshelves are Disk memory. You can only read what is open on your desk (RAM). What you've read, you can use immediately to do something (Cache). If you want to read a book, you need to pull it off the shelf (Disk) and open it on your desk (RAM). If you've run out of space on your desk (RAM), you have to close a book and book it back on the shelf (Disk) to make room for a new book." You're talking about RAM memory in this video.
7:30 Why after studying C++ is this the first time I'm seeing the debug memory and searching like that. What a helpful tool even to just visualize pointers &memory addresses. Thank you.
My biggest problem always has been the difference between pointers and references. Maybe you could explain and compare those two types in a future video
References are just your variable name aliased as something else - "under the hood" they are the same, it's an easier way to use pointers without having to explicitly deference them to access the data.
A pointer is a box that hold memory, a reference is a new name for the same box of memory. Let's say you have a toy box with a sticker on it with the name "toy box", you've put that box somewhere in the house for long term use. then near you you have a box, in that box is the name of the room you have put your toy box. that is a pointer a new box pointing you to the old box whereabout. now assume you have the same toybox, but with two stickers on it the first say "toybox" the second says "oldtoys". that's a reference. it's a second name for the same box.
@@Kanak_Bodkhe pretty much, to avoid the confusion for you. Other languages treat the term references differently, some refer to pointers as references particularly in high level languages. Best way to think of it is that reference == constant pointer.
u know what u r excellent in teaching skills the main thing is way of talking... ur brilliantly clear in it and I can understand it in one go ....seriously ty ty ty ty so much for this much efforts....have a great journey in life.
So a point is a memory address but when you make a pointer it has to store somewhere else in memory what memory address you are storing so a pointer to that pointer is the memory address thats storing the original pointer Why is this useful? Let's say you are making a linked list in C++ you have a series of structs to be list nodes now every time you create a struct its put somewhere random in memory so to organizer your node you make an array of pointers to where each node struct is. Well one way to do arrays in C/C++ is just make a pointer to the first value in the array. but since its an array of point of where each list node is you have a pointer that points to a pointer.
I know the example program does nothing but you can see the struct Node for each node in the linked list is referred to via pointers, and when I made a pointer array it was a pointer that pointed to a pointer
A few bad practices here and there, such as setting a pointer to 0 or using NULL (both those practices are heavily discouraged in C++) or casting an int* to a double* using a C-style cast, which should never be used in C++, but overall it's a great video! :) I'm not being pedantic and I understand that the main focus of this video is how pointers work but some inexperienced people in C++ or beginners may see those things and think that it's fine using them. We should strive to avoid showing bad practices, especially in tutorials!
Man, I have to say, I have watched so many youtube channels, but nothing can reach this level of explanation. I really want to thank you and I will do my best to promote all your videos to my friends and students.
Pointers being hard is not about what you described in here but why do they exist? How do we use them in real world? For the first part, I figured it is that programmers don't want to deal with long random addresses so they abstracted them one level up into pointers. Second question remains though, especially when there are stars everywhere in a typical cpp program. Like, not just variables but functions and whatnot? It is intimidating. Also if I were to overthink it, who decides the memory addresses? The OS? How does the pagefile work? Does these addresses actually change behind our back but as long as we call a pointer by name, it is all good? Is this the real reason pointers exist, so that memory addresses can be managed by OS and we get to stay on a higher level unaffected? Should I tinker with the address of a pointer without declaring it as a pointer, would that create a hole in the matrix?
vtastek exactly the point where I'm struggling. Why don't just use variables? What are pointers actually good for? It's not the how, it's the why for me :S
+vtastek Pointers are very useful to manipulate memory directly when simple variables might be very limiting... It's hard to explain so let me give you an exemple. In OpenGL there are these things called shaders that are basically programs that are executed by the GPU. You have the option to compile these shaders inside your graphics application. The argument that OpenGL takes (glShaderSource) is the reference of the pointer that points to the first character of the sequence of characters that represent your entire shader program, that is usually written into a file. That's a very smart way to deal with this task of compiling shaders inside a graphics application IMO. Another exemple, when you're dealing with files like images, sounds etc... They are all basically datas stored in the memory, in other words, sequence of bytes that can be manipulated freely. You will want to store the memory address of these sequence of bytes that represent your file to *address* the file right. As Cherno said memory, data manipulation(store, retrieve, access stuff in memory) is probably most of what we do when we're programming, as you said simple variables are just an abstraction to memory, pointers is just another more faithful of what memory really is. That's why we say C++ is a very powerful language, Java for exemple doesn't even give you this tool for safety reasons. About the second part of your question: yes, is the operating system the handles the whole memory system, how it actually does it, is a very complicated task and there is this book: Modern Operating Systems from Andrew Tanenbaum that you can read if you wanna go in depth about it, but for graphics programmers you don't have to worry how the OS handles it. That's kinda one of the functions of the OS TBO: abstract the computer system for programmers to worry about just with the application they are developing.
BlackPhillip2018 glshadersource taking a pointer instead of the shader file directly is a smart way because? I would guess so we can manipulate the shader source files, change keywords, create templates, combine files ourselves and stuff like that...
The reason as I understand it at least (as it relates to functions), is because whenever you pass a variable into a function normally, C++ actually copies that variable before using it. Normally that wouldn't be an issue, but if your dealing with memory sensitive applications, there's no reason to have two copies of everything you're passing into functions (again depending on the situation). The very act of copying also takes up CPU cycles, so that can be important when you're dealing with variables holding a lot of data and/or applications that need to be as fast as possible. Also, if you want to modify a variable in place, passing the pointer to that variable would allow the function to do that as well.
Jonah Loctar hmm... Can we say that this copying is the default behaviour for most other programming languages? Because it feels like pointers then should be default/right way of doing it. variables must be a fallback. I write shaders and use functions but never used stars or saw them anywhere for that matter, hopefully that's not critical for shaders. ^_^
I like how you say "don't worry, don't overthink it" lol, pointers have been my biggest headache in trying to learn C++ for probably 6 months plus, I thought I was making progress, got functions, got classes, variables, control flow, all good, then pointers, no idea whatsoever lol, would I be correct in saying that the point is to act upon the original variable itself instead of making a copy????? Even if that's right.......................... I still don't get it lol, ARGGGGGGHHHHHHHHHHHHHHHHHH!!!!!!!!!!!!!!!!!!!!!!!!!!!
and then you say "smart pointers???" Alright, now I'm terrified, probably gonna bust a blood vessel trying to get my head around this, credit where credit is due though, you've made it make more sense than any other RUclipsr I've seen cover it, still doesn't make all that much sense though
The problem with pointers is the syntax, not the concepts. See the concept of a pointer is baby level easy. It's the damn syntax. An asterisk means different damn things. In declarations you put in the address, but you wouldnt do that after. Thats hard and counterintuitive as programmers who consider grammar. This inconsistency is mind bending but something to have to get used to
From my limited knowledge (learning c++ for a year) pointers are a nice way to make less variable copies, for example when passing them by value to functions, it creates a local copy (function scope) of the original variable and you only work on the copy the original variable is left intact, if you want to modify the original variable you need to return the copy value from the function and assign it to the original variable, because of the copy this is less efficient. But if instead you pass a variable by pointer you don't need to do a copy, you can modify the original variable directly inside the function, I think the technical term is "modify in place". Pointers can do much more but i'm still learning. My initial problem with pointers when I started learning, was how they use the same char * to do two different things (not to talk about . vs -> in c++), one * creates the pointer the other, gets the true value from the pointer, but both are used in the same way, defined at the left of the pointer name, this is simple now to me but initially was confusing. A thing that still confuses me with pointers, specially when I see them used in advanced C programs, is the usage of double ** or in some rare cases even triple ***, I know what that is supposed to do but comprehending code with them in, is sometimes complicated to me.
Great refresher. Although probably not the best in practice you could change the type of the pointer in order to manipulate the data. int number = 8; void * ptr = &number; int * int_ptr = reinterpret_cast(ptr); *int_ptr = 10; or *(reinterpret_cast(ptr)) = 10; std::cout
You need to use delete to dealocate your memory so that it can be used later again or else you will have a dangeling pointer that uses your memory for nothing. Thats why you always need to call delete or delete[ ] .
TomHazRedstone if you create something on the heap (with a 'new' keyword for example) then you want to delete it. If you create it on the stack though (eg. Local variable inside a function or method) then you don't need to worry about it, as it will be discarded from the stack automatically when that block or section of code finishes running.
This is the best explanation of pointers I've ever seen. So simple and perfect for those like me, who wants to clear the basics. I just started to do masters in Game Development and in one of our classes we are building game engine and oh I am struggling with pointers a lot. Although I've spent lots of hours on understanding pointers and basics, I couldn't until I watched this video. I am using pointers but I was still confused with basics. Thank you :)
If you cannot explain something simply you do not understand the concept. It is refreshing to see how simple you make computers to understand! The 1 dimensional street analogy makes memory so important! Each part of the memory is an address . If we all lived on this street we could find each other by our pointers and store 4 bytes at a time!
Very simple explanation about pointers. I know this video doesn't cover all the "power and danger" pointers hold (from I've been told, I don't really know), but it was an excellent starting point to not get scared.
I would only imagine a Vulkan series coming way later down the line to never. To use Vulkan effectively you need to be an expert (if you want to build good applications with it); fluent in C++ and certain programming paradigms. Getting a 2D triangle in Vulkan with one tutorial I did took nearly 1000 lines of code. Its nowhere near as simple as OpenGL.
When I moved from Java to CPP I was confused but I quickly grapsed a concept of pointers and how memory works. Now I'm thankful for that awesome language. Being able to work with raw memory is great!
It's amazing when you can manipulate the memory of other programs running as well to make exploits for things like games. Direct memory access was the whole reason I switched to C++
I would like to differ that "types do not matter". Even if type casting does not change the content of the memory it surely has some effect. Say you have int var = 8 and then type casted the address of that integer to double pointer double *ptr = (double *) &var. Now if you print the value by cout
Don't worry, this dude is the most self absorbed crock head I've ever seen. This, and all other videos, aren't about C++, they're about Cherno. This guy has zero clue and zero industrial, real world, working knowledge of C++. This guy is equivalent to RealTutGML and TheRealBoston. But hey, this isn't just my view, check programming on Reddit and you'll see many Redditors advise against this guy.
@@kaizen9451 I disagree. First of all doesn't he work for EA? This video is good, he is just focussing on certain aspects and clearing certain things up. Of course types matter when you actually use the data, but he said that.
While you're in the kitchen, you should write a C++ Cookbook. Nice tutorial BTW. Pointers can be tricky to understand if they are presented in the wrong way. This is very clear and it's great that you showed how to use the C++ memory inspector in VS. This makes things so much easier to follow. It's a bit easier to type in &some_ptr in the memory inspector to have a look at the actual pointer memory location. Thanks for your work :)
So to continue housing address analogy: a pointer is an address of a plot of land. On which you can have a various size housing. So when you say a char, it’s like a small studio apartment, and when it’s a long long it’s like a huge mansion. A computer wanting to know the size of the variable that you want to write into this address is like a realtor/house builder wanting to know if it’s a family of one or eight moving in and what type of housing to build for them.
"Memory is a linear 1 dimension line" giving a street example. Me thinking back to the days of memory banking because 16-bit bus limits and having all the houses on that street suddenly go underground and are replaced with different houses that all have the same address on the same street but now it'd different data.... I don't miss those days lol.
i keep coming back to this video so ill just write my main take away: - pointer is an integer that contains the memory address of a byte - "int* ptr = 8" is how you declare a ptr var - "*ptr" is how you access the data inside the pointer (dereferencing) - "memset(ptr, value, byte size) - delete[] ptr - there is such thing as a pointer to a pointer
Yeah the types of pointers are actually important even though the start of the video implies they aren’t. Compiler will use pointer types for type safety and when you increment pointers and use pointer arithmetic the compiler will need to know what is being pointed to.
For an example of multi line pointers Say you're looking at a game, you have a health address in memory, (this is backwards) the health address is being pointed at by the player struct, the world is pointing at the player struct, and the game class is pointing at the world struct.
honestly, I got pointers for the first time today, after a year of programming in c++ (as a hobbyist). I UNDERSTAND it now your video was a big push, and eventually 1 line of code made it click for me ``` int var = 8; printf("The value of var is %i and the adress of var is %p", var, &var); ``` that's it :o
Very well explained! Absolutely agree, extremely basic stuff - even more so if we look at the assembly(machine code) output --> it would have been really useful to show the assembly output, particularly the different forms of MOV being utilised (dump memory to show opcodes) when the C++ data type changes.
A pointer is a variable that stores a memory address. Because a memory address is always the same size (it does not matter what data type is located in that address) the size of any pointer variable is always gonna be the same, and it depends on the computer processor. For example, pointer variables will be 8 bytes for 64-bit processor, 4 bytes for 32-bit processor. I repeat, remember you are storing a memory address in a pointer, not data. If you are asking yourself - what is the point of going through all this extra steps? Well, each time you declare a variable you are asking for memory space to store the data; if the data you want to store is an integer, then you declare the variable as an int variable and the computer will give you 4 bytes to store that data since the size of an int is 4 bytes, if it is a double, it will give you 8 bytes, and so on. Each time you are using this variable throughout the program, you are coping this value and using more memory space. By using a pointer, we are accessing the original "storage," in other words, we would not be copying the data, but rather accessing and changing the original value.
Excellent explanation, my teacher is good but ain't no way he was teaching like this! Keep up the good work mate, finally understand this, and it's actually pretty simple!
Really a very great video. I saw other videos on the same topic but yours felt very natural and it felt like if you my doubts about pointers and explained them. Thanks bro. Keep Up.
this man is teaching c++ in the kitchen WHAT A LEGEND
That caught my attention as well 😄😅😅
He is codeChef.
He went to the park in thet other tutorial
😂😂😂
Let him cook
Couple of years ago, when I tried to understand this, I told my teacher at school: "So pointer is like a shortcut on a desktop". And he started to yell with excitement: "Thats the best analogy I have heard"
In what language was the yelling? I wish I could hear it. It must be so fun.
@@jeffz7310 He was probably yelling in C or C++ if I had to take a guess.
@@chasewatts4822 Like this ?
std::cout
They teach you pointers at school? they were teaching us how to open mspaint from the start menu
A shortcut is a launcher, like executing another pseudo executable to launch an executable (with params allowed).
POINTERS in C++ but every time he says "a pointer is just an integer" it gets faster
I agree, they talk about houses with addresses - yeah easy. But what we want is practical examples of real code using pointers and leave out the simple examples because they really are too easy.
Cause poiters make your code faster
is he using visual studio?
@@Soccercrazyigboman yes
@@user_375a82 Imagine having to move an entire house from one address to another... vs just telling some process/function to refer to a different house (address) instead. What do you think is going to be easier for the computer to do?
Thanks for pointing me in the right direction.
Get ouuuttt! Get ouuuutttt!
was that a pun?
Oh take my like and go f yourself
@@itay1232 😂😂
are you that ship that Gil Amelio
was responsible of pointing?
I would like to mention an interesting behavior of pointers. If a pointer is incremented, e.g. ptr++; , then the address it holds is not always incremented by one byte. Instead it is incremented by the size of its type. Thus, a 4 byte integer pointer 0x00000004, would become 0x00000008, if you type ptr++;
And thats how arr[i] work for successive values of i.
Lol never even thought about that for some reason 💀
Of course it's incremented by the size of the type. After all that's what the types are for, to determine how many bytes are occupied by an object and that's one of the reasons why statically typed languages are faster :)
I'm crying in relief, I've been struggling to understand what are pointers and how they work for MONTHS because my C++ class is not the best for newbs like me, and you just clarified everything in less than 20 minutes!!! THANK YOU!!
*My takeaways:*
1. A pointer is an integer that stores a memory address 1:10
2. Void pointer 3:55
3. Dereferencing a pointer 9:25
4. A pointer to another pointer 14:15
I have seen you somewhere
@@chiragsingla. :)
Prob another programming video and thanks for all these timestamps very helpful
@@chiragsingla. You are welcome!
I know this comment thread is kinda old but if anyone saw this please elaborate myself a little bit. After I finished the clip, I went on to try using a pointer that points to the address of a variable of string data type. But when I declare my pointer data type as integer, the compiler gave out warning that it cannot convert from string to int. I then change the pointer data type to string then it works. So I reckon either Cherno talking about int data type just in this context, but generally pointer data type should be the same with it's address variable's data type
I already took 2 semesters of c++ focused computer science classes at univ but I love the way you explain things it’s both entertaining and makes you think a bit different
One of my professors gave me your channel and said it was a huge help to him when he was first learning C++. This is the first video of yours I've watched (he told me to study up on pointers) and you explain things very well and thoroughly... I'll definitely watch more of your teaching videos. Thank you.
Your professor ? How long has he been programming for in C++ ? :O
@@Gaelrenaultdu06 Yeah... that doesn't add up. Probably meant "instructor". Students often use the term professor when they mean instructor at a college. Really, a "professor" would have at least a doctorate, and a doctor of computer science would not have been introduced to C++ within the last 4 years.
@@bradleyfallon6847 Who knows... there are hundreds of programming languages, and someone with a doctorate isn't necessarily going to focus on learning a big part of them.
14:32 ... it's kind of reminds me of "I know a guy who knows a guy"
who knows a guy, who knows a guy :)
😆
For anyone struggling with pointers, I made up an analogy that helped me learn. Hopefully it helps you understand!
Let's say you have a bunch of warehouses in an industrial district, and each one of these warehouses (memory storage) has a different material stored in them (this is the variable).
To find a warehouse with say, wood (a variable), you would need to search up the address (memory address) of where the actual WAREHOUSE that stores wood is. So you could go there and pick up the wood(the variable memory), and build a house with it (memory modification, or modification of where the variable data is stored).
Bullshit. You should get your overthinking head out from here. I’m sure you never did anything significant in c++ if that’s the thing you say to anyone struggling with pointers. Fuckn go away.
@@korigamik Why are you so hostile? gtfo
@@korigamik who asked
Smart
Wood is not the variable, is the data.
I can see cherno just running into a room, slamming his hand on a table, and just yelling *”POINTERS”*
Pointers did everyone's head in back in first year Uni. I looked back on it in later years realizing just how simple they are.
Is only complicated when those teaching don't know shit about them.
@@txorimorea3869 unlike cherno lol
I'm watching this again 4 years later... still the best video on pointers, everything becomes clear and understandable. I was trying to learn Rust, but nobody can compare to The Cherno's teaching skills on youtube. May just end up sticking with C++ because he makes it so easy!
"If this is actually a double, we could be in trouble." Nice rhyme.
I really like how you state the use of your next explanation before the actual explanation. Ex. "Then how do we access the data?"...."We do ____."
This preps my brain to make space for the answer because I know what its for.
Too many teachers and professors will say: "we can do ____"..... "This lets us access the data."
Simple difference, but its the difference between "oh this is the answer let me pay attention" and "oh sht that was important wait what did he say"
Also, the way you keep iterating important points and continually connect previous points allow me to connect all the things Ive been listening to so I can organize them in my head.
Thanks.
I recall struggling with this concept way back in the day. I can greatly appreciate the amount of simplicity you bring to it from a bottom up approach
Just as memory is everything for programming.
Your correct and precise pronunciation of words, as well as the proper cadence you use when speaking, is everything. Especially for those of us who are native English speakers. For which I thank you.
I have been writing code in C++ for more than 10 years and I like to review, learn and above all... absorb new resources to teach. And in you I have found a great source.
Greetings from Argentina.
Pablo
As someone that hasn't programmed in 11 years(and that was in python) trying to learn c++ you have by far made the best course I've took so far and I've took quite a few on udemy....so thanks for breaking it down super simple 😄
This series is the best I've seen on the internet. I'm learning/understanding more than I learnt in school... Thank you!
Quality is really improving every video, keep up the good work
10:52 But I'm not a rapper
Appropriate profile picture there... xD
But are you invincible or in fact vincible?
???
@@_Omni I make a meal for my friends, try to make it delicious, try to keep it nutritious, make wonderful dishes. Nobody thinks about the way i feel, none of them compliment the meal!
cherno spittin' that inferno while gettin' on with you're learno
You have a great pronunciation. Every word is clear! Thnx!!
Amazing work breaking down for us things like that. I love the way you are pushing us to play with memory addresses and kind of reverse engineer how stuff works rather than keeping it shallow like most other videos out there. Great work keep it up
If we say this is actually a double, we can be in trouble.
implement double pow(double val, double degree)
No i don't no if i should stay or shold i go
Thanks for your excellent video! I’m just getting going in C++ using C++ Primer and your tutorials. They compliment each other well. Your explanation of pointers was clearer than all of the others I have read or heard. Cheers!
BEST TEACHER EVER !!!!!!!!!!!!!!!!!!!!!!!!!! AND I CANNOT STRESS THIS ENOUGH MAN
You are a good human :) I can understand pointers now! I see the point, it's like knowing where you left your keys, if you know where you left your keys you can access your car, and a pointer is where you left your keys.
Great video, just one thing:
int var = 8;
int* ptrJ = &var; // ptrJ point to the same memory address that contains 08 00 00 00
double* ptrD = (double*)&var; // ptrD point to the same memory address as the other pointer but contains some junk value
I think it's because when you cast to double, it doesn't stop to the first 4 bytes (08 00 00 00) as the int, but it forces to take 8 because the double data type is 8B long, so the same 4B as before plus some junk that was already there (es: 08 00 00 00 c6 8a 6f 37), which results in a seemingly completely unrelated decimal number when printed
8:46 you see it even here and Cherno removed this junk data.
Best C++ RUclipsrs on RUclips. Outstanding C++ pointers explanation.
I know you wanted to touch on it in a different video, but it would help to explain what type of memory you're talking about here: Cache/RAM vs Disk memory. This is an area of confusion for newcomers and I think you could explain it quickly. The simplest metaphor I ever heard was: "Your brain is the CPU (and has cache), your desk is RAM memory, and your bookshelves are Disk memory. You can only read what is open on your desk (RAM). What you've read, you can use immediately to do something (Cache). If you want to read a book, you need to pull it off the shelf (Disk) and open it on your desk (RAM). If you've run out of space on your desk (RAM), you have to close a book and book it back on the shelf (Disk) to make room for a new book." You're talking about RAM memory in this video.
what an underrated comment
This channel is the very first among all other channels that explains it in a way I can actually understand! instant sub!
I always thought of a pointer as an integer, im happy that my guess/mind picture got confirmed by someone :D
7:30 Why after studying C++ is this the first time I'm seeing the debug memory and searching like that. What a helpful tool even to just visualize pointers &memory addresses. Thank you.
"It's just one big blog. it's like one big line"
My brain: Oh...like *brainfuck??*
Don't be dumb
@@michielarkema ++++++++++[>+++++++>++++++++++>+++>+.
I never had a good memory, but this video is shifting things around. Nicely done, Cherno! Thanks
Next video : CLASSES AND STRUCTURES in C++ (recorded in bathroom)
But... Pointers are realy easy to understand when you are in kitchen!
Next video: TEMPLATES in C++ (recorded in the restroom)
🤣🤣🤣🤣🤣
next video: structs in C++(recorded while skydiving into the pacific ocean)
Your channel is simply amazing, these series are by far the
most useful tool I've found in RUclips, thank you.
damn you are very good at explaining things my prof seemingly can't tell us in a 90 minute lecture
What a great series; it all comes down to your solid knowlege of the subject and being able to explain it so clearly. I'm loving it.
I came from java programming and I really like it so far! Great job
Point is an integer, a number which stores memory. A pointer points towards a memory.
This guy makes programming seem so simple
My biggest problem always has been the difference between pointers and references. Maybe you could explain and compare those two types in a future video
References are just your variable name aliased as something else - "under the hood" they are the same, it's an easier way to use pointers without having to explicitly deference them to access the data.
A pointer is a box that hold memory, a reference is a new name for the same box of memory.
Let's say you have a toy box with a sticker on it with the name "toy box", you've put that box somewhere in the house for long term use.
then near you you have a box, in that box is the name of the room you have put your toy box. that is a pointer a new box pointing you to the old box whereabout.
now assume you have the same toybox, but with two stickers on it the first say "toybox" the second says "oldtoys". that's a reference. it's a second name for the same box.
So a reference is like the "TYPE" of a regular Object?
@@mortvald so reference is like a new name for that memory or the data but different from the previous name that memory had
@@Kanak_Bodkhe pretty much, to avoid the confusion for you. Other languages treat the term references differently, some refer to pointers as references particularly in high level languages.
Best way to think of it is that reference == constant pointer.
u know what u r excellent in teaching skills the main thing is way of talking... ur brilliantly clear in it and I can understand it in one go ....seriously ty ty ty ty so much for this much efforts....have a great journey in life.
"Let's say i have a pointer which points to my pointer".
Wat
int a=4;
int *b=0;
b=&a;
int **c=0;
c=&b;
printf("%d",**c); //this will print 4
Both pointers are pointing at each other
So a point is a memory address but when you make a pointer it has to store somewhere else in memory what memory address you are storing so a pointer to that pointer is the memory address thats storing the original pointer
Why is this useful? Let's say you are making a linked list in C++ you have a series of structs to be list nodes now every time you create a struct its put somewhere random in memory so to organizer your node you make an array of pointers to where each node struct is. Well one way to do arrays in C/C++ is just make a pointer to the first value in the array. but since its an array of point of where each list node is you have a pointer that points to a pointer.
EXAMPLE WRITTEN IN C
struct Node
{
int value;
Node* prev;
Node* next;
}
Node** initialize_linked_list(int size)
{
Node** list = (Node**) malloc(size * sizeof(Node*));
for (int i = 0; i < size; i++)
{
list[i] = (Node*) malloc(sizeof(Node));
memset(list[i], 0, sizeof(Node));
if (i = 0) continue;
list[i]->prev = list[i-1]
list[i-1]->next = list[i]
}
return list;
}
void free_linked_list(Node** list, int size)
{
for(i = 0; i < size; i++)
free(list[i]);
free(list);
}
int main()
{
int size = 3
Node** list = initialize_linked_list(size);
free_linked_list(list, size);
return 0;
}
I know the example program does nothing but you can see the struct Node for each node in the linked list is referred to via pointers, and when I made a pointer array it was a pointer that pointed to a pointer
A few bad practices here and there, such as setting a pointer to 0 or using NULL (both those practices are heavily discouraged in C++) or casting an int* to a double* using a C-style cast, which should never be used in C++, but overall it's a great video! :)
I'm not being pedantic and I understand that the main focus of this video is how pointers work but some inexperienced people in C++ or beginners may see those things and think that it's fine using them.
We should strive to avoid showing bad practices, especially in tutorials!
Thanks for sharing and for your effort......
"When you understand how and when to use pointers there is no limit "
Segmentation faults incoming
IMHO, this video is the most simple but powerful elaboration and explanation about Pointer; simply comprehensible. TY
1. changing a value in a function
2. linked list (for the *next item)
3. polymorphic classes
and a lot more:)
Dude these videos are so underrated , i have seen your videos your deserve more views
Any way great and well explained tutorials :)
This is the best explanation on pointers I have seen to date. I wish I had had it when I took a course in C++ 25 years ago.
"if we make it a double, there would be trouble, Double trouble!"
Such a great explanation, this does not feel like a YT video but a really good lecture from a really good teacher.
Good! Keep it up!
Man, I have to say, I have watched so many youtube channels, but nothing can reach this level of explanation. I really want to thank you and I will do my best to promote all your videos to my friends and students.
Thank you. I thought they are pointless so far.
Pointers being hard is not about what you described in here but why do they exist? How do we use them in real world? For the first part, I figured it is that programmers don't want to deal with long random addresses so they abstracted them one level up into pointers. Second question remains though, especially when there are stars everywhere in a typical cpp program. Like, not just variables but functions and whatnot? It is intimidating.
Also if I were to overthink it, who decides the memory addresses? The OS? How does the pagefile work? Does these addresses actually change behind our back but as long as we call a pointer by name, it is all good? Is this the real reason pointers exist, so that memory addresses can be managed by OS and we get to stay on a higher level unaffected? Should I tinker with the address of a pointer without declaring it as a pointer, would that create a hole in the matrix?
vtastek exactly the point where I'm struggling. Why don't just use variables? What are pointers actually good for? It's not the how, it's the why for me :S
+vtastek Pointers are very useful to manipulate memory directly when simple variables might be very limiting... It's hard to explain so let me give you an exemple. In OpenGL there are these things called shaders that are basically programs that are executed by the GPU. You have the option to compile these shaders inside your graphics application. The argument that OpenGL takes (glShaderSource) is the reference of the pointer that points to the first character of the sequence of characters that represent your entire shader program, that is usually written into a file. That's a very smart way to deal with this task of compiling shaders inside a graphics application IMO.
Another exemple, when you're dealing with files like images, sounds etc... They are all basically datas stored in the memory, in other words, sequence of bytes that can be manipulated freely. You will want to store the memory address of these sequence of bytes that represent your file to *address* the file right.
As Cherno said memory, data manipulation(store, retrieve, access stuff in memory) is probably most of what we do when we're programming, as you said simple variables are just an abstraction to memory, pointers is just another more faithful of what memory really is. That's why we say C++ is a very powerful language, Java for exemple doesn't even give you this tool for safety reasons.
About the second part of your question: yes, is the operating system the handles the whole memory system, how it actually does it, is a very complicated task and there is this book: Modern Operating Systems from Andrew Tanenbaum that you can read if you wanna go in depth about it, but for graphics programmers you don't have to worry how the OS handles it. That's kinda one of the functions of the OS TBO: abstract the computer system for programmers to worry about just with the application they are developing.
BlackPhillip2018 glshadersource taking a pointer instead of the shader file directly is a smart way because? I would guess so we can manipulate the shader source files, change keywords, create templates, combine files ourselves and stuff like that...
The reason as I understand it at least (as it relates to functions), is because whenever you pass a variable into a function normally, C++ actually copies that variable before using it. Normally that wouldn't be an issue, but if your dealing with memory sensitive applications, there's no reason to have two copies of everything you're passing into functions (again depending on the situation). The very act of copying also takes up CPU cycles, so that can be important when you're dealing with variables holding a lot of data and/or applications that need to be as fast as possible. Also, if you want to modify a variable in place, passing the pointer to that variable would allow the function to do that as well.
Jonah Loctar hmm... Can we say that this copying is the default behaviour for most other programming languages? Because it feels like pointers then should be default/right way of doing it. variables must be a fallback. I write shaders and use functions but never used stars or saw them anywhere for that matter, hopefully that's not critical for shaders. ^_^
amazing explanation dude , simple and straight to the point,
I like how you say "don't worry, don't overthink it" lol, pointers have been my biggest headache in trying to learn C++ for probably 6 months plus, I thought I was making progress, got functions, got classes, variables, control flow, all good, then pointers, no idea whatsoever lol, would I be correct in saying that the point is to act upon the original variable itself instead of making a copy????? Even if that's right.......................... I still don't get it lol, ARGGGGGGHHHHHHHHHHHHHHHHHH!!!!!!!!!!!!!!!!!!!!!!!!!!!
and then you say "smart pointers???" Alright, now I'm terrified, probably gonna bust a blood vessel trying to get my head around this, credit where credit is due though, you've made it make more sense than any other RUclipsr I've seen cover it, still doesn't make all that much sense though
HOLY SHITBALLS CHERNO, I GET IT, NOT GAY BUT I FUCKING LOVE YOU MAN
lol
The problem with pointers is the syntax, not the concepts. See the concept of a pointer is baby level easy. It's the damn syntax. An asterisk means different damn things. In declarations you put in the address, but you wouldnt do that after. Thats hard and counterintuitive as programmers who consider grammar. This inconsistency is mind bending but something to have to get used to
@@GoonCity777
"is mind bending but something to have to get used to"
most people, when asked how is to learn c++.
From my limited knowledge (learning c++ for a year) pointers are a nice way to make less variable copies, for example when passing them by value to functions, it creates a local copy (function scope) of the original variable and you only work on the copy the original variable is left intact, if you want to modify the original variable you need to return the copy value from the function and assign it to the original variable, because of the copy this is less efficient. But if instead you pass a variable by pointer you don't need to do a copy, you can modify the original variable directly inside the function, I think the technical term is "modify in place". Pointers can do much more but i'm still learning.
My initial problem with pointers when I started learning, was how they use the same char * to do two different things (not to talk about . vs -> in c++), one * creates the pointer the other, gets the true value from the pointer, but both are used in the same way, defined at the left of the pointer name, this is simple now to me but initially was confusing.
A thing that still confuses me with pointers, specially when I see them used in advanced C programs, is the usage of double ** or in some rare cases even triple ***, I know what that is supposed to do but comprehending code with them in, is sometimes complicated to me.
After watching this video I feel so tired...my head is going to explode with all of these weird terminology and different uses of **
Great refresher. Although probably not the best in practice you could change the type of the pointer in order to manipulate the data.
int number = 8;
void * ptr = &number;
int * int_ptr = reinterpret_cast(ptr);
*int_ptr = 10;
or
*(reinterpret_cast(ptr)) = 10;
std::cout
why tho?
Something that I'm quite confused about is when to use "delete" and when it will be taken care of automatically.
For each new, there must be a delete. this is the universal rule in c++.
*Unless you bother with smart pointers
You need to use delete to dealocate your memory so that it can be used later again or else you will have a dangeling pointer that uses your memory for nothing. Thats why you always need to call delete or delete[ ] .
I know why, I just didn't know in what situations it would be done automatically.
TomHazRedstone if you create something on the heap (with a 'new' keyword for example) then you want to delete it. If you create it on the stack though (eg. Local variable inside a function or method) then you don't need to worry about it, as it will be discarded from the stack automatically when that block or section of code finishes running.
That makes sense, thank you.
This is the best explanation of pointers I've ever seen. So simple and perfect for those like me, who wants to clear the basics.
I just started to do masters in Game Development and in one of our classes we are building game engine and oh I am struggling with pointers a lot. Although I've spent lots of hours on understanding pointers and basics, I couldn't until I watched this video. I am using pointers but I was still confused with basics.
Thank you :)
"really simple stuff!"
are you serious!?!?!?
If you cannot explain something simply you do not understand the concept. It is refreshing to see how simple you make computers to understand! The 1 dimensional street analogy makes memory so important! Each part of the memory is an address . If we all lived on this street we could find each other by our pointers and store 4 bytes at a time!
I can tell you how memory works!.... But I forgot.
I could*
@@doxo9597 is there a difference?
@@mastershooter64 Could is past tense of can. He no longer remembers, so he should say could.
@@jangtheconqueror ah I see, got it
Very simple explanation about pointers. I know this video doesn't cover all the "power and danger" pointers hold (from I've been told, I don't really know), but it was an excellent starting point to not get scared.
Are you going to make a series on Vulkan?
I would only imagine a Vulkan series coming way later down the line to never. To use Vulkan effectively you need to be an expert (if you want to build good applications with it); fluent in C++ and certain programming paradigms. Getting a 2D triangle in Vulkan with one tutorial I did took nearly 1000 lines of code. Its nowhere near as simple as OpenGL.
When I moved from Java to CPP I was confused but I quickly grapsed a concept of pointers and how memory works. Now I'm thankful for that awesome language. Being able to work with raw memory is great!
It's amazing when you can manipulate the memory of other programs running as well to make exploits for things like games. Direct memory access was the whole reason I switched to C++
I would like to differ that "types do not matter". Even if type casting does not change the content of the memory it surely has some effect. Say you have int var = 8 and then type casted the address of that integer to double pointer double *ptr = (double *) &var. Now if you print the value by cout
Don't worry, this dude is the most self absorbed crock head I've ever seen. This, and all other videos, aren't about C++, they're about Cherno. This guy has zero clue and zero industrial, real world, working knowledge of C++. This guy is equivalent to RealTutGML and TheRealBoston.
But hey, this isn't just my view, check programming on Reddit and you'll see many Redditors advise against this guy.
Types are a matter of housekeeping on the level of the compiler, but on the level of machine code, the CPU dosn't care.
@@kaizen9451 I disagree. First of all doesn't he work for EA? This video is good, he is just focussing on certain aspects and clearing certain things up. Of course types matter when you actually use the data, but he said that.
While you're in the kitchen, you should write a C++ Cookbook. Nice tutorial BTW. Pointers can be tricky to understand if they are presented in the wrong way. This is very clear and it's great that you showed how to use the C++ memory inspector in VS. This makes things so much easier to follow. It's a bit easier to type in &some_ptr in the memory inspector to have a look at the actual pointer memory location. Thanks for your work :)
2:02 The word "byte" means "house" in Hebrew O:
also in arabic but we spell it bayte hhh.
bae loves to byte
Jeff Tang
Is he a german?, because i speak German as well and could hear that german accent
So to continue housing address analogy: a pointer is an address of a plot of land. On which you can have a various size housing. So when you say a char, it’s like a small studio apartment, and when it’s a long long it’s like a huge mansion. A computer wanting to know the size of the variable that you want to write into this address is like a realtor/house builder wanting to know if it’s a family of one or eight moving in and what type of housing to build for them.
"Memory is a linear 1 dimension line" giving a street example. Me thinking back to the days of memory banking because 16-bit bus limits and having all the houses on that street suddenly go underground and are replaced with different houses that all have the same address on the same street but now it'd different data.... I don't miss those days lol.
I love you, I'm in a C++ bootcamp and I started late. I'm learning fast but am missing alot of sleep.
Thank you! Thank you! Thank you!
A dislike ? Who do this ? I'll chase him, don't worry.
They have their screen upside down
i keep coming back to this video
so ill just write my main take away:
- pointer is an integer that contains the memory address of a byte
- "int* ptr = 8" is how you declare a ptr var
- "*ptr" is how you access the data inside the pointer (dereferencing)
- "memset(ptr, value, byte size)
- delete[] ptr
- there is such thing as a pointer to a pointer
Um that was simple. Thank you!
Yeah the types of pointers are actually important even though the start of the video implies they aren’t. Compiler will use pointer types for type safety and when you increment pointers and use pointer arithmetic the compiler will need to know what is being pointed to.
awake at 5 am for this... lol.
I think this is THE BEST playlist on C++. Thank you for making it so simplistic and awesome!
👍👏👏👏
10:52 "If we say this is a double we could be in trouble."
For an example of multi line pointers
Say you're looking at a game, you have a health address in memory, (this is backwards) the health address is being pointed at by the player struct, the world is pointing at the player struct, and the game class is pointing at the world struct.
Subscribed just cuz you continuously reiterated the definition of pointer. That really helped!
Commenting for your exposure. The best channel on RUclips for C++ !!!
honestly, I got pointers for the first time today, after a year of programming in c++ (as a hobbyist). I UNDERSTAND it now
your video was a big push, and eventually 1 line of code made it click for me
``` int var = 8;
printf("The value of var is %i and the adress of var is %p", var, &var);
```
that's it :o
The Cherno is the only person who can talk about pointers for 3 hours and still be able to captivate his audience....
You are legend in the c++ world. Thank you for everything.
Very well explained!
Absolutely agree, extremely basic stuff - even more so if we look at the assembly(machine code) output --> it would have been really useful to show the assembly output, particularly the different forms of MOV being utilised (dump memory to show opcodes) when the C++ data type changes.
Nicely explained. I like how you took it back to the values stored in memory too.
A pointer is a variable that stores a memory address. Because a memory address is always the same size (it does not matter what data type is located in that address) the size of any pointer variable is always gonna be the same, and it depends on the computer processor. For example, pointer variables will be 8 bytes for 64-bit processor, 4 bytes for 32-bit processor. I repeat, remember you are storing a memory address in a pointer, not data.
If you are asking yourself - what is the point of going through all this extra steps? Well, each time you declare a variable you are asking for memory space to store the data; if the data you want to store is an integer, then you declare the variable as an int variable and the computer will give you 4 bytes to store that data since the size of an int is 4 bytes, if it is a double, it will give you 8 bytes, and so on. Each time you are using this variable throughout the program, you are coping this value and using more memory space. By using a pointer, we are accessing the original "storage," in other words, we would not be copying the data, but rather accessing and changing the original value.
Thank you! The whole time i was watching I understood how it works but I was like so why use a pointer over a regular variable.
Not sure how others but I'm learning also Visual Studio tricks from your videos. Thanks to GH I was able to discover your channel.
Excellent explanation, my teacher is good but ain't no way he was teaching like this! Keep up the good work mate, finally understand this, and it's actually pretty simple!
Your videos are strangely addictive.
Absolutely brilliant! Keep up the spread of knowledge man.
Really a very great video. I saw other videos on the same topic but yours felt very natural and it felt like if you my doubts about pointers and explained them. Thanks bro. Keep Up.
Pointers (*) store memory addresses, while references (&) provide memory addresses.
This channel is a blessing to the C++ enthusiasts/beginners :D
This is the best explanation of Pointers I've ever heard... Cherno the coding guru
The series is very good, Simple and easy to understand. This is my best video series for cpp. Keep up the good things. ❤✨