Call Stacks - CS50 Shorts

  Рет қаралды 130,885

CS50

CS50

Күн бұрын

Пікірлер: 142
@realVertiqo
@realVertiqo 4 жыл бұрын
Seriously Guys put this video into weeks 3 shorts as an addon to the recursion one, this explains it so good!
@marketsareopenmao
@marketsareopenmao 4 жыл бұрын
Perfect course until right here too! I don't get how they show us that merge sort algorithm then don't tell us how it's working. I assume we are supposed to learn how to build a merge sort then but no way you can do that without perfect knowledge of the call stack.
@GrumpyStoic
@GrumpyStoic 3 жыл бұрын
@@marketsareopenmao @cs50 Yeah I got stumped in Week 3's Merge sort too. Just happened across this video now so hoping it'll help me progress.
@marketsareopenmao
@marketsareopenmao 3 жыл бұрын
@@GrumpyStoic lol yeah 11 months later I stopped there... I want to get back to it but it took a while to understand merge sort. Then I got too busy to continue.
@gonzaotc
@gonzaotc 3 жыл бұрын
Good advice.
@djm_852
@djm_852 3 жыл бұрын
Yes - for me the recursion short didn't make sense (i.e. HOW does the function know!!) until I saw this. The videos should be merged.
@maximbrykov
@maximbrykov 6 жыл бұрын
Finally, I got recursion and stack frames after your explanation. Thank you Doug.
@thewallstreetjournal5675
@thewallstreetjournal5675 5 жыл бұрын
Finally I see what is meant by a return. It makes no sense without the concept of the stack frame.
@marnierogers3931
@marnierogers3931 2 жыл бұрын
I took a CS50 course online and have now decided to do a computer science degree thanks to that course. I always come back to Doug's videos when I need a10/10 explaination. Cheers Doug!!
@lsdengo1589
@lsdengo1589 Ай бұрын
I have decided to do a computer science degree as well! How is it for you so far?
@ricklangston8895
@ricklangston8895 6 жыл бұрын
Finally! I was trying to figure out this exact recursion problem without knowing what a call stack was. Then I saw 'call stack' briefly mentioned on Stack Overflow, did a Google search, found this video, watched it, and saw the light at the end of the tunnel. For a while, I thought I was just not able to grasp recursion and that it was a concept enjoyed by only the most sophisticated intellects...kind of like looking at one of those old 3-dimensional posters that contained a hidden image obscured by garble that could only be revealed if you stared at the poster with the right amount of eye blur, mind shift, or focal point. I never did find the 'knack' for looking at those posters and, before watching this video, I was certain I'd have to dismiss recursion as another one of those things I "just didn't get". This video really helped me out. Recursion might not be so strange after all. Thanks.
@JackyVSO
@JackyVSO 4 жыл бұрын
This was great. I was confused after the Week 3 lecture and still confused after the Recursion video but now I get this principle.
@shreephadke3679
@shreephadke3679 4 жыл бұрын
This video was incredibly easy to understand and the concept was very eloquently taught. Thank you, Doug!
@oussamatarhi6454
@oussamatarhi6454 4 жыл бұрын
Frankly, those Shorts are a knowledge treasure. Thank you David J. Malan, thank you Doug Lloyd, thank Harvard.
@albinsopaj
@albinsopaj 4 жыл бұрын
Where was this video before? I really needed it? Finally. Easy to understand.
@davidallsopp4030
@davidallsopp4030 4 жыл бұрын
the way of thinking about this in terms of what is "active" and what is "paused" in the stack is super helpful!
@mollyexten4508
@mollyexten4508 4 жыл бұрын
Again, I love these visual examples of functions and the order in which they pop off the top of the stack. This is so helpful!
@lb9726
@lb9726 5 жыл бұрын
best visual explanation on recursion. I finally understand it. Thank you.
@beaumiffle
@beaumiffle 3 жыл бұрын
If anyone needs a visual explanation; I've wrote it out below. I couldn't understand till I typed everything out, hopefully this helps someone too. FUNCTION: int fact(int n) { if (n == 1) return 1; else return n * fact(n-1); } int main(void) { printf("%i ", fact(5)); } EXPLANATION: int fact(int 5) { does (5 == 1)? no move to else statement; else return 5 * fact(5-1); [aka: return 5 * fact(4)] [what is fact(4)? let's find out] } int fact(int 4) { does (4 == 1)? no move to else statement; else return 4 * fact(4-1); [aka: return 4 * fact(3)] [what is fact(3)? let's find out] } int fact(int 3) { does (3 == 1)? no move to else statement; else return 3 * fact(3-1); [aka: return 3* fact(2)] [what is fact(2)? let's find out] } int fact(int 2) { does (2 == 1)? no move to else statement; else return 2 * fact(2-1); [aka: return 2 * fact(1)] [what is fact(1)? let's find out] } int fact(int 1) { does (1 == 1)? YES return 1; else statement not used } thus; fact(1) = 1 NOW BACK WE GO BACK! int fact(int 2) { else return 2 * fact(2-1); } return 2 * fact(1) what is fact(1)? 1 return [2 * 1] = 2 thus; fact(2) = 2 int fact(int 3) { else return 3 * fact(3-1); } return 3 * fact(2) what is fact(2)? 2 return [3 * 2] = 6 thus; fact(3) = 6 int fact(int 4) { else return 4 * fact(4-1); } return 4 * fact(3) what is fact(3)? 6 return [4 * 6] = 24 thus; fact(4) = 24 int fact(int 5) { else return 5 * fact(5-1); } return 5 * fact(4) what is fact(4)? 24 return [5 * 24] = 120 thus; fact(5) = 120 THEREFORE: printf("%i ", fact(5)); fact(5) = 120 print: 120
@ManhwaFlashbackForFun
@ManhwaFlashbackForFun 2 жыл бұрын
thanks for your explanation, I was confused in int fact 4 when he said 4 * 6
@stonehead66
@stonehead66 5 ай бұрын
Thank you very much!
@satya-gs4sc
@satya-gs4sc 5 ай бұрын
what about the space complexity ? can you please clarify
@mohdshkaib
@mohdshkaib 4 ай бұрын
thanks
@rdx86
@rdx86 4 жыл бұрын
I FINALLY understand recursion! After so many years! Thank you Doug and CS50!
@Hermeticoh11
@Hermeticoh11 11 ай бұрын
Thank you, Doug! This is really helpful. I feel like my mind opens and all of a sudden I can understand how everything I've programmed this far works.
@laurisskraucis2247
@laurisskraucis2247 6 жыл бұрын
Amazing! So grateful for your lessons Doug!
@JimMavrin
@JimMavrin Жыл бұрын
Thank you so much for the explanation! I was gonna bang my head against the wall cuz I simply coundn't understand how recursion works in the mario example David wrote. I spent probably more than an hour jumping through hoops trying to make GPT3 explain it, but I simply coudln't make sense of it. Now with 4:30 it all makes sense.
@andrewpratt3861
@andrewpratt3861 4 жыл бұрын
This brought a bunch of concepts together for me. Thanks, Doug!
@esjihn
@esjihn 6 жыл бұрын
This and the numberphile explanation (recursion) are the best explanations on the web.
@DylanRobert1
@DylanRobert1 4 жыл бұрын
Wow this is necessary for understanding recursion in week 3. Thank you for the information.
@LUITEN1
@LUITEN1 4 жыл бұрын
Thanks so much Doug!!! The quality of explanation on your videos are superb!!
@maxim5519
@maxim5519 Жыл бұрын
The first explanation of recursions during all weeks in CS50 what I was able to understand 🙂 You definitely have to change Short name from Call Stacks to Recursions !!!
@omar24191
@omar24191 4 жыл бұрын
Best Recursion explanation I found in the web! Thanks Doug!
@Timo-Epis
@Timo-Epis Жыл бұрын
I did understand recursion but I invented my own concept about it and you finally made the link between call stack and recursions when I was learning about call stacks. Thank you!
@aaronog4117
@aaronog4117 Жыл бұрын
I love you, Doug. Thank you for your explanation. It helps me to become a better person.
@saul6893
@saul6893 Жыл бұрын
You just explained magic to me!
@girijababu
@girijababu 3 жыл бұрын
Awesome explanation...From the day I learned recursion in data structures and algorithm subject years back, I had this doubt. How the variables are retained in each call and the final output. Today this video gave me the answer...great relief
@mandyzhang9531
@mandyzhang9531 3 жыл бұрын
Such a great video! You explained the call stack so well! thank you Doug.
@patriotpatriotic3894
@patriotpatriotic3894 6 жыл бұрын
Thank you very much for this explanation and visualisation!
@Honestly_Marie
@Honestly_Marie 4 жыл бұрын
Doug and his sexy codes lol, that is really all I remember from that recursion video.
@GrumpyStoic
@GrumpyStoic 3 жыл бұрын
That bit was a classic. Love ya Doug you sexy code beast!
@fakrulotaku5655
@fakrulotaku5655 8 ай бұрын
Not sexy, it was “seaxy”
@JuliaFeyst
@JuliaFeyst 2 жыл бұрын
only after this video, I resolved tideman problem from Week 3 problem set. THANKS DOUG
@realEraldoNeto
@realEraldoNeto Жыл бұрын
Thank you very much, Doug! It was very intuitive and your explanation was precise and direct. :))
4 жыл бұрын
Doug you are an angel, thank you very much for the explanation.
@dixztube
@dixztube 3 жыл бұрын
This was really great explanation. Very simple easy to follow
@iEatBass
@iEatBass 4 жыл бұрын
Doug you're the man, dude!
@bactran7799
@bactran7799 4 жыл бұрын
thank you, it's so clear. Help me a lot to understand how recursion works and call stacks
@3du45d
@3du45d 4 ай бұрын
Hey guys, just stumbled across this video, but please note that the call stack is not correct as shown from around 04:05. However, the "fact" function does go on the stack before printf!!!!.
@OfficialSombrero
@OfficialSombrero 2 жыл бұрын
I finally understand recursion now. from the prior weeks lecture I was like well if its returning 1 when it gets to fact(1), how is it going to solve for the actual value. This should have been in the prior weeks lectures but i understand its a lot to fit in. Thanks
@learnerjetix
@learnerjetix 9 ай бұрын
Finally understood why the return 1 in the base case does not end the program.
@luckydevil1601
@luckydevil1601 Жыл бұрын
Incredible explanation! Thank you!
@KaungSettLwin-x7e
@KaungSettLwin-x7e 7 ай бұрын
As a self though I have the problem understanding recursion. Some say "If you want to learn recursion you have to know recursion". Turns out that to understand recursion, we need to understand how call stacks work. And then the rest is become easy. Thanks
@gauravpdaksh
@gauravpdaksh 3 жыл бұрын
CS50 is the best course. I wish I could goto Harvard.
@waltertaju
@waltertaju 4 жыл бұрын
Finally understood recursion properly with this explanation!
@TheOctavaTube
@TheOctavaTube 2 жыл бұрын
Great presentation. Minor point on this factorial example, it seems that the call stack should also include the 'n' value , n*fact(n-1) fact(1) 2*fact(1) ---- fact(1) 3*fact(2)---- fact(2) 4*fact(3)---- fact(3) 5*fact(4)----fact(4) fact(5) ------fact(5) print() -----print() main() ---- main()
@AyushDhingra
@AyushDhingra 4 жыл бұрын
If we give a much bigger value to fact(), will there be a case when the memory contains so much paused fact() frames that the program crashes? By the way, this is a very helpful video.
@braceleerohith
@braceleerohith 4 жыл бұрын
yup a stack overflow occurs.
@nannubedi7773
@nannubedi7773 5 жыл бұрын
Very nice sir!! This indeed helped a lot. Thank you so much.
@kieranbarker1902
@kieranbarker1902 Жыл бұрын
This is fantastic, thank you!
@qq-mf9pw
@qq-mf9pw 2 жыл бұрын
Very clear explanation! Small nitpick, printf() should not have a stack frame in this example. Arguments are evaluated first (building on top of the main() frame) and all fact() frames will be removed from the stack before calling printf(). But this is not a big deal at an introductory level :)
@Ryan-xb1ry
@Ryan-xb1ry 2 жыл бұрын
This is so great. Thank you
@raztubes
@raztubes Жыл бұрын
Pedantic point, but printf is called AFTER the fact completes. That parameter has to be evaluated before the call enters printf. Then again, been a while since i've done C, so i might be wrong.
@nowak93
@nowak93 2 жыл бұрын
Got a little while before I figured out recursion last week, the clue was what I returned up the stack.
@AnujTiwari-g8o
@AnujTiwari-g8o Жыл бұрын
kya baat hai guru maja aa gaya . waah waah waah waah .
@maboy1994
@maboy1994 4 жыл бұрын
These folks are brilliant! I wonder why this video is excluded from the shorts in week 3?
@xinguangmingwo2343
@xinguangmingwo2343 4 жыл бұрын
Thank you very much. It saves me a lot of time.
@kaliomar5988
@kaliomar5988 5 жыл бұрын
great explanation Mr. Lloyd
@nikbobrov5045
@nikbobrov5045 4 жыл бұрын
I never saw this kind of explanation. Now recursion is transparent to me like spring water.
@philteng760
@philteng760 5 жыл бұрын
Thank you, great work, life saver! Thank you so much!
@alialparslan2288
@alialparslan2288 3 жыл бұрын
Until this short, I thought that I understand what is recursion. Now I understand well.
@mushahidhussain1516
@mushahidhussain1516 6 жыл бұрын
You're amazing sir.
@asrcool4416
@asrcool4416 2 жыл бұрын
Very nice, easy to understand explanation! Thank you!
@NeKe4rl
@NeKe4rl 5 жыл бұрын
Very clear, thank you
@gregoriomiranda6917
@gregoriomiranda6917 2 ай бұрын
Now I understand the Mr. Meeseeks episode of Rick and Morty
@meghnasingh4396
@meghnasingh4396 4 жыл бұрын
Very well put ! Thank you very much !
@jsteezeful
@jsteezeful 4 жыл бұрын
Excellent explanation! Thank you.
@flyte9844
@flyte9844 3 жыл бұрын
best video on recursion ! thx !
@MrSkyydude
@MrSkyydude Жыл бұрын
Really good explanations.
@Ronaldoforever3
@Ronaldoforever3 3 жыл бұрын
Explained in very easy way tysm.
@braceleerohith
@braceleerohith 4 жыл бұрын
so that's why the return type is so important ...OMG..now everything makes sense.
@Liam-10
@Liam-10 Жыл бұрын
Thank you ❤️
@jessif.
@jessif. 2 жыл бұрын
Thank you.
@jeroenkoffeman9402
@jeroenkoffeman9402 4 жыл бұрын
great visual explanation!
@NKLGaming01
@NKLGaming01 3 ай бұрын
Thank you very much
@ian_senior
@ian_senior 4 жыл бұрын
protip: 3:34 you can remove the else and get the same results. like so... int fact(int n) { if (n == 1) return 1; return n * fact(n - 1); }
@freshlemon101
@freshlemon101 4 жыл бұрын
Or, int fact(int n) { return n == 0 ? 1 : n * fact(n - 1); }
@exnihilonihilfit6316
@exnihilonihilfit6316 4 жыл бұрын
@@freshlemon101 You mean "n == 1", not 0.
@freshlemon101
@freshlemon101 4 жыл бұрын
@@exnihilonihilfit6316 I included 0 factorial
@exnihilonihilfit6316
@exnihilonihilfit6316 2 жыл бұрын
You're correct; your version works for factorial 1 and factorial 0. Good job. They completely ignored factorial 0 in this exercise - I guess because they didn't want to deal with explaining why 0 factorial is 1. :) (and it took me over a year to respond) :]
@amaldev4150
@amaldev4150 4 жыл бұрын
why isn't this video on lect 3? this gave me so much hard time ! i couldn't understand recursion, and i was so stressed for not understanding it! please put this on lect 3
@Avalanchanime
@Avalanchanime 2 жыл бұрын
Thank you
@subvertedhope
@subvertedhope 4 жыл бұрын
Wish someone would have just told me that functions work like Magic: the Gathering cards from the start.
@stoicstone521
@stoicstone521 Жыл бұрын
understood. thank you
@itspurelypassionate
@itspurelypassionate 4 жыл бұрын
Oh.. now i understand recursion.Thanks
@abd-elrahmanmohamed9839
@abd-elrahmanmohamed9839 6 жыл бұрын
Well explained , Thanks !
@PareshRadadiya22
@PareshRadadiya22 3 жыл бұрын
Doug Lloyd is awesome man. 👨🏼‍🏫
@codeative
@codeative 6 жыл бұрын
So clear, thank you :)
@pfever
@pfever 2 жыл бұрын
3:37 this is technically wrong, before printf() can be called fact(5) must be evaluated.
@sia3540
@sia3540 3 жыл бұрын
How do I troubleshoot Windows 10 error "new guard page for the stack cannot be created"? I know nothing about programing!
@a.rangarajanrajan3239
@a.rangarajanrajan3239 5 жыл бұрын
Thanks for grate vedio. Who will handle this frame creation if we don't have is, For example general embedded systems
@maxfeliciello4761
@maxfeliciello4761 6 жыл бұрын
Awesome, Thanks!!
@ridoychandradey4446
@ridoychandradey4446 2 жыл бұрын
It's all clear now how recursion actually work under the hood. Thank you Doug Lloyd.
@DaniloSilva-pl3sq
@DaniloSilva-pl3sq 4 жыл бұрын
GOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOD
@kakashisenpai99
@kakashisenpai99 4 жыл бұрын
Thanks a ton !
@MichaelFoley64
@MichaelFoley64 7 жыл бұрын
main calls fact(5) which returns then main calls printf(string,result of calling fact(5))
@AFStimo2
@AFStimo2 5 жыл бұрын
yep, this comment should be on top - fact is called first and printf is called only when all fact calls are done, otherwise there is no parameter to pass to printf
@markmathias9240
@markmathias9240 4 жыл бұрын
@@AFStimo2 The code is written such that while the fact function is defined, it doesn't come into play until after "int main(void)"
@max500k
@max500k 5 жыл бұрын
Great video, very clear explanation, thanks!
@GOODBOY-vt1cf
@GOODBOY-vt1cf 4 жыл бұрын
thank you!
@yonaxl
@yonaxl 4 жыл бұрын
So is this a good way or not a good way to program?
@anuarsadykov9707
@anuarsadykov9707 5 жыл бұрын
If I had fact(1 000 000 000), would it be reasonable to iterate values in a loop instead of using recursion? If I use recursion, functions would stack up billion times and occupy a lot of memory, while loop operates inside main(). Which one occupies more space in memory recursion or iteration in a loop method? Which one is faster?
@kaliomar5988
@kaliomar5988 5 жыл бұрын
loops is faster and it occupy less space in memory than recursion.
@balupabbi
@balupabbi 4 жыл бұрын
how does call stack look like with tail recursion and why does it use less stack memory compared to regular recursion?
@nshk7163
@nshk7163 4 жыл бұрын
I understood recursion functions but still don't fully understand how to implement it instead of an iterative one
@asashish905
@asashish905 6 жыл бұрын
I have a question, is this applicable on all callbacks? Or only Ajax calls and set Timeout?
@rtrs5413
@rtrs5413 2 жыл бұрын
i was just curious how you first learned this??
@김태우-o5h3f
@김태우-o5h3f Жыл бұрын
very helpful!!
@arielspalter7425
@arielspalter7425 4 жыл бұрын
Excellent explanation! Where are all the likes?
@stefaneew
@stefaneew 2 жыл бұрын
You are my hero. I waned to die because of not understanding recursion on the week 5
The Call Stack and Stack Overflows (example in C)
12:56
Jacob Sorber
Рет қаралды 47 М.
Recursion - CS50 Shorts
13:50
CS50
Рет қаралды 174 М.
BAYGUYSTAN | 1 СЕРИЯ | bayGUYS
36:55
bayGUYS
Рет қаралды 1,9 МЛН
coco在求救? #小丑 #天使 #shorts
00:29
好人小丑
Рет қаралды 120 МЛН
wtf is “the stack” ?
8:03
Low Level
Рет қаралды 127 М.
Tail Recursion Explained - Computerphile
16:05
Computerphile
Рет қаралды 178 М.
The JS Call Stack Explained In 9 Minutes
9:30
Colt Steele
Рет қаралды 89 М.
AI Is Making You An Illiterate Programmer
27:22
ThePrimeTime
Рет қаралды 266 М.
Hash Tables - CS50 Shorts
18:47
CS50
Рет қаралды 152 М.
File Pointers - CS50 Shorts
18:22
CS50
Рет қаралды 155 М.
5 Simple Steps for Solving Any Recursive Problem
21:03
Reducible
Рет қаралды 1,3 МЛН
Stack vs Heap Memory - Simple Explanation
5:28
Alex Hyett
Рет қаралды 279 М.
BAYGUYSTAN | 1 СЕРИЯ | bayGUYS
36:55
bayGUYS
Рет қаралды 1,9 МЛН