Q: is there a time limit?
A1: Yes, we will design it to be completed in 1-2 hours, but there is 3 hour formal limit
Q: What is the format like for the diagnostic?
A1: live answered
Q: For the diagnostic are we going to be provided with test cases that we have to pass?
A1: No, we do not expect you to run your code for the diagnostic
Q: How does the code we will write for the diagnostic compare length wise to the homework assignments?
A1: The diagnostic will have questions that are scoped like section problems, not like homework problems.
Q: Isn’t that what the operation friend functions looked like?
A1: live answered
Q: Does the third option result in a compile error, or does it only create problems at runtime?
A1: live answered
A2: Runtime error
Q: did it freeze?
A1: His screen doesn’t appear frozen to me
Q: nvm, my internet lagged out
Q: Aren’t we returning a Vector pointer? Why do we just return vec by itself?
A1: live answered
Q: whats the difference between the heap and stack?
A1: live answered
A2: Coming up in a sec
Q: does dynamic memory still have a scope?
A1: live answered
Q: Can you make a example of global variable?
A1: No, no global variables! Don’t want to show examples of things that we don’t want you to do :)
Q: sorry, I guess I just don't know what global variables is
A1: live answered
Q: If e.g. we declare a Vector on the stack, how come we don't get a stack overflow when we add a ton of elements to it?
A1: live answered
Q: We don’t use global variables because we can accidentally change it, right?
A1: Yes, exactly. There is no guarantees how and where the variable’s state is changed.
A2: Right, and this can lead to unexpected behaviour
Q: Does it create sort of the array of there 10 integers?
A1: live answered
Q: is that because doing something like type* var1, var2 will only apply * to var1 and not var2? So it would be better practice to do type type *var1, var2?
A1: live answered
Q: can we use indexes to call things from the array?
A1: yes, we can !
A2: yes
A3: yup, we’ll see that in a sec
Q: does the nultiple elements create a vector of integers?
A1: We’re allocating an array of integers, which is an important distinction here
A2: It creates an array which can hold integers
Q: This sorta reminds me of instanciating an object in Java. Are they similar or are these two completely different things?
A1: Very similar, yes!
Q: so is it similart to a map where instead of pointing to value, it points to a memory location?
A1: Yes, if allocated on the heap
Q: Is it possible to request too much memory, if so what is it called and is it hardware/os bound?
A1: live answered
Q: Is the int* tenInts=ine int[10] an array of 10 integers, or 10 separate integers? Do they all have the same name tenInts?
A1: It is in an array of ten integers that we will refer to using special notation using the name tenInts
Q: Will you return the first address of int[0] there?
A1: Yes if you need to communicate the beginning of the array to other parts of the code, you could return the pointer
Q: Are they arrays from C?
A1: yes
Q: Why is this better than just using a global variable? Isn’t secondTen still accessible to other functions to be modified?
A1: live answered
Q: where do you declare these dynamic memory variables? in functions?
A1: You can declare them anywhere you want to use them, in functions is most common.
Q: Is memory from main() stored in the heap?
A1: No, main has a stack frame, just as other functions do
A2: All memory is default stored on the stack (like all variables we’ve seen)
Q: is this the same buffer overflow found in the buffer overflow attack?
A1: live answered
A2: Yes, exactly.
Q: if we lose access to it once the function is finished, what’s the difference between this and just a regular variable…
A1: By it are you asking about a dynamically allocated variable?
A2: The storage space for all local/stack-allocated variables is autoamtically allocated when you enter the function. and deallocated when you exit. Dynamically-allocated memory has to be explicitly allocated and deallocated under programmer control
Q: '@chase yes
Q: can you choose where in your memory your array or int is stored?
A1: live answered
Q: does the location of the memory affect the speed of your program?
A1: Not really, unless you’re talking about RAM which we won’t cover in this class
Q: do we esrase the memory we requested after we are through with it? or is this automatic?
A1: Yes, we have to manually give it back to the operating system! But we haven’t tlaked about how to do so yet, we’ll get there soon
Q: Is an underlying array how the Stanford (and the standard library) Vector are implemented?
A1: yes!
Q: what types of things are automatically stored in the heap?
A1: Nothing is “automatically” stored on the heap! Its all up to the programmer to be explicit about when they want things to be allocated there.
Q: so the heap is never used for anything unless you use the asterick?
A1: Pretty much
A2: More precisely, it is the use of the new keyword to get storage in the heap
A3: Not exactly – its more accurate to say the heap isnt used for anything unless you use the new keyword
Q: Is deleting kind of like how "deleting" a file in an OS doesn't really delete the file until the space needs to be used elsewhere?
A1: live answered
Q: but they will still be in sum right?
A1: live answered
Q: what happens if there is no space left in the heap?
A1: Your program will crash with an out of memory error.
A2: new request will return a nullptr
Q: How does the computer know how large the reservation of memory pointed to by a pointer is?
A1: live answered
Q: so the delete keyword deletes the pointer that is associated with the memory rather than actually interacting with the memory
A1: No, again the delete keyword doesn’t really delete anything. It tells the operating system “I am no longer using memory at this address”.
Q: what if we don't know how much memory we need?
A1: Then you have to assume in the beginning and resize later
Q: Why would you want to delete and then re-use the variable? What’s the benefit in doing that?
A1: You wouldn’t this will lead to unexpected behaviour
A2: You are deleting the memory (unreserving it) and then making a new reservation, a typical reason to do this would be to change he size of the reservation for example
Q: So this is similar to “free”? After we call delete, the memory previously assigned to blockOfInts* is “freed”?
A1: Correct
Q: What if I write sensitive data to a dynamically allocated variables and give back the memory, when the next program accesses that memory location, will it access the data I wrote?
A1: Yes, it might
Q: but are the 42s still there?
A1: Maybe! For some amount of time – the idea is that you have no guarantees about what is there from that point forward
Q: When you "reuse" a pointer, does it matter what size you ask for? - does it reuse the same portion of memory?
A1: live answered
A2: No a pointer just points to the beginning of a chunk of memory, so it can point to arbitrarily sized chunks without any modification (besides reassignign to new return values from the new keyword)
Q: How can you try to reaccess the 42s after delete?
A1: live answered
A2: You shouldn’t! By calling delete you have given up your permission to access that memory
Q: The heap and the stack are the two parts of "memory" right?
A1: live answered
Q: what happens if we just end the program without deleting?
A1: You have something called a memory leak
Q: why we do not put int* in front of blovkofints = new int[100]
A1: The blockOfInts variable has already been declared earlier
A2: This is not declaring a new variable, just reassigning the variable
Q: What happens if you try to point to deleted memory? After all, you still have the pointer to that memory…
A1: live answered
A2: You’re allowed to point to deleted memory, but as soon as you try to access that memory, bad things happen
Q: So could another program or malware running at the same time as your program have the ability to scrub working memory and recover possibly sensitive information?
A1: This shouldn’t be possible because of something called virtual memory.
A2: Each program has its private/secure memory, that is not acccessible to other problems
Q: so blockOfInts as a pointer still exists but now we can make it point to something different?
A1: yes, exactly
A2: Yes, correct
Q: should we always delete at the end of the program? Or does the program automatically delete it at the end of execution?
A1: live answered
Q: Why don’t we try to make all of our local scope variables like this? Why don’t we normally delete all variables after we’re done with our function to free up some memory? What’s the benefit of using dynamic memory variables in the local scope?
A1: live answered
Q: What are the initial values in the array
A1: live answered
A2: garbage values! random large negative values are common, but in general the values are undefined
Q: When will be 42 space be empty?
A1: Sorry, I’m not quite sure I follow the question. What do you mean when you refer to the 42 space?
Q: what if i don’t delete and directly reuse the pointer?
A1: If you don’t delete then you are completely open to reuse the variable
A2: The original memoryin heap becomes a “leak” — thst reservation stays active but that memory no longer accessible because you don’t have a pointer to it anymore
A3: If you reassign the pointer to point somewhere else without deleteing the memory it initially pointed at, you will “leak” that memory in the sense that it is still reserved for use but nobody knows how to access it
Q: sorry but what is the benefit of accessign memory from the heap again?
A1: live answered
Q: If we have two pointer and assign one to the other, when we delete one pointer, can the other still access the data?
A1: It will point to a memory address but the data there will most likely be deleted
A2: Yes, but that would be an error. deleting applies to the memory, not the pointer itself
Q: I'm still confused why you can't use a global variable other than for good style
A1: Style is important!
Q: I know we will talk about pointers later, but what are we referring to now when we say pointer?
A1: A pointer is an object which referrs to a memory address
Q: What’s the point of this? How is this better than the way we regularly use variables?
A1: live answered
A2: Explicitly controlling allocation and deallocation allows us to build more flexible data structures that are not limited by the controw flow in/out of functions that stack variables are subject to
Q: Is the syntax delete[] arrayName or delete [] arrayName? I believe it may have been different with tenInts and blockOfInts
A1: works the same
Q: can you do the int* blockOfInts; on one line and then blockOfInts = new int[10]; on another line? Also, can you set blockOfInts = new int[100]; without deleting first?
A1: To your first question, yes thats fine. To your second question, you can do that but it would result in a memory leak in the sense that your previous memory would still be allocated but you would lose the pointer to point to it, meannig the program could no longer access it.
Q: Is there smth similar to a garbage collection that we discussed at CS106A when we won’t proactivaly use delete?
A1: C++ does not have garbage collection
Q: Can you use new
before delete
? What happens if you do?
A1: You always have to use new before delete. If you don’t use new to allocate something, then you don’t have a valid pointer to call delete on.
Q: does every class need a destructor?
A1: Yes if it allocates any memory
Q: so if we assign a new space for something, does it overwrite or clear the current value in space then put the new value?
A1: When we reserve a new space (using new), the contents of the space have whatever contents were previously left there
Q: If you allocate a class object on the heap with e.g. new Fraction, when is the destructor called? (Since it wouldn't go out of scope)
A1: It will only be called when you explicity use delete on that object
Q: Oops i meant if you use new
twice before delete in my previous question.
Q: what do you mean by a class being no longer viable?
A1: I’m not sure I caught Chris’s point on that – what was the context?
Q: If we have "delete" in the destructor, does this mean we'll create a pointer in the constructor?
A1: Very likely, yes.
Q: Is there not a good way to order to garbage collector to wait while certain time-sensitive processes are running in garbage-collected languages?
A1: The closest thing to control over the garbage collector that we have is to use delete
A2: Some languages do give the programmer some control over when the garbage collection runs/doesn’t run, but that isn’t nearly the same control over those costs as a language like C++ where all allocation/deallocation is explicit
Q: Did you write it in LaTeX :)
A1: live answered
Q: Can you ask the user how many pages he wants
A1: live answered
Q: so the thing that makes memory 'dynamic' is the fact that your 'spot' can grow and shrink right ?
A1: live answered
Q: Basically, it’s making a new, bigger array on the spot instead of you having to make an entirely different program with a bigger array?
A1: Yes!
Q: What is the difference between delete and destructor again?
A1: live answered
A2: The destructor is a special method of a class that gets called when the class instance is going out of scope. Most of the time we will use delete in the destructor to free any dynamic memory that the class may have used during its lifetime.
Q: If an array is of a fixed size, how is Vector built on top of it?
A1: live answered
A2: We’ll see that in a couple lectures!
Q: who is supposed to report when the function goes out of scope - the programmer or the program itself
A1: live answered
A2: A variable automatically goes out of scope when exiting its code block
A3: The computer will automatically call the destructor when it realizes a variable has gone out of scope
Q: what do you mean by the class goes out of scope?
A1: live answered
A2: When you have an instance of the class as a local variable and the local variable falls out of scope (aka at the end of a define if/else block or function where it has been defined)
Q: how would you set the value for int* myInt = new int; Is it just myInt = 5?
A1: live answered
A2: We’ll talk about that in a couple lectures when we go into pointers in more depth!
Q: I still don’t really understand the difference between destructor and delete…
A1: live answered
Q: so automatic deconstricution (deletion) only applies to classes? How does this relate to how dynamic memory allocations last untnil the end of the program?
A1: live answered
Q: For the classes and programs we have written so far in this class, do our local variables all still take up memory even after the function ends?
A1: No
Q: When is the diagnostic again?
A1: live answered
A2: Wednesday and Thursday of next week
Q: so automatic deconstricution (deletion) only applies to classes? How does this relate to how dynamic memory allocations last untnil the end of the program?
A1: live answered
Q: Unrelated, but do section leading applications open every quarter?
A1: Yes!
A2: Yes, there is an appication period every quarter
Q: Does the indexing operator automatically follow the pointer? Since we assigned to array[i] earlier
A1: live answered
Q: Is there a point of putting an empty destructor?
A1: live answered
A2: No, all classes have an empty destructor by default
Q: So delete will be able to "clear" up some memory for other uses in a program?
A1: Correct
A2: Yes, exactly it will free it up for other components of the program to use
Q: Are local variables to a function automatically dynamically allocated?
A1: No
A2: No, local variables are never dynamically allocated!
Q: you can assign value to memoery like this blockOfInts[i] = 42?
A1: live answered
A2: live answered
Q: even after deleting the memory using delete, you can reassign the pointer to new memory, and you dont need the int* for that
A1: live answered
A2: Yes, the int* is naming the type for the variable declaration, you can later assign to that variable without redeclaring it.
Q: So the mid quarter test is not multiple choice but all live coding quetsions right?
A1: live answered
A2: It will be coding + some code reading and free response
Q: do std::strings and stanford strings have automatic deconstructors because they’re classes?
A1: live answered
Q: sorry i meant freeing the memory
Q: Would the dianostic have any conceptual questions to answer similar to homework or it would only have the problem solving part?
A1: live answered
A2: There might be some free response questions yeah
Q: In your experience, what are good situations to dynamically allocate memory to variables and when should you not bother with it?
A1: live answered
Q: What is the difference between deleting and reallocating the memory, and creating a new array of a larger size and copying over?
A1: live answered
Q: Is it good style to declare with new on global scope?
A1: live answered