 Hey everybody, it's Brian and welcome to the 26c++ tutorial Was kind of going through my inbox here. You can see here's my my YouTube page And I had a new comment from Donut Sucking Machine 2. I love that name. That's hilarious Says hi Brian saw your C++ by value by reference video was very helpful Wanted to request you upload an elaborate video about dangling reference and dangling pointers for beginners That's actually a very good question. So you know what donut sucking machine 2 this one's for you All right Did a quick search on Google dangling pointers. What are they? Well dangling pointer and if you go to Wikipedia and look it up and read the whole article We'll tell you that a dangling pointer is a pointer that points to a position in memory That's no longer valid meaning you had a pointer to an object that object got deleted But the pointer still points to that memory and there's a couple brief examples here And I'm going to go through one real quick. All right. Here's our template You can see we're doing include iostream using namespace std We have our main and it's just the simple boilerplate. We've been using here So we're going to go int Get in so we're just going to make a function that returns The address of an integer say int num And we'll just say one two three four There's our number in memory and we're going to return the address to that number so we're going to return a reference to it and Just for the sake of argument here, let's add a few things so we can kind of trace what's going on in memory Say reference Whoops, so we're going to print out the address of where it is Let's actually print out the value to The very simple function just returns the reference or the address remember the the ampersand is the address of we'll call the reference and then down here in in main We're just going to say We'll call this pointer just so we know what it is And we're going to initialize that to zero remember you should always initialize your pointers to zero And we'll say Let's See what should we say here empty? And let's copy this So we're just going to print out that the pointers empty Then we're going to assign the pointer a little copy and paste magic here. I remember our our get into function Simply returns the address of this integer right here that we're creating right now We want to verify This is in the same spot. So we'll say assigned And what that's going to do is going to print out the pointer So it should print zero and then it'll print some memory in location and then we're going to Actually print out the value of that too because we want to see what it what it equals here Let's point to that memory location Save our work now quick review Once again, we have our function Get int which just returns a pointer So we're saying here's our integer. We're going to print out the reference location the value and we're going to Return the actual address and here is where we're returning that address right here So we're signing that pointer and then we're going to try and use that pointer for something and This is actually a rather interesting topic and that's part of the reason why I wanted to do this video It's something that throws a lot of people off and it actually causes a lot of bugs in professional programs And it's a security vulnerability I'm actually quite a security nut and dangling pointers are a very very big deal in security right now because You have a pointer that's pointing to an invalid location in memory You can do things with that pointer manipulate the program. So anyways without further ado, let's run this It compiles links Okay, empty. This is our initial pointer. See that's all zeros. That means it's not pointing anywhere in memory Here's the reference. That's where we create that variable And the values 1 2 3 4 so so far everything's what we expect now we assign it notice how it's the same memory location this 0045 Fb 70 same memory location, but when we go to print out the value we get this This junk. What is that? Well, that's invalid memory. That's a dangling pointer You're sitting here going but we haven't deleted the pointer. Let's explain what's going on here You have to go back to our conversation about the stack and the heap This is all being done on the stack We're creating this variable on the stack Once we hit this return. Yes, we're returning the address in memory of this number But this is automatically deleted by the compiler because it's on the stack remember everything in the stack once it goes out of scope Let's actually put this here out of scope Once it goes out of scope, it's automatically deleted by the compiler meaning all these variables and all these things on the stack No longer exists because the stack is completely wiped clean and then it moves back here So remember this is the stack Then it jumps to this at which point this becomes a stack Then the context of execution jumps back down here and this becomes a stack So once you jump out of get in This number no longer exists even though you have the memory location So you have a dangling pointer meaning it's pointing to a memory location that your program is no longer using The the object in memory that you used to use has been deleted So I hope that clears up some of the confusion I would actually go out to the Wikipedia and read that full article. It's a very good article One thing you should know is There are some guidelines out there on programming for example You can go to Google and type in Google C++ coding standards or coding guidelines And they'll actually detail how to avoid things like dangling pointers So this is Brian. Thank you for your feedback. I hope you found this Informative and I hope I answered your question fully. All right. Thanks a lot