Pointers in 16 Minutes | C++ For Java Devs Ep. 8
Vložit
- čas přidán 21. 05. 2024
- Join the Discord: / discord
In this episode I explain how pointers work in C++. I begin by showing how the value stored inside a pointer is a hexadecimal memory address, which we can inspect using Visual Studio's debugging tools to see the contents. Then I show how you can modify the value stored at the address by using the dereference operator. After that I give a visual explanation of what is going on underneath all the code, with a brief introduction to the heap. Then I show the address-of operator and the pointer-to-member operator, and explain how to use each of these with pointers. Finally, I conclude with a challenge asking you to modify a vector using a function that returns nothing.
0:00 In This Episode
0:33 Syntax of a Pointer
1:08 Inspecting a Pointer
2:18 The Dereference Operator
3:21 Pointers Explained Visually
5:29 Mini Challenge: Pointer Swapping
7:24 Coding the Pointer Swap
9:32 Address Operator
12:33 Pointer to Member Operator
14:22 Challenge
---------------------------------------------------------------------
Website: ambrosiogabe.github.io/
Github: github.com/ambrosiogabe
Here are some books I recommend if you want to learn about game engine development more thoroughly. I do not profit off any of these sales, these are just some books that have helped me out :)
My Recommended Game Engine Books:
Game Engine Architecture: www.gameenginebook.com/
Game Physics Cookbook (Read this before the next physics book): www.amazon.com/Game-Physics-C...
Game Physics (Ian Millington): www.amazon.com/Game-Physics-E...
Game Programming Patterns (Free): gameprogrammingpatterns.com/
My Recommended Beginning Game Programming Books:
JavaScript Game Design: www.apress.com/gp/book/978143...
My Recommended Java Books:
Data Structures/Algorithms: www.amazon.com/Data-Structure...
LWJGL (Free, but I haven't read this thoroughly): lwjglgamedev.gitbooks.io/3d-g...
I do not even need to learn C++ but think it's useful for every developer to have a basic understanding of what pointers are. This is a really simple and well explained. As you said, it doesn't have to be complicated.
At 2:11, be advised that the value 00 represents 1 byte, just as FF would represent one byte. A byte being 8 bits, (or Binary digITs,) it takes two hexadecimal digits to cover those 8 bits. Hexadecimal F is represented as 1111 in binary, which is only 4 bits, which is why 2 hexadecimal digits make up the full possibility of values in a byte.
Omg i have been looking for c++ for java developers tutorials for ages ˆˆ
Pointers seem so useless when everything is so simple in java !
Awesome! I'm glad that these tutorials will hopefully help you out :). And yea pointers seem like an overcomplication at first, but once you learn to use them in certain ways they can help you get a very fine level of control over your objects
Pointers are way more than what this video goes into details about. For example, a pointer could also be an array. Now that means you have a pointer to a value and then the memory next to the pointer is the next value. This gives you 2 values next to each other which is something the CPU likes. It becomes cache friendly and will give huge performance increases when building heavy programs like games for example
omg i watched like 5 tutorials on this and i finally understood it! Thank you so much
Pointers for me was one of those things that I just _could not_ understand because it seemed so complicated. Then one day it clicked and I realized that they're waaaay simpler than I thought.
The same thing happened for me haha. I think a lot of my confusion was because of the emphasis on references and smart pointers in modern C++. I wasn't understanding the basics of pointers, and those were confusing me so much more
You're really good at teaching stuff
i start c++ and this tutorial time is perfect thank you
No problem! I'm glad that I got the tutorials out at a good time :)
Hi!
Hey :)
8:43 you forgot to delete the tmp pointer, unless it's not necessary?
I did forget to delete it :) . In the case of this program, the operating system will automatically clean up the memory once the program finishes execution, but it's definitely a good idea to always delete allocated memory
@@GamesWithGabe Actually you shouldn't delete that tmp pointer. If you did, then you would be double freeing the memory pointed to by a originally.
A simple rule to remember that is "number of `new`s == number of `delete`s".
@@RobarthVideo Yep you're right. I didn't bother actually watching my video when I responded to this comment initially (because I mistakenly thought I knew which part of the video they were talking about haha). But I address this in the next video as well :)
Question! When should I use explicit pointers like this, and when is it better to use smart pointers?
(I'll stop commenting on your old vidoes soon. Promise. (Might be a lie, who knows!))