All right, let me help you with 9.8.
My Programming Lab. I'm hitting the Plus sign. And I'm going to Week One. And I'm going to Chapter Nine. And I'm opening up 9.8. And I'm clicking here.
It says write the code for call to the new operators, so we have to use a new operator. And it will dynamically allocate memory for a single integer value.
Now there's two different parts of memory. One is the stack. And it is controlled automatically by C++. And it's allocated, cleaned up, the whole works by C++ automatically.
The other part is called the heap. And the heap is not controlled at all by C++. The heap is actually under your full control. So when you create a variable dynamically, you're actually putting it on the heap. And to learn more about this, read your textbook and also, you can Google. So you can say C++ stack versus heap. You can see, it came up automatically.
So a lot of people are Googling like this, right. So C++ stack versus the heap. And then this is stack versus a heap. You want to read things like this. You can also watch YouTube videos on this. But just really get to know the difference between the two.
The Important part is that C++ controls the stack for you. The stack. And you have to control the heap completely. The heap is a different part of the memory. The stack is actually built at the top of the memory and it goes from 000 down, like that, based on what applications are being opened up, things like this.
The heap is on the other side. The heap is down at the bottom of the memory. And that's built up that way, just by you, actually, creating variables dynamically. All right so you learn more about it by simply googling and reading.
And again, the critical part is remembering that you have to clean up anything that you put on the heap. And also, you have to use the new operator to tell C++ to create the variable on the heap. So it says right here, "Write some code for a call to the new operator that will dynamically allocate memory for a single integer. So all you do is simply say, "New int", like that. If you say a new int, that's going to create a new integer on the heap.
And, just remind you all, any time you use the word new-- so if I come over here and I say, int pointer number is going to be a new integer, and then this new integer-- let me go ahead and initialize it 27-- so that's a full line of code right there. So, with this right here, the new keyword creates a variable on the heap. And then it hands you back a pointer to that new variable. So you need to create a pointer to that variable and that's why I'm calling it an int pointer to that number, OK?
So the number now points to that variable that's sitting over there in the heap. And the only way I have access to it is by using this pointer. So, and that's one of the reasons that we had to learn about pointers and why pointers are so important. So we can create things on the heap and dynamically create them, OK?
Now if you ever use the new keyword, you need to remember you also have to clean it up. So you need to use a delete keyword and clean up that memory. So, if I say delete a number, that actually removes it off of the heap, so that your memory is clean. If you forget to remove the dynamic variable, then by using the Delete keyword-- if you forget to do the delete-- then that variable will sit there on the heap until you close your application, or until you reboot your computer.
And that right there is called a memory leak. And memory leaks used to be just the worst. All the software seemed to have memory leaks. I remember applications just slowing down my programs, my computer. And things would get so slow I had to reboot my computer. And then after a while I would figure out what program was causing a memory leak and then I just wouldn't use that program anymore.
To learn more about memory leaks, and just do "memory leak", and then you can read about what memory leaks are. And then you can also learn how to prevent them. And you could actually add software. You could add code to your applications that show you how to prevent a memory leak. All right?
So it can be prevented and that's why today's applications don't have memory leaks as much. In fact, I have not hit an application in a long time that has a memory leak, which is a good thing. All right?
So just remember, when you use the new keyword, it's putting it on the heap. So what they're asking for you to do is just to write code to call the new operator that will dynamically allocate memory for a single integer value.
So they just want the call, they don't want the entire line like this. They just want the call. So I'm going to go to get rid of this. And I'm going to get rid of that. And, I think they do this might be all that they want. They might even want less but, let's find out. I'll go ahead and hit submit.
All right, there you go. So this is creating a new integer on the heap. And it gives you back a pointer to that new integer that was created down there on the heap.
Write call to the new operator to dynamically allocate memory for a double, whose initial value is 17.3. So, I'm going to say if new double, and the initial value is 17.3, like that, OK? So this is just a call. Now they're not asking for the entire line of code, they're just asking you for a call. I'm going to hit hits submit. And there it is.
All right. Now my question is, what would happen if I would have just created the whole variable. So if I say double pointer, and then I say, number is going to be assigned, and then I come over here and I hit a semi-colon, like this. So this is actually the full line of code that creates the double on the heap. And when it creates that double on the heap, it gives you back a pointer to that double. So I'm assigning that pointer that gives me back to the number variable. So that's why this has to be a double pointer, all right?
So let me hit submit, see what happens. So it did not like it. Do you see that? I haven't yet seen a correct solution that uses the asterisk. And because I'm using the asterisk here to create it, it's saying that I have problems here, all right? So, you can see that all I want is a call. So the call means this part, here. And I'm going to get rid of this right here and that's all they want right there, OK?
All right let's go ahead and hit this one. Given a variable IP that's already been declared as a pointer to an integer, write the code that dynamically allocates memory for a single integer value, assign the resulting pointer to IP, and initialize the integer value to 27.
So, IP has already been declared as a pointer to an integer. And then we're going to assign a new integer with an initial value of 27. So there's what it looks like.
All right. The class date has a single constructor. So if you're using a method like power or something like this-- In fact, let me just show you real quick. I'm going to pop open Visual Studio.
If I say power, and that's the power-of function, I hit my left paren-- You can see that I need to have the base and the exponent. Because this is a power-of function. So 3 to the fourth power, is 3 and then comma 4, like that. And you can see that we have multiple choices like that because the power function has been overloaded. If you remember from your last class, overloading a variable, overloading a method, you just create the method multiple times. But you just have different types of inputs. So your argument list-- for parameters in this case actually-- your parameters have different data types, all right?
So you can see as I go 3, the data types change. So the number of parameters and/or the data types can change. And those are overloads. See that?
So that's what they're talking about when they're saying right here, the date has a single constructor that accepts an integer value for the month, and then another integer value for the day, and then the year, in that order.
So given the date variable, date p, they want to dynamically allocate a date object with the initial value of March 12, 2006. And assign the resulting pointer to date p.
[End of Audio]