Another new function is used to get rid of the data and free up the space on the heap for reuse, the function "free". To use it, you simply call it with the pointer to the block as the only argument, and the block is deallocated.
In order to illustrate another aspect of the dynamic allocation and deallocation of data, an additional step is included in the program on your monitor. The pointer "pet1" is assigned the value of "pet3". In doing this, the block that "pet1" was pointing to is effectively lost since there is no pointer that is now pointing to that block. It can therefore never again be referred to, changed, or disposed of. That memory, which is a block on the heap, is wasted from this point on. This is not something that you would ever purposely do in a program. It is only done here for illustration.
The first "free" function call removes the block of data that "pet1" and "pet3" were pointing to, and the second "free" call removes the block of data that "pet2" was pointing to. We therefore have lost access to all of our data generated earlier. There is still one block of data that is on the heap but there is no pointer to it since we lost the address to it. Trying to "free" the data pointed to by "pet1" would result in an error because it has already been "freed" by the use of "pet3". There is no need to worry, when we return to the OS, the entire heap will be disposed of with no regard to what we have put on it. The point does need to made that losing a pointer to a block of the heap, forever removes that block of data storage from our program and we may need that storage later.
Compile and run the program to see if it does what you think it should do based on this discussion.
In order to illustrate another aspect of the dynamic allocation and deallocation of data, an additional step is included in the program on your monitor. The pointer "pet1" is assigned the value of "pet3". In doing this, the block that "pet1" was pointing to is effectively lost since there is no pointer that is now pointing to that block. It can therefore never again be referred to, changed, or disposed of. That memory, which is a block on the heap, is wasted from this point on. This is not something that you would ever purposely do in a program. It is only done here for illustration.
The first "free" function call removes the block of data that "pet1" and "pet3" were pointing to, and the second "free" call removes the block of data that "pet2" was pointing to. We therefore have lost access to all of our data generated earlier. There is still one block of data that is on the heap but there is no pointer to it since we lost the address to it. Trying to "free" the data pointed to by "pet1" would result in an error because it has already been "freed" by the use of "pet3". There is no need to worry, when we return to the OS, the entire heap will be disposed of with no regard to what we have put on it. The point does need to made that losing a pointer to a block of the heap, forever removes that block of data storage from our program and we may need that storage later.
Compile and run the program to see if it does what you think it should do based on this discussion.

