Can't emphasize enough how valuable this insight into what's going on behind the scenes is. The people who make it through this and the other lectures will be more aware of those nuances and their consequences.
No assignment is random, I mean they took care that even how they stored data in assignment 2, was on the same lines so students can get good feel of it before this lecture. I am blown away. Professor Jerry Cain and TAs thanks a lot.
You mean the mac os bit? Basically just saying that when you compact the heal you change the pointers to the beginning of each allocated block,. they had a compactable heap (which I guess is not really necessary now ram is so cheap compared to processing power) with a seperate memory block that would hold the pointers memory blocks in that section the heap (handle) so that when compacted and the memory changed, the client could still access the data correctly
the structure I like using for Heap implimentation is: void*Prev; void*Next; void*NextFree; int ThisSize; It fits boundary and makes it easier to link in and out for the nodes. The parent is pointing to dummy nodes at beginning and end of heap space. But that's just how I do it *shrugs*
At around 16:30 he doesn't seem to explain how the size of each 'free' block is known by malloc/realloc. He explains how the location of the next free node is known but not the length of the region. Surely it needs to store the length of the free block also? I cannot be calculated by subtracting the node addresses for example.
First off, what are you trying to accomplish here? What do you mean it doesn't work? If you are trying to print "test" to the screen, string and newstring are two different character arrays, which are not scoped in.
1) yes, I was trying to print "test" considering both functions have the same (in theory) local variables, in the same locations in memory, the second function should pick up "test" as a garbage value
It actually does not have the same local variables. The two variables inside the function A and B are stored in two different locations. If you wanted B to get the string, you'd have to call A which returns or has an out parameter for the string. Then store it in newstring.
So I compiled it again, to try to fix whatever the problem was. It works! I have no idea why it does now but it didn't when I commented that. Code (works in both gcc and clang in linux): ---- #include void A() { char string[] = "test"; } void B() { char newstring[5]; printf("%s ", newstring); } int main() { A(); B(); return 0; } ---- if you put printf("%p ")s in each function, you should see that they're the same adresses Edit: tried it on termux and it didn't work until I put the lines to print the adresses. Looks like it's implementation dependant unless you do that.
Interesting, but not surprised. When you call a function it is placed on the stack and the stack pointer is incremented. Once it returns the function gets popped off the stack and the pointer gets decremented. If the functions have similar structures I'm sure the some of the variables will have the same address in the stack as the other variables from different functions, depending on how the function gets called. So lets say you called A inside B, after you print out the address. You should get a different address for it's variable from the first call you did to A, correct? Try saving the string. Use a void* which is globally visible, and set it to one of the strings inside the functions. Cast it and print it in the main function, what do you get? I'm gonna guess something that looks like garbage
P.S. If you want to learn C I suggest you will not learn much from CS 107. The professor that they all rave about makes more errors than you can shake a stick at.
This lecture alone is worth my entire programming career, absolutely gold.
Can't emphasize enough how valuable this insight into what's going on behind the scenes is. The people who make it through this and the other lectures will be more aware of those nuances and their consequences.
I really like the way He explains everything. So easy, yet so good! thank You Stanford for sharing those lectures.
my god all of these things happen with memory, and we take it for granted.. cannot fathom the idea of this course being free.
No assignment is random, I mean they took care that even how they stored data in assignment 2, was on the same lines so students can get good feel of it before this lecture. I am blown away. Professor Jerry Cain and TAs thanks a lot.
hi user, can you link to the assignment repo for this course for this specific year?
Full of insight in every sentence. Thank you!
This is a golden lectures! =) Thanks!
Finally getting to the interesting part in the last part of the lecture.
He is really good, easy, complete and precise.
this intro class is gold !! my college's intro class was meh...
just great !! The professor rocks..
You mean the mac os bit? Basically just saying that when you compact the heal you change the pointers to the beginning of each allocated block,. they had a compactable heap (which I guess is not really necessary now ram is so cheap compared to processing power) with a seperate memory block that would hold the pointers memory blocks in that section the heap (handle) so that when compacted and the memory changed, the client could still access the data correctly
Very usefull lecrute.Thx!
the structure I like using for Heap implimentation is: void*Prev; void*Next; void*NextFree; int ThisSize; It fits boundary and makes it easier to link in and out for the nodes. The parent is pointing to dummy nodes at beginning and end of heap space. But that's just how I do it *shrugs*
Year 2019, there is no AI yet that organize the heap memory for better performance?
This makes sence man!
yes it happens also on youtube university:D
He says -"I've got about 25 seconds."
My god do they keep track of the lectures down till seconds?!
At around 16:30 he doesn't seem to explain how the size of each 'free' block is known by malloc/realloc. He explains how the location of the next free node is known but not the length of the region. Surely it needs to store the length of the free block also? I cannot be calculated by subtracting the node addresses for example.
@wobblejuice I think it's the lack of prof Mehran Sahami :)
"Does that make sense to people?" x 1000
the part how the heap get compacted kill me
a lot less views than from the first lecture. people dropping off like flies.
Sorry, but www.CS107.stanford.edu/ doesn't work anymore. Instead, web.stanford.edu/class/cs107/ should be used
Good video, does this makes sense? I now understand a lot more, does this makes sense to people? Ok?
So I made an experiment:
Main()
{
A();
B();
Return 0;
}
A()
{
Char string[]="test";
}
B()
{
Char newstring[5];
Printf("%s
", newstring);
}
Does anyone know why it doesn't work?
First off, what are you trying to accomplish here? What do you mean it doesn't work?
If you are trying to print "test" to the screen, string and newstring are two different character arrays, which are not scoped in.
1) yes, I was trying to print "test"
considering both functions have the same (in theory) local variables, in the same locations in memory, the second function should pick up "test" as a garbage value
It actually does not have the same local variables. The two variables inside the function A and B are stored in two different locations. If you wanted B to get the string, you'd have to call A which returns or has an out parameter for the string. Then store it in newstring.
So I compiled it again, to try to fix whatever the problem was.
It works! I have no idea why it does now but it didn't when I commented that.
Code (works in both gcc and clang in linux):
----
#include
void A()
{
char string[] = "test";
}
void B()
{
char newstring[5];
printf("%s
", newstring);
}
int main()
{
A();
B();
return 0;
}
----
if you put printf("%p
")s in each function, you should see that they're the same adresses
Edit: tried it on termux and it didn't work until I put the lines to print the adresses. Looks like it's implementation dependant unless you do that.
Interesting, but not surprised. When you call a function it is placed on the stack and the stack pointer is incremented. Once it returns the function gets popped off the stack and the pointer gets decremented. If the functions have similar structures I'm sure the some of the variables will have the same address in the stack as the other variables from different functions, depending on how the function gets called.
So lets say you called A inside B, after you print out the address. You should get a different address for it's variable from the first call you did to A, correct?
Try saving the string. Use a void* which is globally visible, and set it to one of the strings inside the functions. Cast it and print it in the main function, what do you get? I'm gonna guess something that looks like garbage
*glug*
I like the way he says "Mmm" after everything. Kind of sexy.
druble O.o
don't know why... what he's saying is common sense really =/ the only problem i can see people having is if they aren't familiar with the terminology.
potato
P.S. If you want to learn C I suggest you will not learn much from CS 107. The professor that they all rave about makes more errors than you can shake a stick at.