Lecture 5/8: Lecture 15 Q&A
Lecture 15 Questions and Answers (Exported from Zoom Q&A log)
Q: Unrelated question, but I've been on this for the past half an hour, where is the emacs default init file?
A1: Usually it is in your home directory, named .emacs (files whose names begin with dot are by default hidden)
Q: It says it was recording when I entered the room 5 mins ago if that helps?
A1: Yes I think so!
Q: Is C still used or is it only C++?
A1: C is still very big in systems programming (more popular than C++ in fact)
Q: Is there much coding in cs107 like we’re used to or is it really a different world dealing with some more unfamilar, hard-core memory stuff?
A1: There is much coding of a systems variety — lower level, lots of pointers, bits and bytes. All in C, so not much in the way of high-level abstractions
Q: what would be the equivalent of a pointer in java?
A1: Java does not expose the concept of a pointer/memory address. However, it does internally use addresses as a means to access shared data (such as objects)
Q: can you have a pointer for a pointer
A1: Yes (scarily enough!)
Q: If 1234 is the memory address, what is 10? I thought 10 was an address
A1: 1234 is the memory address of the pointer variable. 10 is the memory address of the string cat
Q: Since Python is heap allocated, are Python variables … kind of like pointers?
A1: Yes they are!
Q: how do we get access to that 10?
A1: There is an operator that when applied to an variable will tell you what address that variable is stored at.
Q: which takes up more memory? the pointer or the variable?
A1: It depends. A pointer holds an address, and all addresses are same size. But the variable it points to could be an int, or a double, or of Map<string,int> and those things are different sizes
Q: do u have to declare the string seperatley in this cass
A1: Declare separately from ???
Q: Does petPtr correspond to 10 in the previous example
A1: The value of petPtr is the address 10, yes
Q: So if youre calling a function does sum(int& num) and sum(int &num) mean differnet things?
A1: No. The & that is used to indicate pass-by-reference can be next to the type, next to parameter name, but means same thing either way. Chris was mentioning that use of & for address-of is a completely different use of the &
Q: Why do we need pointer?
A1: Pointers facilitate sharing, allow dynamic data structures that are constructed from links — all this coming up in later lectures
Q: Would we get an error if we did petPtr=pet?
A1: Yes. This would be a type mismatch in assignment
Q: why not just cout « pet « ?
A1: You are correct that would produce the same output!
Q: what should we do if we want the value of pointer?
A1: You can cout the variable petPtr itself (without * derference)
Q: is this like going to a dictionary value and asking what its key is?
A1: Sort of — maybe it is more like having a set of PO boxes and the difference between the number #1453 and the contents of the mailbox #1453
Q: look at pet not petptr
A1:
Q: Will the address of pet be the same each time you run the program? Or will it change depending on where your computer is saving it in the memory?
A1: It can vary from run to run on same system, or when running on differnet system
Q: That address had 12 digits. Does that mean there are 16^12 memory locations on the computer??
A1: Great question. on a 64-bit system, memory addreses are stored in an 8-byte (64-bit) variable, which allows for 2^64 different addresses!!
Q: you should change slide 13 to "a couple of pointers"
A1: Ha! https://xkcd.com/138/
Q: do we also use nullptr to avoid storing the bogus value? (or does the bogus value never take any space?)
A1: nullptr replaces the garbage/bogus value that would otherwise be there
Q: Can multiple pointers point to nullptr?
A1: Yes.
Q: ^^ is there a difference performance/storage wise between letting that bogus value sit as ptr rather setting it to nullptr? which one should be used?
A1: Setting to nullptr is much preferred. This is akin to initializing a variable so that it has known contents efore you go on to use it
A2: and more importantly, you can test for ptr == nullptr to know that it has been set to that, there is no way to tell that a uninitialized poitner has a garbage address
Q: when you dereference a pointer, is the memory address stored by that pointer gone?
A1: No, it simply reads the contents from that location, the contents stay at that location
Q: Did I completely miss the guest lecture yesterday night?
A1: We were able to record PIXAR night — you can catch the recording on Canvas. It was great, please do watch it!
Q: Is a pointer more efficient than passing, for example, a set by reference ?
A1: Passing a pointer and passing by reference are same efficiency
Q: Is -> a symbol that's related to pointers
A1: Yes the arrow is a combination of the * and . operators
Q: Is it in base-16 so that we can represent more addresses than in smaller base representations?
A1: It is just easier to read base 16 than binary or decimal
Q: is this what sleep paralysis feels like?
A1: I think so
Q: bravo nick!
A1:
Q: Hey Chris! what is this symbol -> and how is it related to pointers?
A1: live answered
Q: Thank you! super cool
A1: live answered