Lecture 6 | Programming Paradigms (Stanford)
Top Comments
All Comments (28)
-
@glcrazy i think that was a typo.. he wanted to check elemSize not s->elemSize, to make sure it was not 0 before he set it to s->elemSize
-
@Ghouly89 you're right, thanks
-
@algnadjib That's not the reason why you don't need to cast the pointer.
What happens is that the compiler implicitly casts the void * returned by malloc to the pointer type you need, in this case an int *. If the pointer remains a void *, or a typeless pointer, then it doesn't matter if we know that all pointers are 4 bytes wide, because we still have NO idea how big the object it is pointing to is.
-
@blackcompe cool! thanks!
-
@zengrz No, because your going to memcpy() into that space on the next invocation of stackpush(). E.g. $ - bottom of stack $|2|1|3| logLength = 2 So, 3 is the last element in the stack. The client calls stackpop(). I don't need to deallocate or erase anything, just the return value and set logLength accordingly. $|2|1|3| logLength--; So, 1 is the last element in the stack. Now the client calls stackpush(7). I just overwrite the next element since its garbage. logLength++; $|2|1|7|
-
@43:00 do you not have to deallocate the space that is supposed to be popped before exiting the stackpop function, since you used the 'memcpy' and not 'memcut' or something?
-
@kaarta no casting needed because every pointer is a 4 byte wide
-
This guy is a genius. Clear as water.
At 29:00 assert(s->elemsize > 0) should be assert (elemsize > 0) because s->elemsize is not yet attributed to elemsize, so there is garbage in it.
glcrazy 2 years ago 20
Good stuff. I like this guy's enthusiasm.
region1111 2 years ago 9