Lecture 5/11: Lecture 16 Q&A


Lecture 16 Questions and Answers (Exported from Zoom Q&A log)

Q: is there section this week?

A1: live answered


Q: what is an at function?

A1: This function returns the string at a given index


Q: can chris redefine what new does really quick?

A1: New is a request to the system for a chunk of memory


Q: or anyone:-)

A1:


Q: would chris be able to show us a little iterm2 tutorial, maybe after the final exam, so that we can make our own programs?

A1: We should have some time in the last week to do some advanced topics — post on Ed with your suggestions?


Q: why do you not call delete in the for loop in main? does the class self-delete?

A1: The destructor is called automatically when you leave the scope (in the same way that the constructor is called automatically when you enter the scope)


Q: We have a traditional final later in the quarter, right?

A1: Yes, but evaluated somewhat differently than tradition, see “end-quarter assessment” info on course web site


Q: So does the desctuctor run everytime in “Demo demo;” line to clear up the memory of the last demo variable?

A1: Constructor runs on at time of declaration, destructor runs at end of scope (i.e. closing curly brace)


Q: what is the exact definition of “overloading”

A1: Overloading is definiing two of more different functions/operations that share the same name


Q: what does overload actually mean (in regards to printing)?

A1: Overloading is definiing two of more different functions/operations that share the same name


Q: What is a namespace?

A1: It allows you to section off a group of definitions and logically group under a section name


Q: pragma once i think* :)

A1: live answered


Q: Could you increase text font?

A1: live answered


Q: is capacity how many more it can hold (so 3 in this case) or is it always just the same as init_capacity?

A1: It will start as init_capacity but could change if we later enlarge the space


Q: is ~ just a convention for destructors or does it really mean something to our computer?

A1: Just as the “name” for a constructor has to be the name of the class, the name of the destrutor has to be ~name of class


Q: literally what was that ostream stuff lmao

A1: A ittle bit of gobbledy goop, yes,, just same as in previous classes Chris has shown you


Q: Why did he move to .cpp file?

A1: The header file is where you put the interface, the cpp files is where the implementation goes


Q: wizardry!

A1:


Q: Yes, we want to see how he did the curly brace thing. ^_^

A1:


Q: Why do we redefine "elements = new int[]" without the star? Like why isn't it "*elements = new int[]"?

A1: *elements would derefence the pointer and retreive the single integer at that address


Q: could you further explain why capacity is 8 at the beginning? why not 0

A1: We are reserving space in anticipation that the client will add elements soon


Q: I'm still a little confused about the line that says elements = new int[INIT_CAPACITY]. Can you explain that again?

A1: This is asking the OS for memory to store 8 integers, the OS returns an address for that memory, and we store that address as the elements pointer variable


Q: is elements just one pointer to one int value or is it 8 pointers to 8 different boxes?

A1: It is one pointer to a continguous region of 8 boxes


Q: do we not have to augment the capacity as well?

A1: No, we have not changed the capacity. We asked for space for 8 numbers in the constructor but have not changed the capacity


Q: Oh, so I guess… when you do new int[10], it's actually returning the memory of addresses of 10 ints. And not the actual ints themselves. Is that right?

A1: yes, exactly. Here is your mantra: “a pointer is a memory address”


Q: is the exand similar to how social media scrolling adds a few more posts once you get to the bottom of a page?

A1: You could say that. — you grab a small amount to start and wait until you arrive at situation where you need to get some more


Q: why did chris take out friend from the ostream&

A1: The friend only goes on the declaration in the header file, not on the definition.


Q: Is the expand private function what Chris referred to on Friday with the Stanford Word Program example as a way to not overload the computer with memory?

A1: In that example, he was torn between underallocating or overallocating, the enlarge approach is letting the allocation grow on an as-needed basis rather that try to do a one-size-fits-all


Q: If you do int* ptr = new int[10]; Then ptr now contains the memory addresses of 10 ints. So I guess I'm confused, when you call ptr[0], are you calling the memory address of the first item, or the value of the first item?

A1: There is only one memory address, it is the address of a continguous region of 10 ints


Q: when you "delete" the memory is it technically still there but can now be overwritten? is it completely removed (like what the blue boxes are in the depictions)

A1: It is technically still there but is marked so as to be available for recycling (OS can repuprose that memory for some other use)


Q: so does deleted just prohibit our ability to interact with a variable anymore? what does “giving the memory back to the operating system” mean?

A1: I think of it akin to you handing in your hotel room key when you check out. You are telling the hotel that they can reassign your room to a new customer. They may not do that right away but it would still be wrong of you to go back to that room and try to get your stuff. You checked out, you are done with it, it is not yours anymore


Q: what is the "new" keyword doing?

A1: Asking the OS to allot us a region in memory and return its address


Q: Why did we have to delete[] elements before reassigning it? What if you skipped step 3?

A1: If you did not delete, you would create a memory leak. The memory would not be recycled


Q: Why do we use int* newElements here?

A1: We are declaring a new variable of type int pointer and then assiging that variable to hold the address returned by new


Q: Why do we update the capacity twice? Once in line 56 and another in line 71.

A1: Line 56 doesnt change the value of our capacity variable, it just multiplies current capacity by 2 and asks for that size of memory


Q: What is the purpose of step 4? In my understanding, I don’t see how pointing the elements from our “deleted” array come into play if we already have a pointer for our new elements.

A1: We need to hold on to that address so that subsequent add operation will add to this array, not our old one


Q: why is expand() not a private function?

A1: You’re correct. it is a private function.


Q: Why is it so instant when we add elements to vectors in classes already defined for us?

A1: It does take some time, but for some enough N it is not that noticable. What make it so rough on Chris first example was N = 1000000


Q: I was going to post this question on Ed, but it seems that the page isnt loading for me. Ive noticed that our assignments have been becoming more vague (which is fine, as I’m sure you want us to become more self sufficient). Given that creating our own classes is an extremely unique topic that will take a while to process, will our assignments that deal with class creation be more descriptive?

A1: If you don’t understand something about the assignment, specification asking on Ed for clarification is totally fine!


Q: probably won’t have time today but next class can we go over the friend and overloading of « because it seems that one is always last and gets skipped in interest of time

A1: I’ll pass along to Chris. One thing to note is that the print operation is similar code in most cases, so not all that interesting…


Q: so if we remove a lot of elements we can shrink the vector too like the opposite of expansion when we need more?

A1: We could, but in many situations we don’t bother, we just leave it overallocated


Q: For the remove function, we also need to include the expand() if the count is = to capacity, right?

A1: No, there’s not need to expand the array, since we are removing a value and not inserting a new one


Q: The thing that Chris mentioned about .remove(), how it leaves the last value unchanged… isn't that a problem if you try to access that last index with .get()?

A1: You’re right that with the curren timplementation there might be some problems, so you would probably want to enforce some error checkin gin the get() method to make sure you’re trying to get at an invalid index

A2: The get operation should be written to throw an error if client asks to retreive an element beyond the range of valid indexes


Q: Does the order of this cpp file matter? What if we put the constructor/destructor at the end, after all our functions?

A1: live answered


Q: Why we can’t use cout directly?

A1: live answered


Q: thanks!

A1:


Q: Is the Stanford Vector class constructed like this, with dynamic memory? Or is it stored on the stack?

A1: live answered


Q: Why do we need a header file to define everything? Doesn’t cpp file define and implement everything already?

A1: live answered