Lecture 5/6: Lecture 14 Q&A


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

Q: it’s all good chris, you’re doing great!!!

A1: Fun start!


Q: for some reason the screen is blinking, is that just me?

A1: I am not seeing it on my end, try connect/reconnect?


Q: will Nick be having Thursday morning office hours?

A1: Yes, my office hours are now permanently 9-11am Wednesdays and Thursdays


Q: could you make a setNum function to change the private variables, or are they totally inaccessible?

A1: Yes, this would be possible because the setNum function would be included in the class implementation and thus has access to private variables


Q: why did we say out instead of cout?

A1: out is the name of the output stream that we are writing the fraction to


Q: why do both ostreams have ampersands?

A1: They are being passed./returned by reference (copying streams is generally disallowed)


Q: why in the reduce function you dont have to use the “->this” trick?

A1: In this case there is only one variable in scope whos name is num, it belongs to this (in the constructor before there was a parameter named num that needed to be distinguished from this->num)


Q: I’m a little confused what these function definitions are for, is the purpose of the class to make predefined functions to be called in other functions?

A1: the functions are packaged with the class. reduce is a funtion that applies to a Fraction


Q: Will it be recorded?

A1: I believe we cannot, due to copyright issues. :-(


Q: Will we have the chance to sign up for any of those three days for assessments or is it based on our SL schedule?

A1: It depends on SL availability.


Q: what does “lower level” mean?

A1: less abstract, closer to nitty-gritty of how things work closer to hardware


Q: what are global variables?

A1: It’s good that you don’t know that :-)! (These are variables declared outside of function scope, we don’t use them as considered poor style)


Q: so a c-string is just an array of chars

A1: Exactly right!


Q: what does he mean by lower

A1: lower level meaning less abstract, closer to hardware


Q: What is buffer overflow? I did not catch that.

A1: Take CS107 and all will be revealed! (this material not needed for CS106B)


Q: Can you change the memory you “asked for” (when you first declare the array)?

A1: You can read/write that memory, yes. It has been set aside for your use


Q: What are we looking at? What are the numbered axes?

A1: it is the storage space internal to the computer


Q: Storage space is organized in a 2D grid?

A1: The storage is one-dimensional underneath, but you can think of it as organized into rows and columns


Q: does chris know this student ?

A1: I don’t think so, but keep listening, I bet we will hear if he does


Q: wow can u imagine being that smart :O

A1: Keep on studying CS and see where it takes you!


Q: professor at MIT…now who would want that …LOL

A1: Consolation prize if you can’t hack it at Stanford?? :-)


Q: lose points if we do what?

A1: In CS107, it is expected that you are careful about use of memory and always cleanup after yourself


Q: why are the brackets in front if the array…should it not be after the array name?

A1: For delete[] the brackets are a part of the operator name


Q: is deleting kind of like clearing a variable?

A1: Sort of, the analogy I like to use is checking out of hotel room so that it can be given to another customer


Q: How do these relate to temp variables?

A1: Delete only applies to variables created with by new operator


Q: can you give back portions (delete part of it)?

A1: No, you can only give back the whole thing that you were initially given.


Q: do you have to delete first if you want to go from int[5] to int[100]?

A1: yes


Q: If we want to allocate more memory to ourselves, can we go int* blockofInts = new int[5] and then later go blockofInts = new int[10] or do we have to delete before we redeclare?

A1: You have to delete the old one first


Q: But what does it mean to “free the memory?” If the variable still exists, when would it be overwritten?

A1: delete is the way you tell the OS that you no longer need the memory (free/delete are used interchangably as the verb for that action), yes once you release your hold on that memory, it can be reused and overwritten for another use


Q: why don’t we need to delete other variables? Can they be overwritten without warning?

A1: Only variables created with new need to be explicit deleted.


Q: is delete used for efficiency? how similar/different it is from garbage collectors?

A1: C++ does not have garbage collector, all allocation/deallocation is explicit. This design gives programmer explicit control/responsibility. Yes, efficiency is part of the motivation for that


Q: what is the constructor doing here?

A1: I am not sure what you are referring to, can you give more context?


Q: wait why does he have the deconstructor in the constructor?

A1: Not sure what you mean — the constructor is separate from destructor…


Q: If you delete some memory, what would happen if we call or attempt to view what's in that allocated space?

A1: Bad things happen! Non-deterministic behavior, crashes, errors, etc.


Q: so is it an issue if i dont return the memory in the end? is this risking overflow?

A1: The issue is one of resource exhuastion. If you never release the memory, it cannot be recycled/reused


Q: right, in chris’s code he has the constructor and inside the constructor is the destructor, i thought they were seperate

A1: Is there a stray brace there? Shoud be two separate functions, one defined after another


Q: '@student: what iis a deconstructor? I think it is called a destructor.

A1: If you say deconstructor we will know what you mean, but you’re correct, the name is actually “destructor”


Q: I still don’t see how building these 2 arrays of even and odds was different than making 2 vectors? What was the purpose oc constructing and destructing here?

A1: You’re absolutely right! The point here is to learn how to write a class like Vector yoursel


Q: In this DynamicInts class, we didn't have to do that weird friend ostream& thing, but we were still able to use cout. So what was the point of the ostream& thing before?

A1: By default, an object doesn’t have any defined behavior when you try to output it to a stream, i.e. cout « myobject « endl won’t work unless you have defined the operator« for your class


Q: do we put the deconstructor at the end of our class?

A1: You can define it anywhere in the class implementation. Often you put it right next to the constructor


Q: if delete allows you to keep data why wouldn't you just delete every page as you go

A1: live answered


Q: cant you just multiply storage by a constant each time it runs a loop or something?

A1: live answered


Q: maybe they should have used keyword “free” instead of “delete”

A1: live answered