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