Lecture 15 Zoom Q&A


Q: Since hashsets aren't ordered, when you run a for each loop to traverse a hashset, will the order reestablishh everytime you reach a new iteration of the loop?

A1: No, they use somthing called hashing to determine locations in memory. So the elements aren’t agrunteed to be stored near each other in memory


Q: Is it possible to apply for section lead for next year?

A1: live answered


Q: What's the CS198 course you have to take?

A1: It is a workshop on teaching


Q: will we still have a pset next week in addition to the diagnostic?

A1: live answered

A2: Next assignment is released next Wednesday instead of saturday as usual


Q: Is that assignment due two days after it’s assigned?

A1: live answered


Q: can you give me a brief preview about how he's "lying"

A1: live answered


Q: Is an address like a key but for one value?

A1: live answered


Q: If we set pet to “dog,” would dog now be at address 10?

A1: live answered


Q: is passing by reference like func(int& num) and calling like func(number) the same as passing a pointer like func(int *num) and calling like func(&number)?

A1: Yes, behind the scenes a reference is very much like a pointer!


Q: so technically you can have a pointer of a pointer?

A1: Yes you can!

A2: Yes, you are allowed to do so but we won’t have a need to do that in this class


Q: so is a pointer sort of like a data type for memory addresses?

A1: live answered

A2: Yes!

A3: Yes, exactly!


Q: Will the addresses be a int or hex value?

A1: live answered


Q: wouldn’t this lead to an infinite chain of pointers to pointers

A1: live answered


Q: so the value of a pointer is just the address of another variable

A1: Correct


Q: Can you change values @ certain addresses? Or is 10 always going to be cat

A1: live answered


Q: Why do you need to declare the type of a pointer, if all of them are going to a memory address anyways

A1: live answered


Q: if we have a pointer to a string and then we directly change the contents at that memory address to a different data type (like int) from somewhere else, would we have an error at runtime or while compiling?

A1: Mostly you would get into trouble with the compiler for type mismatches


Q: So right now, those pointers aren’t holding any specific addresses?

A1: live answered


Q: could you have a pointer pointing to a pointer?

A1: yes


Q: Is it the same for int* ptr and int *ptr?

A1: yup

A2: Correct


Q: What’s the syntax for declaring a pointer to a pointer?

A1: int** pointerToPointer;


Q: Since c strings are defined as char* and strings are essentailly a wrapper class, isn’t a string* a pointer to a pointer of characters?

A1: No, a string is an object of the string class, internally it likely has a member variable that is a pointer to a sequence of characters, but that is within the abstraction


Q: so &pet returns a pointer?

A1: Yes, exactly (since it is returning the address of the pet variable)

A2: Yes, & applied to a variable returns the address of that variable, i.e. a pointer


Q: can you alter the value of pet by altering petPtr?

A1: Yes

A2: Not exactly by altering petPtr but by doing special operations on petPtr (that we will learn about soon)


Q: What are we not allowed to say petPtr = pet? Why is the ampersand needed?

A1: No, they are different types! pet is a string and petPtr is a pointer to a string

A2: The ampersand is necessary to extract the address of pet to get a pointer to the variable

A3: Because a string pointer is not the same type as a string. The ampersand gives you the address of pet which is the type that we need


Q: Can you print petPtr?

A1: Yes, it will print the numeric address


Q: Does that mean we assign the value of cat’s address to the “?”, and then make petPtr point to cat with the connection of value“10”?

A1: live answered


Q: could you also use cout « this->petPtr « endl; ? would this accomplish the same thing as cout « *petPtr « endl; ?

A1: The former prints the numeric address, the latter prints the string stored at that address

A2: We’re not working with classes right now, so there is no this keyword


Q: Why is there an x in base 16?

A1: The 0x is the prefix that indicates the number to follow is expressed in hexadecimal


Q: Does the computer prevent users from using pointers to access memory related to other programs by accident?

A1: live answered


Q: Can you directly set int* xptr = 0x34ab 
.

A1: Yes, but this would be meaningless because you don’t know if that is a valid address of something


Q: so a pointer points to a VALUE (cat) not necessarily a variable (pet)

A1: live answered


Q: Since &pet returns a pointer, is that a temporary pointer that is then passed to petPtr?

A1: live answered

A2: It’s not a temporary pointer, it is the actual address wherre the pet variable lives


Q: What happens if you try to pass a pointer by reference?

A1: It’s possible! We will see that in a week or two


Q: Is this why ampersand is used when creating a reference parameter? Since we are using a version/extension of a pointer?

A1: live answered

A2: No not really, its just two different uses of the ampersand operator


Q: is dereferencing a read-only operation in a sense? Like, I’m guessing you can’t do *petPtr = “dog” right?

A1: live answered


Q: With the petPtr and pet example, what does ’10’ represent versus the ‘1234’?

A1: live answered

A2: 10 is the address (location in memory) of the pet variable. 1234 is the address of the petPtr variable (every variable has a corresponding memory address)


Q: if you did *petPtr = dog, would that change the value of pet from cat to dog?

A1: Yes, it would

A2: Yes

A3: to be accurate you would want to say *petPtr = “dog”;


Q: Can we combine string* petPtr; and petPtr = &pet; to string* petPtr = &pet;

A1: Yes


Q: so is it fine in this class to check if(!petPtr)? Seems standard in wild code in the world

A1: live answered

A2: If you have initialized your pointers to nullptr, then testing for equal/not-equal to nullptr makes sense.


Q: why we just delcare pet and then make the pointer euqal to the address of pet rather than make it nullptr first

A1: live answered


Q: so it is better to declare a pointer that does not start with a value as nullptr, than just nothing at all?

A1: Yes, if you declare without initializing, the pointer variable will hold a junk address, which is no good!

