Lecture 5/18: Lecture 19 Q&A


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

Q: Will we be getting feedback like in IGs?

A1: We could fold into your assign4 IG, let me talk to SLs about that


Q: those collab sessions won't be recorded i assume?

A1: No, there won’t be any material explitcily presented. It’s just a space to come start working and to chat with other students and ask questions as they come up.


Q: Is the collaborative session going to be recorded?

A1: No, there won’t be any material explitcily presented. It’s just a space to come start working and to chat with other students and ask questions as they come up.


Q: Is this the same mechanism as this-> ?

A1: Yes!


Q: so the pointer is pointing to the value 7?

A1: yes, you can think of that way, the pointer holds the location in memory where the value 7 is stored


Q: Is it better to create an instance of a class by using Date myDate = Date() or Date* myDate = new Date()

A1: The first one is a local variable (so goes away when you leave the scope in which it was declared), the second one is allocated in memory where you explicitly control when deallocated


Q: dynamic memory is heap or stack?

A1: Dynamic memory is what gets allocated on the heap


Q: does “doing it with dynamic memory” just mean using the heap?

A1: Yes. But remember there are two uses of word heap going around right now — the heap data structure, and the heap of memory that is allocated and deallocated using new and delete


Q: What is the heap data structure again?

A1: The binary heap that has parent value high priority than its two children (i.e. Assign 5)


Q: Why is this a desireable way to structure data?

A1: We’ll see reasons for why this is useful later on in the ecture! One reason is that with the link structure, when you want to insert something new in the middle, you don’t have to shift everything else over (like you do with a vector or array)


Q: Im really confused as to what “next” means

A1: We’ll see the formal definition in a little bit, but you can think of it right now as just a pointer to the next element of the linked list.


Q: Can a struct have a constructor? Or do we typically only see classes having constructors?

A1: Struct can have constructors, but we will rarely if ever see that in this class. Constructors will be confined mainly to classes in our experience this quarter!


Q: would you need 100 to point to 3 first before having 2 point to 100

A1: Try diagramming out what would happen if you did it in the other order! What you hsould find is that if you point 2 to 100 first, you no longer have a pointer to 3 anywhere! Which means that it is now lost forever. So it is important that we point 100 to 3 first before pointing 2 to 100


Q: can we delete 3 first and then link to 4? does the order matter?

A1: Yes, the order matters a lot. If you delete 3 first before you have established the link to 4, then you will lose the pointer to 4, meaning that the remainder of the list gets “lost” forever because now you have no way to refer to it


Q: head is the front/first element?

A1: yes, correct


Q: hahaha this is so awesome

A1: glad you enjoyed it!


Q: is there a reason there is a semi colon afer } in struct Tower?

A1: Yes, that is the syntax used for struct definitions. All struct definitions must end with a semicolon after the closing bracket


Q: Out of curiosity, what do you think of tuples? Just because we haven't talked about them. Are they good ways of storing info, or are there better ways?

A1: Tuples are a good way of storing info, but they are a little beyond the scope of this class! They fall into the category of things that are more C++ language specific that we don’t necessarily have a chance to cover in CS106B.


Q: I understand that pointers are memory addresses as taught in lecture 5/8. To explain the pointer, we used a 4 box schematic where the variable (value) has a memory address and a pointer is a new data type whose value is the memory address of the variable. How do we explain a node in the context of these 4 boxes? it seems like the node is a fusion of these 4 boxes?

A1: A node itself is just a data type, much the same way that an int is. So, when you declare a node, you can think of it as a box that encpasulates two pieces of infromation ( a data value and a pointer). This node also has a place where it lives in memory, so if you get that address and sote it in a variable, then you have a pointer to a Node *. So yes, we are taking advantage of all of the different things that we say in lecture last week when it came to working with pointers and variables.


Q: Why is the declaration a pointer? Could we just have said Tower head; Tower.name = “San Jose”; ?

A1: Linked lists are almost always built off of nodes that are allocated in dynamic memory, which is why we chose to do Tower *head = new Tower. In particular, we could not have a helper function be responsible for creating new towers if the memory was no allocated on the heap.


Q: The CreateTower, is enqueueing the towers correct?

A1: Yes, since we set temp->next = next into the helper function, we are actually linking the new tower into the overall linked list.


Q: can you just explain why we need start->next instead of start.next here in signal?

A1: Since start is a pointer to a Tower and not a Tower itself, we need to use the -> instead of the . syntax to access elements from inside of it.


Q: Is there a more efficient way to create the towers or is listing each one most efficient? How would that work for a larger list?

A1: If you were able to programatically generate station names, you could totally compress all of those individual calls into a for lop or something similar. I think this way of doing things is mostly for demonstration. You would not want to have this much duplication in real code that you’re writing.


Q: Can you remind me what exactly the -> syntax does? Does it return a pointer or a value?

A1: The -> syntax extracts the value of a struct field from a pointer to that struct.


Q: So are we creating this linked list “backwards” — ie the head starts as the last thing in the list adn at the end is the first thing ?

A1: Yeah, I think you have the right idea!


Q: So earlier, we used the -> syntax in the context of this->property to assign properties in a constructor. Does that imply that "this" is a pointer?

A1: Correct, you can think of “this” as a pointer to the current instance of an object when you are implemeting class methods.


Q: where is the corresponding delete for the new?

A1: I don’t think I saw any, but I might have been missing it. Sounds like memory leaks galore!


Q: about the deleted heap code. will that be added back to website after a5 is due?

A1: Likely not, as this assignment will be reused in the future


Q: oh ok so I can think of a node as three boxes on top of each other? the first box is the data, the second box is the memory address of the next node, and the memory address of this node is in the third box (which can be pointed to from a different node)? So a node is a combination of a variable and a pointer?

A1: Correct! A node is a combination of a variable and a pointer, and we can use the pointer to point to another code, which helps build our overall linked list structure.


Q: If I have a pointer called Tower* myTower, how would I access the Tower that it's pointing to? Would it be (*myTower)?

A1: Yeah, that would give you the whole Tower object. We normally don’t do this because we are usually only concerned with accessing only field of a struct at a time (in which case myTower->field is the way to go) but you do have the correct understanding about what the dereference operator does here.


Q: Okay, so myTower->field is the same thing as (*myTower).field, but the -> syntax is just neater?

A1: Yup!


Q: how is this different from what we just coded?

A1: I don’t think it’s necessarily different, it’s just a visualization of what we did earlier. We’re using the same “insert at the front of the linked list” to achieve two different goals


Q: so “->” is a different way of writing a pointer?


Q: Can you give a quick refresh of the * syntax and what it means


Q: so would toReturn be 7 in that case?


Q: thanks Chris!