Lecture 15: Final Exam Review and Dynamic Allocation - Episode Artwork
Technology

Lecture 15: Final Exam Review and Dynamic Allocation

In this lecture, the instructor reviews key concepts for the final exam, focusing on cumulative topics such as dynamic allocation, object-oriented programming, and pointers. The session includes a dis...

Lecture 15: Final Exam Review and Dynamic Allocation
Lecture 15: Final Exam Review and Dynamic Allocation
Technology • 0:00 / 0:00

Interactive Transcript

spk_0 All right, so welcome to the extra session.
spk_0 Come on in and have a seat.
spk_0 So yeah, first thing we'll do is go over any questions about the exam.
spk_0 Maybe I should just go over like a quick topic list.
spk_0 So let me really do the exam as a cumulative.
spk_0 There's only only way to do it because you know,
spk_0 everything is built on top of each other.
spk_0 So we're going to have the midterm topics,
spk_0 which would be types and variables.
spk_0 Control flow.
spk_0 So this is stuff in the midterm.
spk_0 Arrays.
spk_0 Functions that's both using functions and the clearing functions and the fighting function.
spk_0 So there's really three parts there using.
spk_0 Clearing.
spk_0 So now I'm going to do all those three.
spk_0 And then if you've done the homework and the quizzes and stuff,
spk_0 you've done all three of those.
spk_0 Right?
spk_0 And if you've actually taken the midterm, you've done the first one.
spk_0 Functions.
spk_0 All right.
spk_0 So after the midterm, we got into.
spk_0 What do we get to do?
spk_0 Object ready for right?
spk_0
spk_0 So that's classes.
spk_0 And again, it's going to be using.
spk_0 Clearing.
spk_0 Defining.
spk_0 So from here, you want to know.
spk_0 Certainly.
spk_0 Access slash privilege levels.
spk_0 So that consists for us.
spk_0 It's public and private.
spk_0 So no difference between public and private member data.
spk_0 There are a few different other access levels, but we're not going to get any.
spk_0 For this class for this course.
spk_0 So the ones who will really start up with a public and private.
spk_0 And these mechanisms are required.
spk_0 Or there and C++ for something called information high.
spk_0 It's a way of abstraction.
spk_0 And what happens here is that you can have a single header file.
spk_0 For example, the influence of stack or rather of header file that the clearest,
spk_0 for example, a stack data structure.
spk_0 And you can switch out implementations of the stack.
spk_0 So for example, you can have different stack.cc files.
spk_0 They correspond to different different implementations of the stack.
spk_0 Maybe one stack is faster with a lot of elements.
spk_0 Maybe another stack is, I don't know, easy to pop.
spk_0 So there could be different attributes or different implementations.
spk_0 And you can swap either one in and out without changing client code.
spk_0 So that's something called information hiding.
spk_0 You can change the representational representation of things without changing the interface.
spk_0 So another example or analogous example would be like changing the engine in your car.
spk_0 So maybe Mississippi makes one kind of engine that's like, you know, has zero to 60.
spk_0 And I don't know, 15 seconds.
spk_0 And another one has more economy or something like that.
spk_0 So even though you swap the engines in your car, you still have the same steering wheel.
spk_0 Right? Same gas pedal. Same brake.
spk_0 The interface hasn't changed, but the internals of your car have changed.
spk_0 So it's that mechanism is C++ is also available to you.
spk_0 Good.
spk_0 No, no difference between a member functions.
spk_0 And non-member functions.
spk_0 So non-member functions being the ones we learned before the midterm.
spk_0 No, about friend functions really just know that now what they are.
spk_0 Anyone would reiterate what a friend function is?
spk_0 So what's different between a friend function and a member function?
spk_0 It's kind of like to have your cakey-toe.
spk_0 That race belt.
spk_0 The friend function, does it have access to public and private?
spk_0 Right.
spk_0 So it had just like friend, member functions, friend functions have access to the private member data.
spk_0 I mean, everyone has access to public.
spk_0 Unlike member functions, what special background functions?
spk_0 It's something to do with the way they're called.
spk_0 Right? So if you have a member function like for instance,
spk_0 decimal, fraction, fracked, and you say fracked.desimal.
spk_0 And this is in some client code. Maybe it's in main.
spk_0 So notice you use a selection operator here. Right?
spk_0 You select an operation or a function from an object.
spk_0 Right? Whereas let's say a normal function called like mean, I guess, right?
spk_0 Where we had an array and a size of the array.
spk_0 Notice that this is missing.
spk_0 Right? There's no selection operation. Right?
spk_0 There's no object reference here.
spk_0 This is a non-member function. So let's say this is in main.
spk_0 The one caveat to that is in the definition of member functions,
spk_0 you actually can call a member functions in this way.
spk_0 And that's on the implicit parameter.
spk_0 So if you call a member function within a member function of the same class,
spk_0 that's taken as selecting that member function from the implicit parameter,
spk_0 which is the object you're executing.
spk_0 But otherwise, the difference between friend functions and member functions is just the way you call.
spk_0 So a member function, you use a selection operator, you select it on an object instance
spk_0 or an object rather or object reference.
spk_0 And for friend functions, for example, like with the insertion operator,
spk_0 which happens to be a friend function.
spk_0 So the insertion operator has, it's actually a friend function of the fraction class, right?
spk_0 It has access to the private member data that class.
spk_0 Even though it's not called a member function, right?
spk_0 You're not selecting an object.
spk_0 And the way you can rewrite this is as follows, right?
spk_0 So if you want to think of operators as function calls, you can rewrite it like this.
spk_0 So here is a function, namely operator insertion,
spk_0 it has access to the private member data of fractions, but it's not a member function of a fraction.
spk_0 Right? There's no selection going here.
spk_0 That's gone.
spk_0 Or just doesn't exist.
spk_0 So you know that difference.
spk_0 And I think as far as operator or reloading, it's all minute.
spk_0 Look for it.
spk_0 Difference between friend functions and non- and member functions.
spk_0 I'm not going to ask it over with operators, so you don't need to memorize it syntax for that.
spk_0 Okay.
spk_0 Anything else with objects and classes?
spk_0 No about the implicit parameter, right?
spk_0 No about the explicit parameters.
spk_0 How you reference the implicit parameter, just implicitly, right?
spk_0 So go over the solutions for fractions.
spk_0 What else?
spk_0 Anything else?
spk_0 Anything else?
spk_0 Anything else?
spk_0 You know how to declare, or the client size, you should know how to write to the using.
spk_0 The using parts of how to declare a variable of a class, right?
spk_0 So for example, if you have a stack class, you can make a stack by using the type name and then followed by a variable name.
spk_0
spk_0 And know about that point.
spk_0 So we have an official exercise in this.
spk_0 But just know basically a two operations of the point.
spk_0 So the two operations are reference to the reference operations.
spk_0 So know how to declare.
spk_0 So declaring pointers.
spk_0 So the pointers.
spk_0 The clearing.
spk_0 That's really again defining and you.
spk_0 So if I want a pointer to an object, I'll go over this more later.
spk_0 You just follow the, um, the type name by an asterisk and then I can say this is my s, s pointer, right?
spk_0 Or stack pointer.
spk_0 So here is a pointer to the stack, right?
spk_0 And just know the you the two operations of I want to assign a value to a pointer.
spk_0 I just take the normal variable and use the address of operator.
spk_0 So know that the only two valid values for pointers are memory locations and the system of values zero.
spk_0 Okay.
spk_0 And then if you want to select an operation on the stack given my pointer, I can reference my pointer.
spk_0 Right.
spk_0 The reference it rather.
spk_0 And then select let's say pop.
spk_0 A pop for function.
spk_0 And the way that the shortcut for that is my s pointer.
spk_0 What's the operator right use here?
spk_0 Is it shortcut for that syntax?
spk_0 It's also called like syntactic sure.
spk_0 Just a different way of saying same thing.
spk_0 You're.
spk_0 Yeah.
spk_0 Yeah.
spk_0 Trying to give a.
spk_0 Figurative hint there.
spk_0 Yeah.
spk_0 You can see this.
spk_0 Inside.
spk_0 This is both the same.
spk_0 So once it's syntactic sure.
spk_0 And then the other.
spk_0 I sure I sure I am.
spk_0 That's all.
spk_0 What else?
spk_0 Just not of something but I can't remember.
spk_0 Right.
spk_0 So now how did the clear.
spk_0 Oh right now.
spk_0 The clear pointers and know the two operations right the address of or reference operator and the reference operator or confess of operator.
spk_0 Oh and lastly know the relationship between.
spk_0 This is important relationship.
spk_0 Between arrays.
spk_0 And pointers.
spk_0 So an array name is.
spk_0 In relation to pointers.
spk_0 A pointer to the first.
spk_0 Yeah exactly.
spk_0 Eraser array name is a pointer to the first element.
spk_0 So you've been using pointers all along.
spk_0 Believe it or not.
spk_0 And actually the subscript operator is sort of like a special dereference or a contents of operators.
spk_0 So when you use a subscript operator, a subscript notation right the two brackets.
spk_0 You're actually saying go the reference step particular entry.
spk_0 You're right.
spk_0 And use it like a normal variable.
spk_0 Right.
spk_0 I think that's it.
spk_0 And really the big one is going to be.
spk_0 Yeah it's going to be this.
spk_0 So we spend the most time after the best arm out this.
spk_0 All right any questions.
spk_0 No, it's good.
spk_0 Question.
spk_0 Yeah.
spk_0 Okay are there any questions at all like about the final or any course deal.
spk_0 Yes.
spk_0 Just in general is the format going to be pretty much like the midterm where you just like give us.
spk_0 Several problems to say there's more is it going to be smaller scale snippets.
spk_0 And I know the two problems in the midterm are pretty small.
spk_0 In retrospect.
spk_0 Yeah the final is going to be a scene format is just going to be three to six questions.
spk_0 Whereas the midterm was like one to three questions.
spk_0 So yeah, definitely expect like the same format.
spk_0 And I realize I'm a very top grader so I usually scale.
spk_0 So as you guys all.
spk_0 Yes.
spk_0 So the program you're going to do are they going to be like the corner of that version definition.
spk_0 Yeah, but you'll I'm going to ask you to remember function now.
spk_0 So or I might ask you to the clear class and then write a main function that uses the class.
spk_0 So.
spk_0 So I may ask you for a main function here.
spk_0 So like in that case like you could declare class but actually I'm going to write function definitions and then.
spk_0 Just implement it to use kind of like that quiz problem I think.
spk_0 I think like with the stack like right.
spk_0 You don't actually have to write the details of its usage just what might you need.
spk_0 Right so for the quiz I asked you like design of stack class.
spk_0 It comes up come up with the declaration of that class.
spk_0 Yeah, there could be there could be questions like that.
spk_0 Because I mean like in that case you could have written the declaration and you could have written a main file.
spk_0 Exactly right.
spk_0 I might give you like a class decoration just a declaration without the definition and say right.
spk_0 A main program to do this with you know or right or given this declaration give me the definition.
spk_0 Okay, so maybe a good thing to exercise would be to take the class.
spk_0 Decrelation of the stack like we had in the quiz and right of course find definition.
spk_0 So.
spk_0 As far as I mean like really it's just like a math class I think like the more practice you get.
spk_0 And I think in the back of the book there's a bunch of like questions about chapters and stuff.
spk_0 I mean be good to practice that.
spk_0 So I don't think it's like a whole lot of memorization that you need to hear.
spk_0 I mean yeah you don't have a cheat sheet with like syntax and stuff but I think the more you practice it.
spk_0 You know the more you kind of like memorize the syntax right like it's sort of a byproduct I think.
spk_0 So it's just treat like a math class just do a lot of props.
spk_0 And I think there's plenty of small like problems in the back of the book.
spk_0 Or just you know go over solutions on take decorations make definitions take definitions make or take you know main programs or you know different ideas.
spk_0 Any other questions?
spk_0 Yes the ampersand on the my stack equals ampersand my stack.
spk_0 Is that my s pointer to assign the address of my stack yet so that print out the exit my address.
spk_0 Oh there's no print statement here but if you're asking if there was yeah so if you said see out insert s pointer.
spk_0 For example.
spk_0 So the question is well what if I said this see out insert my underscore s pointer and that stands for pointer stack rather.
spk_0 So just that player.
spk_0 So yeah so what would that print?
spk_0 So the only two legal values for pointers are either memory addresses or zero.
spk_0 Zero means that the pointer just points nothing.
spk_0 Yeah that prints the memory location and yeah it's probably a hexed decimal number and may or may not come out on your screen.
spk_0 It depends on the terminal terminal and the relation and stuff like that so nothing meaningful anyway.
spk_0 So if you actually want to print.
spk_0 Well my s pointer is actually a pointer to stack object.
spk_0 So maybe there's maybe want to print the top most element.
spk_0 So if you want to print the top most element you can say see out select out my underscore s pointer select the top.
spk_0 So that peaks at the top if you have a top number.
spk_0 So yeah so printing just the address of the stack in memory is not really meaning.
spk_0 So if you had a stack of integers that would just print like the top most.
spk_0 Good question.
spk_0 I think I'll.
spk_0 All right well if you guys.
spk_0 Yeah good.
spk_0 Do we need to use like a certain or anything or steam out?
spk_0 Oh um if you need any functions like I'll give you the declarations.
spk_0 So you don't have to memorize like the libraries.
spk_0 So if I say you have like it's just like in the midterm I say you have this function available to you.
spk_0 Don't be like that sort of situation.
spk_0 So like there you know you need to know how to read like a function that relation and know how to use it.
spk_0 And then again note a difference between this really goes to anything with the midterm.
spk_0 No difference between using functions declaring functions and defining functions right.
spk_0 So I see a lot of times you go like to call functions like.
spk_0 Like for example the mean function.
spk_0 I believe was a double right so the declaration of that function was double mean.
spk_0 Um.
spk_0 It was items or integers I can't remember.
spk_0 So something like this.
spk_0 Right so when you go to call that function you know make sure you're calling it.
spk_0 Using an actual right now like my or I or something like that.
spk_0 You know leave out the brackets there and you know maybe it's size.
spk_0 So note there's just between parameters and arguments.
spk_0 So here you have you have a function declaration here you have a function call.
spk_0 So these are the formal parameters right.
spk_0 These are like the names of the slots of the function and these are actually what you're putting in those slots, which are the arguments.
spk_0 Sometimes I see this a lot.
spk_0 So I see like call mean and then yeah.
spk_0 So that's obviously that's the declaration right so this you want to use you want to call.
spk_0 So that's wrong.
spk_0 I mean it's really analogous with with declaring variables right.
spk_0 So I have an integer of say new.
spk_0 And if I want to sign new to like let's say five right I would say someone that's right new design five I want to say and new.
spk_0 Is a sign five because that would be re declaring.
spk_0 That's great.
spk_0 Any other questions?
spk_0 You mentioned that.
spk_0 I mean focus on the operator overloading so with like the ice cream, ice cream stuff we did with like the fraction or fall under that category because it.
spk_0 The operator.
spk_0 Right. Yeah.
spk_0 Yeah.
spk_0 Yeah.
spk_0
spk_0
spk_0
spk_0
spk_0
spk_0 It was definitely tricky.
spk_0 Is it was it more of like a precursor to like upcoming classes of your continue on to deal with like files dreams and stuff with files.
spk_0 And actually files.
spk_0 So it's just be covered in the previous last.
spk_0 So if you take two or two for example.
spk_0 Yeah, you do file streams.
spk_0 Yeah, I do file streams sometimes in the sometime I mean we did a little bit of files.
spk_0 And this class and sometimes it is some quarters of the files dreams.
spk_0 But I have an example up with last class right.
spk_0 How to use files.
spk_0 It's really similar to using.
spk_0 Yeah.
spk_0 Yeah.
spk_0 Yeah.
spk_0 Yeah.
spk_0 If you're interested I can read your right to use this material.
spk_0 So.
spk_0 But really with all that stuff like the operator overloading just so you know like you could read documentation of C++ code.
spk_0 So you're not completely lost.
spk_0 It's a big part of C++.
spk_0
spk_0 I was just curious.
spk_0 Yeah. So if someone tells you like okay you know.
spk_0 This this operators overloaded for this class you know what that means.
spk_0 And by the way.
spk_0 One thing I kind of forgot to talk about class was really the exploit this relationship between arrays and pointers.
spk_0 So if you if you if you have a for example a function that passes in the right.
spk_0 Well how else could you write this.
spk_0 So if I didn't want to use this bracket.
spk_0 I think that's good.
spk_0 I think that's good.
spk_0 That goes for everyone.
spk_0 So if an array me if an array name is really just a pointer.
spk_0 When what did you send here.
spk_0 You can point it right so you can in your declaration you actually can say that you don't really take an array but you take a point.
spk_0 So actually these are well.
spk_0 So if you see a function declaration where you see an in star right integer pointer.
spk_0 These are the same thing.
spk_0 Sorry.
spk_0 Really documentation dictates.
spk_0 If it's actually a pointer to a single element right a single integer or is it an array of integers.
spk_0 Likewise if you're going to return in the right.
spk_0 You can also say in star like I had a sub.
spk_0 A sub array function I think that take given an array.
spk_0 Returns another array that took a sub a sub array of that one.
spk_0 So you can write some of the guess tip.
spk_0 I'll see.
spk_0 And that would be the same thing as that.
spk_0 So these two locations are the same.
spk_0 So this star is seeing things practice.
spk_0 So it's the advantage to.
spk_0 Well one thing.
spk_0 Well I guess when you think about the relationship between a raise and pointer so your rating is actually a constant pointer.
spk_0 The first element right of the array where this is not a constant pointer.
spk_0 It's just you actually can change this value right unless you do something like this.
spk_0 It's a little more flexible.
spk_0 So let's just say const in star here and that value that that that pointer can change the patient.
spk_0 So you actually can make after this call this constant keyword wasn't here.
spk_0 You can make this argument or rather this argument here point to another right.
spk_0 Which will be strange.
spk_0 But you may have a function that wants to do that.
spk_0 So it's a tumor flexible.
spk_0 So just to me or P. We said or clarify if if it's not a constant like you don't have constant there that that array could exist anywhere in memory.
spk_0 I mean randomly like, um, it depends on the definition.
spk_0 I'm going to be random really, but it'll be what will the definition assign to you.
spk_0 So it can say like AR is assigned some other new array.
spk_0 Which would be a little dangerous.
spk_0 It depends on the function.
spk_0 Because I mean, I'm just because if you don't declare this constant, I mean, what does it matter?
spk_0 I just say star ray versus constant.
spk_0 Oh, so the difference between the constant star AR quite sure.
spk_0 No, no difference.
spk_0 Same.
spk_0 Yeah, it's the same thing.
spk_0 Yeah, they're both constant pointers to some piece of memory.
spk_0 So what would you consider a pointer that isn't constant?
spk_0 Um, if you remove the cost, you were here.
spk_0 You have it like I had it before.
spk_0 Like here.
spk_0 Yes, that's a non constant reference.
spk_0 So non constant.
spk_0 So that you can check.
spk_0 So in abstract way, what would that mean?
spk_0 If you were to describe.
spk_0 So if you had the definition of let's say the mean function.
spk_0 So that wasn't constant.
spk_0 I mean, let's say this is going to be like a really bad.
spk_0 A really poor mean function definition.
spk_0 So you call this mean function to get the average of the values in some array.
spk_0 And so this let's say it's your definition for the means.
spk_0 It will be.
spk_0 So we'll use a non constant pointer.
spk_0 To an integer.
spk_0 So we left that constant key right out there.
spk_0 Or didn't use this observation right that indicated that was in the right.
spk_0
spk_0 And the definition of since.
spk_0 So AR is assigned my AR work my AR is another right.
spk_0 So let's say Maine was calling this.
spk_0 Actually, this one swap in memory.
spk_0 So if there was a call to mean right given.
spk_0 And you know, let's say some array.
spk_0 Some AR I will say five.
spk_0 You know, there's some values in there.
spk_0 And you call mean right some AR is five values.
spk_0 And now you print.
spk_0 Let's say the first element.
spk_0 Of some AR could be completely different.
spk_0 So this can print.
spk_0 Let's say this print six.
spk_0 And then before this I said.
spk_0 See out.
spk_0 Some AR sub zero.
spk_0 And this would print let's say five.
spk_0 So it could be entirely.
spk_0 So basically when you declare some array here and number either spread their space for this array.
spk_0 And so many are is a constant pointer to this element in memory.
spk_0 If it switches, then wherever my AR is.
spk_0 Now that's gone.
spk_0 Now points over here.
spk_0 So you can flip flop.
spk_0 Unless you make a constant.
spk_0 So the only way that could change is if you wrote it to do that.
spk_0 Right, but you may not have this code level to you, right?
spk_0 You may use using a library function.
spk_0 So mean maybe somewhere in the library.
spk_0 But you don't have the code to or maybe you bought it also.
spk_0 We're downloaded.
spk_0 I don't know.
spk_0 So yeah, you don't always have a definition available.
spk_0 So you have the object code, right, which is not human.
spk_0 Let's give a DGo compiler, which may or may not.
spk_0 But yeah, I'm not going to cover race too much.
spk_0 We just know that you know, I declare it.
spk_0 And what operations are about.
spk_0 So the reference.
spk_0 Like I said, the big talk of it's going to be object oriented.
spk_0 So.
spk_0 Great. Any other questions?
spk_0 Okay, so I'm going to go over like a bit of dynamic memory,
spk_0 which won't be on.
spk_0 Exam at all.
spk_0 So if you like, you can stay.
spk_0 Otherwise, you can leave.
spk_0 So it'll probably be, I don't know about 20 bits.
spk_0 Hopefully so.
spk_0 So I started a little bit about talking about this last class.
spk_0 So basically up to now, our memory programs,
spk_0 the memory of our programs are basically laid out by the compiler.
spk_0 And the compiler can do this since it knows how large variables will be.
spk_0 Right. So it knows that all integers are four bytes.
spk_0 So if you declare integer variable, say my int,
spk_0 there's going to be a four byte slot that will be allocated to the memory of your program, right,
spk_0 when you execute your program.
spk_0 Likewise for a raise.
spk_0 The size of the fire of the arrays are all as constant, right?
spk_0 They're either a literal value like five, I have up there.
spk_0 Or they're a name constant, right? Like max.
spk_0 So the compiler knows how much space to allocate for for your variables.
spk_0 And this is why the size of the fire for a raise needs to be constant.
spk_0 All right. So what is a rate, excuse me, the memory look like
spk_0 when you're actually running your program?
spk_0 So.
spk_0 Let's see if I can erase this.
spk_0 So when you're writing your program.
spk_0 Memory that's allocated for your program is actually split in a few different parts.
spk_0 Some of these parts are read only so you can't change the contents of the memory that corresponds to your program.
spk_0 And others are read right like the ones that correspond to your variables and such.
spk_0 And if you guys remember from the very first lecture,
spk_0 I think we talked about the bond, no even architecture.
spk_0 And the big revelation with this architecture is that both data and instructions or code are stored in main memory
spk_0 and ran the max memory or RAM.
spk_0 So one portion of memory when your program executes is for the code of your program.
spk_0 It'll be in the machine code.
spk_0 So you think we'll get memory when your program is running.
spk_0 So one portion of your memory is going to be the code for your program.
spk_0 And this is read only so you can't change.
spk_0 You can't change that content's memory.
spk_0 There's also a special variable maintained by the runtime system with the operating system called a program pattern.
spk_0 Or for short, it's called PC.
spk_0 So the program counter actually keeps track in this piece of memory.
spk_0 So these are separate instructions here that correspond through your program.
spk_0 To this value is actually going to be like the current instruction.
spk_0 And it keeps on switching as your program goes along.
spk_0 And if you have like an if statement right initial statement, actually you can jump.
spk_0 If you have a loop it can jump at different locations. It's not always the plan.
spk_0 So anyway, there's some loop.
spk_0 There's like a catter that's called the PC that counts.
spk_0 That has the source of value of the instruction.
spk_0 We're the location memory of the current instruction.
spk_0 Okay, so that's that part of memory.
spk_0 So another part of memory is for the data, right?
spk_0 That your program will manipulate or read from.
spk_0 All right, so that's for code.
spk_0 This is going to be for data.
spk_0 So the data is going to be basically variables.
spk_0 Constance parameters.
spk_0 That your program manipulates.
spk_0 And many of these are actually stored in the data structure that runs along with your program called the runtime stack.
spk_0 We're also called the call stack. And actually this is the same kind of stack that we did for the quiz.
spk_0 So, so one portion of memory that corresponds to your data is called the call stack.
spk_0 And that's where a lot of these variables constants of parameters are going to live.
spk_0 There's a caveat to that global parameters and global constants are not stored on the stack unfortunately.
spk_0 So there's another space in memory for that.
spk_0 We won't get into that though.
spk_0 Since global variables are bad.
spk_0 Actually, global constants are okay.
spk_0 And they're stored in a separate portion of memory.
spk_0 But most of the constants we've seen are actually, you know, within the find within function.
spk_0 Okay, so.
spk_0 So this call stack actually is like a stack we have in the quiz, right?
spk_0 That you guys designed.
spk_0 And records get pushed on the stack.
spk_0 In fact, each element on the stack is called an activation record.
spk_0 And when a function is called a corresponding activation record is pushed onto the stack.
spk_0 So when you call a function and the very first function that gets called is which function.
spk_0 Start to the M main, right?
spk_0 There's a corresponding entry that gets pushed onto the stack for that function.
spk_0 And this entry has a couple different things or a couple of different kinds of information about that function.
spk_0 And the function that is currently running or executing is always on the top of the stack.
spk_0 So for example, this is the activation record for main.
spk_0 Main calls mean.
spk_0 Or I'd say main calls.
spk_0 Wow, it's not readable main.
spk_0 Let's say calls standard deviation.
spk_0 And standard deviation calls.
spk_0 Right, because that was part of the equation for standing.
spk_0 So every time a function is called it gets pushed on the stack.
spk_0 And the top most function, right?
spk_0 You think about the top member function of stacks.
spk_0 That's the one that function that's currently executing.
spk_0 When a function completes, right?
spk_0 When either it's a value returning function, maybe the return keyword is encountered or just to end the function hits that activation record for that function is popped off the stack.
spk_0 Along with that popping the activation record for functions.
spk_0 One thing that's on the activation record is something called a return address.
spk_0 And that is the address of the instruction and memory to go back to.
spk_0 So it's the value of the program.
spk_0 So that wherever so when standard deviation called mean, right, it can continue from the same place left off, right?
spk_0 So it doesn't start from the beginning, for example.
spk_0 However, one thing to note though that on this record are also variables in parameters for the function.
spk_0 So you got a variable here.
spk_0 Let's call, you know, called some, right?
spk_0 A lot of people had some variables in their mean functions.
spk_0 Once this is a pop off the stack, that variables gone.
spk_0 And this is where actually where a lot of our school ping rolls come from.
spk_0 So any information on the activation record when it's popped like the value of variables is lost.
spk_0 And consequently, the value of local variables declared in functions are thrown away.
spk_0 So they're gone.
spk_0 And this is why we actually haven't returned a rate in function.
spk_0 So we haven't had really any function that relations where the return type was in array.
spk_0 Right? Right? For example, good, an integer, right?
spk_0 Good for some like that. We should say that.
spk_0 And start. Right.
spk_0 And it's a very good reason for that.
spk_0 Okay. So value returning functions combat this problem, right?
spk_0 That the activation records gone. That values are gone for every local variable, local variable being a variable that was declared in the function definition.
spk_0 So value returning functions combat this problem by returning a value to the caller.
spk_0 Right? So they used a return keyword.
spk_0 You returned the, let's say the value of an expression or a value of variable.
spk_0 So when a function returns a value using the return keyword, for example, a copy of that value is sent back to the caller.
spk_0 Cool.
spk_0 So if you think about the standard deviation function calling the mean function, right?
spk_0 And let's say the mean function returns sum over n, right?
spk_0 Which n corresponds to the number of elements in your right.
spk_0 So a copy of this of that expression, some divided by two, which includes the value of the variable sum is returned to the caller, which happens to be standard deviation.
spk_0 And that gets stored in a local variable of standard deviation.
spk_0 So it sort of persists in that, in that sense.
spk_0 It goes on. It's not completely dead.
spk_0 So from most variables, this is not actually a problem, right? Because you can use a return keyword.
spk_0 However, this is why the relationship between arrays and pointers is very important.
spk_0 Recalls arrays are, recall that arrays can only be passed by reference.
spk_0 And there's a very good reason for that. The array name is a point, right?
spk_0 So anytime you want to talk about array, you always use a pointer.
spk_0 So you'd have to pass a pointer to the array, right? To the first element of the array, namely the array name.
spk_0 So there's no other way to pass an array.
spk_0 The array, really, the array name is a reference. It is in a memory location.
spk_0 So the only way you can return pass arrays is by reference.
spk_0 And this also goes with the returning arrays as well.
spk_0 So that's because the array name is a pointer, ie memory address of the first element of the array.
spk_0 And if the array is allocated on the activation record.
spk_0 So for example, that one of these local variables was an array, like that.
spk_0 That's gone.
spk_0 Once it's popped.
spk_0 So the return address of the first element of the array does us no good because an element no longer exists, right?
spk_0 All the local variables, all the variables in this function definition are gone once the function ends.
spk_0 So if you think about it,
spk_0 let me go back to my.
spk_0 Let's see here.
spk_0 The sub array function.
spk_0 So this function took, um, some are right.
spk_0 This size, right? And then we're not right.
spk_0 And.
spk_0 The number of elements to sub array will have.
spk_0 So for example, if you had an array like this.
spk_0 Let's see, this is where it points to you.
spk_0 This is the size minus one.
spk_0 So zero.
spk_0 And then you just chop off from the beginning.
spk_0 None elements.
spk_0 So let's say none was like two or something like that.
spk_0 So you would return another, right?
spk_0 So this would be the return to right.
spk_0 That just has these two first elements.
spk_0 That's like an x and y.
spk_0 More x and y or some other.
spk_0 So that would be a two on the right.
spk_0 So it's like a sub array of that one.
spk_0 Well, like in the term sub strain or subset sort of a similar sub sequence similar type of situation.
spk_0 Okay.
spk_0 So we return another, let's say integer.
spk_0 So those are function decoration for that guy.
spk_0 Um, so one of the limitations of this would be it would be as follows.
spk_0 And I think it's even though it's not going to be an exam,
spk_0 I think it's a good way to review.
spk_0 For the final.
spk_0 So let's see.
spk_0 So.
spk_0 We can make another right.
spk_0 Right.
spk_0 Let's call it right.
spk_0 So it's.
spk_0 In array.
spk_0 And let's say there's some global.
spk_0 I think we don't know this last time.
spk_0 There's some global cost available to us.
spk_0 That's the maximum number.
spk_0 Any rate system.
spk_0 We'll get away.
spk_0 We'll use an M Galocation give her that as well.
spk_0
spk_0 All right.
spk_0 In a pot.
spk_0 Yeah.
spk_0 We did over this last time.
spk_0 And a possible implementation would be.
spk_0 You know, start by zero.
spk_0 And then while I is less than noon.
spk_0 Five plus plus.
spk_0 And we just said red.
spk_0 So by this assigned.
spk_0 Arr.
spk_0 So we just copies of first new elements.
spk_0 They are are into red.
spk_0 Basically, you have this situation right here.
spk_0
spk_0 So this one's copy here.
spk_0 This one's copy there and up to none.
spk_0 Or you.
spk_0 Okay.
spk_0 So I guess natural thing to do.
spk_0 Be as follows.
spk_0 Return.
spk_0 So this function returns a pointer right to the manager.
spk_0 Red is an integer array.
spk_0 And your name is just a constant pointer.
spk_0 To the first element of your right.
spk_0
spk_0 So if you follow red.
spk_0 Right.
spk_0 Here it is.
spk_0 It points to the first element.
spk_0 So that's fine.
spk_0 That checks.
spk_0 Right.
spk_0 That's a memory location.
spk_0 It returns a memory location.
spk_0 Good to go.
spk_0 I could have also used a bracket here as well.
spk_0 So what's the problem with this.
spk_0 Even when I said over there about activation records and call stacks.
spk_0 So there's a main function.
spk_0 It calls this function.
spk_0 Maybe at the clearest point right.
spk_0 In star U.
spk_0 And it says new right.
spk_0 Right.
spk_0 Is assigned a return value.
spk_0 Of sub array of some other array.
spk_0 Let's say now I wanted to print the first element.
spk_0 But let's assume new arrays on it.
spk_0 So what would I print.
spk_0 And guess.
spk_0 Yes.
spk_0 Well, you're using the subscript operator.
spk_0 So yeah, so think of your right here.
spk_0 You are is just in a rating.
spk_0 Is a rating is our point.
spk_0 Right.
spk_0 The first element.
spk_0 So this is a point of the first element.
spk_0 So the reference that.
spk_0 Right.
spk_0 And we get the actual value.
spk_0 Of that element.
spk_0 So let's look at that.
spk_0 So this executed right.
spk_0 So we made this new.
spk_0 This array inside this function definition call it right.
spk_0 Here it is.
spk_0 Never.
spk_0 We return.
spk_0 Right.
spk_0 Right, which is the location of the first element.
spk_0 This array.
spk_0 But this.
spk_0 This array is where.
spk_0 Actually both of these.
spk_0 They're all an activation record on the stack.
spk_0 Well.
spk_0 Actually this one really is.
spk_0 It's a bit tricky actually.
spk_0 Let me change this picture.
spk_0 It's really like this.
spk_0 So the activation record ends here.
spk_0 And a are.
spk_0 It's actually on here and it points here.
spk_0 So AR is in the stack right.
spk_0 Because it's a parameter.
spk_0 It's on the activation record in vocal variables like red.
spk_0 Are on the stack as well on the top of the stack.
spk_0 While it's executing.
spk_0 When you hit return that ends the function right.
spk_0 No more execution.
spk_0 So what happens is the activation record.
spk_0 Pops got right.
spk_0 This one's still here because this is this is from the caller.
spk_0 Right.
spk_0 But this is from the call.
spk_0 This is from the call function.
spk_0 So red.
spk_0 This array that.
spk_0 Red points to the first element of no one exists.
spk_0 Got it.
spk_0 So this value here in UAR.
spk_0 Actually points.
spk_0 Garnish.
spk_0 It's.
spk_0 Something called a dangling point.
spk_0 It's a pointer whose value is a memory location where nothing is allocated.
spk_0 In fact, this will probably crash a program.
spk_0 This is why we have returned to raise.
spk_0 Because local variables like red.
spk_0 Right.
spk_0 Local to function definitions are allocated on the stack.
spk_0 And every time the function ends the activation record and where that local
spk_0 variables is is allocated is pot.
spk_0 So that's a problem.
spk_0 Does everyone see the problem?
spk_0 Yeah.
spk_0 Good.
spk_0 And you can't make a copy of it.
spk_0
spk_0 So you can't make a copy of the whole thing.
spk_0 Right.
spk_0 Because you're right actually is the definition of an array.
spk_0 Right.
spk_0 Name is a constant point into the first element.
spk_0 Is it clear for everyone?
spk_0 All right.
spk_0 So consequently allocating a raise that needs to be returned by the function on the stack is not useful.
spk_0 Because once a function is finished that array no longer exists.
spk_0 So.
spk_0 In summary, there are several limitations with the call stack for storing values of variables that includes returning to raise like we have here.
spk_0 And also that the structure of the activation record is laid out by the compiler.
spk_0 When you compile your programs.
spk_0 So the compiler lays out in a nice format.
spk_0 How many variables it needs?
spk_0 What the size of each variable will be?
spk_0 And the operating system when you execute like eight out of for example, it replaces all those offsets with the actual memory locations.
spk_0 So, um, demand of memory needed must be known by the compiler at compile time.
spk_0 However, sometimes we don't know how much memories required until we execute the program.
spk_0 Right.
spk_0 Here's here's an example.
spk_0 Actually, that's not an example.
spk_0 Never mind.
spk_0 An example would be what the arrays have been using so far.
spk_0 So we combat this problem with by the clearing arrays they have a maximum number of elements right using a constant and the constant.
spk_0 And then we have another notion of size with a raise called the actual number of elements.
spk_0 So you have a maximum and even actual.
spk_0 And we set this maximum number at compile time.
spk_0 So you guys remember we're talking about like making a sort of like grading application.
spk_0 We said, OK, Ohio State, you know, there can be no more than 100, 100,000 students.
spk_0 Right.
spk_0 However, I think there's about 50,000 students here at this university.
spk_0 So you're actually using double the space.
spk_0 But we said we had a very conservative approximation.
spk_0 We said, OK, most of the kid be 100,000 students in Ohio State.
spk_0 So we wasted half the amount of space, right.
spk_0 And for program something for like a moat or a cell phone that has a limited memory.
spk_0 That could be a problem.
spk_0 Also, that memory will not be available to other programs that are running on your computer at the same time.
spk_0 On the other hand, you know, there may be more than 100,000 students at Ohio State at some point.
spk_0 Depends on how long your program is going to be running for it, right.
spk_0 So maybe next year, you know, Ohio State is extremely popular and another 50,000 students enroll.
spk_0 So we're just starting off in this case, the program needs to shut down.
spk_0 And you need to recompile the code, right.
spk_0 Change the constant.
spk_0 But it's almost feasible for all types of programs, right.
spk_0 So for example, if you're thinking about a server application, right, that all is needs to be running.
spk_0 Like a security system, right.
spk_0 Showning it down, recompiling the code is not really an option, right.
spk_0 So.
spk_0 Right. And also, just in the maximum that's too large is a waste of memory.
spk_0 And it may also call something called thrashing.
spk_0 Is everyone anyone familiar with this term, thrashing or memory, thrashing?
spk_0 So what happens is that if your program requests more memory than you actually have in your RAM,
spk_0 there's something called virtual memory that's a notion made by the compiler.
spk_0 So what the, excuse me, the operating system, the operating system, what it does is that if you need more memory than you actually have,
spk_0 it allocates a space on your disk on your hard drive to sort of like swap by the contents of RAM
spk_0 from other programs that are running or even the same program that's running,
spk_0 while new data gets stored in RAM.
spk_0 So this is something called a context switch.
spk_0 It swaps, it swaps out memory to disk.
spk_0 So it's sort of like makes your hard disk like a portion of RAM.
spk_0 What's the problem with that?
spk_0 And you think it guess?
spk_0 How fast is your hard disk compared to RAM?
spk_0 Unless you have a solid plate drive, right.
spk_0 It's pretty slow.
spk_0 And who can afford those, right?
spk_0 Yeah, so.
spk_0 So if it's using this virtual memory as part of your hard disk, that could be very slow.
spk_0 And thrashing occurs when the amount of memory required by your program will not fit into RAM.
spk_0 And portions of memory are fed in and fed out through your hard disk and through memory.
spk_0 So you're basically your operating system is constantly swapping data between the hard disk and memory.
spk_0 And that's called thrashing.
spk_0 And it makes your computers, your program is very slow actually.
spk_0 And very inefficient.
spk_0 So there's a portion of memory reserved for your program when you run your programs called the heap.
spk_0 And this is where dynamic memory can be allocated.
spk_0 So the stack is in the only place where data can be allocated for your programs.
spk_0 There's also something called the key.
spk_0 And this is a special portion of memory where you can request allocation dynamically as your program is running.
spk_0 So when your program needs extra memory, it makes a special call to the operating system.
spk_0 And C++ is actually kind of hidden.
spk_0 So it's actually an operator.
spk_0 And the product that's their language of this function.
spk_0 So they actually made it an operator in C++.
spk_0 And it's also called dynamic memory allocation.
spk_0 So what the operating system does is once it receives this call from your program.
spk_0 If there's some heap for whatever kind of memory you're going to be requesting, then it allocates.
spk_0 You know, maybe it's an array, maybe it's a single variable.
spk_0 It allocates that spot on the heap and returns the address of that memory location, right?
spk_0 The address of that memory on the.
spk_0 So it gives you a pointer on the heat to the requested memory.
spk_0 If it's available.
spk_0 If this heap is full and it's bounded, it can be full.
spk_0 Anyone guess what that returns on with the operating system gives you?
spk_0 So this is full, right?
spk_0 Let's say you've allocated all the memory that you can on the heat.
spk_0 And you request more.
spk_0 But what would you guess the operating system does?
spk_0 Blue screen.
spk_0 Blue screen.
spk_0 It depends.
spk_0 It actually returns a pointer value zero, which is the special Sentinel value, right?
spk_0 Or points nothing.
spk_0 It's also called a null pointer.
spk_0 And there's a keyword in C++ actually with capital letters null, which basically is just zero.
spk_0 So it's a pointer that points nothing.
spk_0 When your program is done with the extra memory that you're requested, you can make another call to the operating system.
spk_0 Instructing it to release that memory so it's available in the future.
spk_0 So this doesn't happen.
spk_0
spk_0 Anyone heard something called memory leak?
spk_0 Actually fire five said like a big problem a couple years ago with memory leaks.
spk_0 So memory leaks occur when you keep on requesting memory on the heat and never releasing it.
spk_0 And as your program runs for a long time like a browser, probably you have opened like a bot.
spk_0 Or maybe Windows your operating system is open for quite a little time.
spk_0 Your memory just goes down the tube and just get drains and drains it.
spk_0 Does anyone have to like reboot the computer after a couple days?
spk_0 Right?
spk_0 Guess what?
spk_0 It's probably due to memory.
spk_0 Not being released on the heat.
spk_0 So let's take a look at a couple of examples how to do this.
spk_0 And by the way, this is going to solve our problem with this function here.
spk_0 So to get a piece of memory on the heat of a scalar type, it's actually really simple.
spk_0 The operator is called new.
spk_0 So let's say we want to allocate a single character on the heat.
spk_0 To do this, we're going to do the clear a pointer to a character.
spk_0 Because remember anything on the heap is going to work.
spk_0 You can only access it through a point.
spk_0 And this is actually the main motivation of pointers.
spk_0 There are other motivations of well and see a pointer and see plus plus.
spk_0 But this is really the main one.
spk_0 So you don't know exactly where is what location of memory this will be out here.
spk_0 You don't even know the offset.
spk_0 So that's why you need a pointer so we'll call it CPTR.
spk_0 And you can sign CPTR to be the location of a new character on the heat.
spk_0 So the way you do that is use the new operator followed by a type.
spk_0 So this is the app.
spk_0 This is the typing.
spk_0 So there's a new character on the heat.
spk_0 And the address of that character is returned in CPTR.
spk_0 Okay, and you can use CPTR just like any other pointer, right?
spk_0 So you can do your reference it.
spk_0 And you can say CPTR is a sign name.
spk_0 So, A gets this one here.
spk_0 All right, to release memory from the heat, you use.
spk_0 Not prior to call delete.
spk_0 And delete is a bit misleading.
spk_0 It actually doesn't the week the memory.
spk_0 There's also things actually deleting memory and computer programs.
spk_0 You always overwrite memory.
spk_0 So it's the lead followed by.
spk_0 The pointer name.
spk_0 And what delete does really.
spk_0 It just marks.
spk_0 The value of the memory locations stored in CPTR as dirty.
spk_0 And by dirty, I mean can be overwritten.
spk_0 So it's available for other.
spk_0 The memory locations on the heat to be allocated.
spk_0 So it doesn't really delete it, but it says, okay, this is a bill.
spk_0 Just mark it.
spk_0 So that's for scalar types, right?
spk_0 For single variables.
spk_0 You can also do this with objects.
spk_0 So for example, it's the clear an array of pointers to 100 fractions.
spk_0 So you can say fraction.
spk_0 Pointers.
spk_0 FAR.
spk_0 I'll say 100 of those.
spk_0 Of course, you can use in the inconsiderate here.
spk_0 But for time sake, I'm not going to use one.
spk_0 Okay.
spk_0 Well, what's every counter for that?
spk_0 So 100 in the maximum size.
spk_0 Say, a count.
spk_0 It's going to be the actual size.
spk_0 So right now there's no elements in that array.
spk_0 And the way I allocate.
spk_0 And you fraction on the heat is this falls.
spk_0 So actually FAR is on the stack.
spk_0 So you look at the stack here.
spk_0 FAR, which is an array.
spk_0 Right.
spk_0 Points to an array on the heat rather than the stack.
spk_0 So it starts at zero.
spk_0 One ends at 99.
spk_0 Okay, right now these are all garbage.
spk_0 And these are, these are, this is space for pointers to fractions.
spk_0 Okay, name it ACAM.
spk_0 So this is on the activation right here for this function.
spk_0 So ACAM is initially zero.
spk_0 All right.
spk_0 So we can say FAR.
spk_0 So ACAM, which would be the first element of FAR.
spk_0 It's really hard thing to say.
spk_0 Is assigned.
spk_0 And you fraction.
spk_0 Now, when that executes.
spk_0 On the heat, there'll be a new fraction for us.
spk_0 And anyone guess what the values of that would be?
spk_0 It's six times.
spk_0 So what constructor gets called here?
spk_0 Would you guess?
spk_0 When I say new fraction.
spk_0 Yeah, default.
spk_0 Right.
spk_0 So it'll be zero and one.
spk_0 And now this element of FAR points to that fraction of the heat.
spk_0 And even there's an old loop.
spk_0 You can keep on going.
spk_0 So at the end, you would have, let's say, a hundred.
spk_0 A hundred pointers to fractions and a hundred fractions on the heat.
spk_0 And you can also read, right?
spk_0 You can say FAR.
spk_0 Sub ACAM.
spk_0 All right.
spk_0 So let me the first one there.
spk_0 Arrow.
spk_0 So that one read it from.
spk_0 Or you can use the insert the right, the insertion operator.
spk_0 Extrash not right or right.
spk_0 Okay.
spk_0 Cool.
spk_0 All right.
spk_0 So let me wrap up here.
spk_0 All right.
spk_0 So getting dynamic memory for non-scaler type.
spk_0 So arrays, for example.
spk_0 You can do something like this.
spk_0 You can write up here.
spk_0 So you declare pointer right to integer.
spk_0 And I'll call this D-A-R-R.
spk_0 This is going to be a dynamic right now.
spk_0 And the way you write this is that you say new.
spk_0 R-R-R.
spk_0 Up, excuse me.
spk_0 Actually, it's a type of name, right?
spk_0 So new.
spk_0 And then the size wasn't firing.
spk_0 You actually have a very little.
spk_0 And.
spk_0 And it's a variable.
spk_0 So those doesn't need to be constant anymore.
spk_0 So now memory, there's some right.
spk_0 And in fact, we can do that here.
spk_0 So we can say we can switch us to VS follows.
spk_0 We can say, right, in right, because the pointer.
spk_0 Is assigned you integer right on the heat of max.
spk_0 I mean, that you can make a variable.
spk_0 Everything's neat to sing.
spk_0 Is that cool?
spk_0 The only problem is that here you have to delete.
spk_0 You're right, and you're done with it.
spk_0 The way that is used the link,
spk_0 he worried followed by a bracket,
spk_0 and closed bracket, and then followed by the right.
spk_0 So that will release it.
spk_0 And now this works.
spk_0 Because this array is allocated on the heat.
spk_0 And when the stack activation record for this function is popped,
spk_0 it returns the value of an array that's on the heat,
spk_0 which persists after the function is over.
spk_0 Any questions on it?
spk_0 So that's how you release memory for non-scaler types for a race using the link,
spk_0 key word, followed by open, closed bracket, and then.
spk_0 So for normal ones, you just say,
spk_0 just a single delete, right, to release it like this one here.
spk_0 And then, for example, if you want to release this one,
spk_0 you say delete, open, closed bracket, followed by the array.
spk_0 And if you don't delete them, they become memory leaks.
spk_0 So once you remember, when you request memory,
spk_0 you always need to release it as well.
spk_0 I think that's all I want to say.
spk_0 So that's good.
spk_0 We got done almost now.
spk_0 Any other questions?
spk_0 Okay.
spk_0 Well, that's all.
spk_0 And do a freeze-day afterwork yet.
spk_0 My thoughts on you, but I do.
spk_0 Even though it's your own word.
spk_0 And take over these.
spk_0 Because like, you have to have that shoulder.
spk_0 Good luck.
spk_0 Good luck on Wednesday.
spk_0 Thanks for coming.
spk_0 Bye.