A2: it is better to declare a pointer with a nullpointer


Q: what is there’s a pointer to an int that actually equals 0? Is that different?

A1: live answered


Q: why is nullptr better than the NULL variable?

A1: live answered

A2: NULL is the older C-way, predates modern C++


Q: how does this relate to the nullPointerExceptions we get when trying to operate on an uninitialized object?

A1: live answered


Q: According to the book, the null pointer should be NULL. Why is it different here?

A1: NULL is the older C-way, predates modern C++


Q: *if


Q: If a pointer itself has a memory address, can you make a pointer to a pointer?

A1: live answered


Q: so string* petPtr; could be initialized to some grabage value?

A1: live answered


Q: can that garbage address actually have a value associated with it?

A1: It is possible

A2: Possibly yes. Either the address will refer to an invalid section of memory, or it will read a garbage value from that location


Q: can you pass a pointer to a function by reference? would the syntax be funcname(string*& paramName)?

A1: Yes, that syntax is correct


Q: so then string *s; followed by if (!s) may be true, even though it could be an invalid pointer since it points to garbage, which may contain nothing?

A1: Not quite. if (!s) is testing whether the numeric address stored in s is zero or non-zero, this does not derefernce the pointer and retrieve contents at address, it is simply looking at the address itself. If s was not iniitialized, the address is holds is a garbage value so might be zero, but statistically more likely to be non-zero


Q: so & just returns the value?

A1: &variable evaluates to the address where variable is stored in memory

A2: It gives you the address, * gives you the value


Q: I have also seen the syntax string* newPtr = NULL; is that the same thing?

A1: live answered

A2: Yes but you should use nullptr, it is the modern C++ way of doing things


Q: What is the utility of the pointer though? Haven't really understood why I might need a pointer

A1: live answered

A2: We’ll see how pointers can be used to build data structures next week!


Q: do we have to make the pointer equal to nullPtr at first? could we not give it the memery address value straight away?

A1: live answered


Q: Why is the error from the Stanford library? I don't see any Stanford functions.

A1: live answered

A2: Stanford library intercepts error messages and prints then out in a prettier/mor einformative form


Q: so if string *s; is initialized to garbage, technically the if(!s) will not catch if s is an invalid library for all cases?

A1: Correct.


Q: What will be printed if we do the following, without *? cout « sPtr « endl;

A1: You’ll print out the memory address of the string

A2: It will print the numeric address stored in sPtr


Q: If you set s= goodbye would it do the same thing?

A1: live answered


Q: Just to clarify, nullptr points to an address of 0, but isn't tied to any value?

A1: Correct


Q: What would happen if we did something like *sPtr = 2.5?

A1: It would not compile because 2.5 is of wrong type (is double, not string)


Q: Isn’t the memory of sPtr supposed to be 0 since it is set equal to = nullPtr? (for the first three lines)

A1: It is later assigned to &s


Q: Isn’t that a bad thing like it would be for a global variable?

A1: Not exactly – we can still only modify the variable if we konw its address so its not “globally” known about


Q: What is an example of a situation where we’d want to change the value using a pointer instead of just the variable’s name?

A1: There aren’t really many situations where we’ll be using pointer modification exactly int his way. We’ll see in about a week how we cna use pointer “rewiring” like this to actually build data structures


Q: Could you alternatively do sPtr2 = &s

A1: Yes, this has same effect


Q: Can we modify s through either sPtr1 or sPtr2?

A1: Yes!

A2: Yes


Q: Why do we need a special type for a pointer if a pointer just holds an int?

A1: Although both are just integer addresses, the type of the “pointee” is important information re: type system. Dereferenceing an int * retrieves a different kind of data than dereferenceing a string*

A2: Its important to distingusih between memory address (pointers) and actually the values actually stored at those memory addresses, even of both in the end are just number.s Its twoo different interpretations of the same number


Q: Can you pass pointers by reference? And how would u tell the difference between dereference and reference?

A1: live answered


Q: Why would we need two pointers to the same place?

A1: It facilitates sharing


Q: can you have a pointer to a pointer to a variable?

A1: Yes, although we won’t need to in this class


Q: If you have a a strptr pointing to a string, can you use functions like *strptr.length()?

A1: Yes, although there is a more compact syntax to do so that we will see in a couple days


Q: so pointers are always passed by refernce?

A1: Not really, but effectively yes

A2: No pointers like all other types are passed by value by default. We will see pointers passed by reference next week

A3: No, pointers themselves are passed by value, just as other types are


Q: Can you explain a bit about the purpose of pointers?

A1: live answered


Q: where can we find the video chris mentioned?

A1: I think he’s gonna show it in class on Monday


Q: If you change the value of a variable, does that change the value the pointer for that variable points to?

A1: live answered


Q: Could we see the video Chris was talking about?

A1: I think he’s going to show it on Monday!


Q: so when we use a pointer in dynamic memory that’s our way of accessing the stuff that is living on the heap? where as if we just did regular variable stuff in a function it would be on the stack

A1: live answered


Q: If I want to get the value of a pointer of a pointer, do I iterate the “&”? Can we think of the “*” as moving us up a level and the “&” as moving us down a level?

A1: live answered


Q: Are pointers a class in C++? So when getting the reference value of a pointer with the & character is that like calling a function from a pointer class?

A1: live answered

A2: No pointers are just a variable type. The & operator is a special operator that operates on vairables and returns their memory address


Q: is there anyway to see all the pointers connected to a specific memory address from the variable side of things? for example something you could do to pet to get back petPtr?

A1: No, you cannot really travel the other drection of the arrow


Q: So is the delete operator in dynamic memory telling the operating system that the memory address is now available?

A1: Yes, exactly