Python Memory Management and Tips Transcripts
Chapter: Python variables and memory
Lecture: Let's talk pointers

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now let's look at an example of some very,
0:02 very simple code in C, C++ and first C++
0:06 it's a little bit complicated,
0:07 cause even simple code can get pretty complicated.
0:10 But here it is. We're gonna look at this function so this is not object
0:15 oriented programming like C++ more on the C side,
0:18 and what we're gonna do is we're gonna have two person structures or they could be classes
0:23 whatever. But we're gonna have two pointers notice the person star,
0:28 not person, but persons star.
0:30 That means we're passing the address of where that thing is in memory
0:33 rather than making a copy of its data and passing it along.
0:37 We're gonna take these two person objects that we're referring to
0:39 by address and then we're gonna ask the question
0:41 "are they the same?" The way we do this in C is you go to
0:45 the pointer, which is some place in memory.
0:49 In this particular example, it was something like this,
0:51 like 0x7ff
0:53 whatever it doesn't matter. This the value of this thing is just a place out
0:58 in memory. And here in that memory location on what's called the heap,
1:02 we've got two pieces of information, right? Two of these pointers point to those places out in
1:08 memory. That's why we're gonna use
1:10 this arrow operator P1 goes to our dash greater than arrow,
1:15 the id. So follow the pointer out and then find out there that memory
1:18 location what the id is and see if they're the same.
1:22 So this is how it works in C,
1:25 and it raises a lot of questions that are maybe not even knowable here.
1:29 For example, who owns these objects?
1:32 Who is responsible for creating them and ensuring that they get cleaned up correctly?
1:37 That answer could vary. It could be allocated in the stack of the thing that
1:41 called this function. It could be some other part of the program created it,
1:45 and when it shuts down or that part cleans itself up,
1:48 it's supposed to delete those out of memory.
1:51 How long will they stick around?
1:52 I don't know. Until the other part,
1:54 whoever owns it, decides that they're going to go away.
1:58 What happens if we forget? What happens if somehow the programmer lost track of that
2:03 piece of memory? Well, that's a memory leak,
2:05 and it's just going to stay out there forever.
2:08 There's nothing that's gonna fix it.
2:10 That's bad. But what's worse,
2:12 usually is if actually it gets cleaned up too soon.
2:15 And the reason is you'll still have that
2:16 p1 pointing to that memory address,
2:19 But what is living out there is either gone or it's Some other piece or some
2:23 partial piece of some other data,
2:25 and that could be all sorts of problems if it gets cleaned up too soon.
2:29 So what I want you to take away from this is just there's a lot of
2:31 actual bookkeeping or accounting around things that were created,
2:35 how long they live, whose job it is to clean them up, when they can do
2:39 it, and they've got to make sure that they don't forget to do it,
2:41 but not too soon. Okay?
2:43 So all of these things are really tricky around C and pointers,
2:47 and we'll see in Python that generally that's not what we have to do.
2:50 A lot of things are done for us,
2:52 but we want to understand what is done for us.
2:54 How is it done for us, and so on.