Understanding Static, Stack, and Heap Memory Regions (Examples in C)

แชร์
ฝัง
  • เผยแพร่เมื่อ 28 พ.ย. 2024

ความคิดเห็น • 67

  • @Bestcuriosity_1
    @Bestcuriosity_1 3 ปีที่แล้ว +14

    Today I found real teacher who can make me love c programming to go deep dive in this field
    thank you sir for making such beautiful lecture series for us graduate student who got stuck in such hard concepts which was explained by you in easy manner.
    Huge Respect & love from India

  • @hadiasghari2266
    @hadiasghari2266 3 ปีที่แล้ว +15

    Studying CS in Germany and by far you are the best. NO ONE can explain it like you. Shame on my Prof

  • @rotrose7531
    @rotrose7531 3 ปีที่แล้ว +14

    Thank you very much, it it difficult to find real c programming tutorials, and u provided a perfect one. Please do not stop

    • @jonathanmuckell9827
      @jonathanmuckell9827  3 ปีที่แล้ว +2

      Thanks for the encouragement! Glad you found this helpful.

  • @milosb793
    @milosb793 6 หลายเดือนก่อน

    I've been learning this on the university for couple of years, and none made me understood this that good as you. Really quality content. Thank you and keep pushing!

  • @tlwilkinson
    @tlwilkinson 2 ปีที่แล้ว

    This is the best video online explaining heap and stack.

  • @yonatanam1
    @yonatanam1 2 ปีที่แล้ว +9

    Global variables are accessible even from other c modules by using the extern statement. However, if u add static to the global variable, then it is not accessible from other modules. This means that you can define another global static variable with the same name in another module, and it will be separate from the first one which was declared in the other module.

    • @insoft_uk
      @insoft_uk ปีที่แล้ว +1

      I use static as well not all the time just as you pointed out it’s actually an important part to know so one knows when it’s needed or not and also used for functions I think he made a big mistake as beginners looking at this video are going to think it’s optional and not necessary at all.

  • @wilfredv1930
    @wilfredv1930 ปีที่แล้ว

    Your videos about programming are a treasure. thanks a lot!

  • @pietraderdetective8953
    @pietraderdetective8953 7 หลายเดือนก่อน

    Amazing explanation on this memory topic. Well done Sir, well done!

  • @nicolasbourbaki8896
    @nicolasbourbaki8896 ปีที่แล้ว

    I wished I had watched this video when I started c coding! Damn job explaining pointers!

  • @davidjiang7929
    @davidjiang7929 3 ปีที่แล้ว

    This is a really good explanation of the variable lifetimes.
    I came here because programming language analysis class just introduced this, and the textbook did not have good explanations on these concepts.

  • @redmundperrz7234
    @redmundperrz7234 4 หลายเดือนก่อน

    Finally its crystal clear now for me this topic

  • @BigManTing506
    @BigManTing506 3 ปีที่แล้ว +2

    You're so good at explaining concepts!

  • @ArunKumar-ll9lo
    @ArunKumar-ll9lo 4 ปีที่แล้ว +2

    A very good informative session. Keep it up..

  • @douglasthomson2986
    @douglasthomson2986 2 ปีที่แล้ว

    Great explanation and super useful. Thanks from the UK.

  • @jakenguyen2584
    @jakenguyen2584 2 ปีที่แล้ว

    Awesome video, Jonathan. Your slow, detailed explanations really help with drilling the points into my slow brain.

  • @ProfessorLinux
    @ProfessorLinux ปีที่แล้ว

    I appreciate this series. You are a great teacher.

  • @rajeshhariharan7575
    @rajeshhariharan7575 2 ปีที่แล้ว

    Wonderfully explained; thanks a lot Prof. Jonathan Muckell

  • @sallaklamhayyen9876
    @sallaklamhayyen9876 3 ปีที่แล้ว

    Jonathan Muckell you are a wonderful teacher
    great job thank you so much
    please continue

  • @andyburris2555
    @andyburris2555 2 ปีที่แล้ว

    Great video for reviewing these concepts. Correct and concise.

  • @John.C.chhuak
    @John.C.chhuak 2 ปีที่แล้ว

    Best explanation! And I like the visual aids.

  • @acluster3411
    @acluster3411 11 หลายเดือนก่อน

    Great teaching!

  • @onuryorutken5540
    @onuryorutken5540 3 ปีที่แล้ว +1

    Great video with great examples!!! Thank you very much.

  • @lunaxiao9997
    @lunaxiao9997 ปีที่แล้ว

    Great video! Well explained

  • @MrOse-nh5pd
    @MrOse-nh5pd 3 ปีที่แล้ว

    You're truly a good teacher. 👍

  • @THawpeek2006
    @THawpeek2006 3 ปีที่แล้ว +2

    Nice video with good explanation thanks bro for this

  • @denjiuzumaki4309
    @denjiuzumaki4309 4 หลายเดือนก่อน

    nice examples!!

  • @m.houdeib
    @m.houdeib 3 ปีที่แล้ว

    Perfectly explained.

  • @oliverkobing4726
    @oliverkobing4726 3 ปีที่แล้ว

    What a great teacher!

  • @aminedoughmi8582
    @aminedoughmi8582 3 ปีที่แล้ว

    excellent course ! keep up the good work

  • @hubercats
    @hubercats 3 ปีที่แล้ว

    Thanks for sharing this very clear, helpful video!

  • @sendlocation8476
    @sendlocation8476 2 ปีที่แล้ว

    @Jonathan Muckell @Anyone
    So does that mean when you push the first thing onto the stack. It always occupies the highest memory address first? So it allocates at the very top?

  • @jackm.1628
    @jackm.1628 3 ปีที่แล้ว

    Fantastic explanations, thank you professor!

  • @Onlinetraining-h7x
    @Onlinetraining-h7x 8 หลายเดือนก่อน

    May i know what is the purpose of storing address in a pointer when anyways the variable is not removed in heap memory?

  • @m4tchm4k3r5
    @m4tchm4k3r5 3 ปีที่แล้ว

    thank you very much, really helpful. You are gold.

  • @Byynx
    @Byynx 4 หลายเดือนก่อน

    Since HEAP has unlimited memory space available, is the HEAP inside the process memory region or outside ?

  • @ii7mdj_353
    @ii7mdj_353 11 หลายเดือนก่อน

    Yoooooooooooooooooooo, that makes so much sense now.

  • @echoptic775
    @echoptic775 3 ปีที่แล้ว +1

    8:00 doesnt static in c mean that its a private variable/function for that file only?

  • @Manuel-j3q
    @Manuel-j3q 3 ปีที่แล้ว +5

    Does such system of memory regions applies to all languages (C family, Python, Java, etc.) or only to C specifically?

    • @jonathanmuckell9827
      @jonathanmuckell9827  3 ปีที่แล้ว +3

      The memory regions is are used across programming languages. It's a computing fundamental that is supported by both the operating system and the underlying hardware architecture. Many programming languages try to hide some of the messy details from the programmer (C gives you a lot more control). For example, in Java when you use the keyword "new" you are allocating memory space on the heap. The programmer never needs to free this memory, since Java uses a concept called Garbage Collection that automatically frees memory that will no longer be accessed.

    • @Manuel-j3q
      @Manuel-j3q 3 ปีที่แล้ว +1

      ​@@jonathanmuckell9827 not exactly what I meant to ask.
      I meant "is such memory layout/arrangement (stack at the "beginning" of RAM, heap near the "end", and they grow towards each other; static located "outside" of range that memory range between heap and stack) is a standard way of memory representation for all programming languages (regardless is it have manual (C, C++) or automatic (C#, Java) memory management) or such approach specific only to C?
      For example, if I write
      char * heap_char_1 = new char;
      char * heap_char_2 = new char;
      char * heap_char_3 = new char;
      char stack_char = "a";
      in C++, on computer that have only 100 bytes of RAM, and then print addresses of all four characters into console heap_char_1 would have address 100, heap_char_2 99, heap_char_3 98 and stack_char 0, or something like that?! Or that would be true only in C?

    • @jonathanmuckell9827
      @jonathanmuckell9827  3 ปีที่แล้ว +2

      @@Manuel-j3q OK - Good question. It is language independent, since it is enforced at the hardware-level. For example, here is an example of the MIPS architecture. If you look on the second page, you can see the documentation for the MIPS processor's memory allocation. Stack grows from a high address down, while the heap grows from a lower address up. This is common practice across various processor architectures. courses.cs.washington.edu/courses/cse378/09au/MIPS_Green_Sheet.pdf

    • @Manuel-j3q
      @Manuel-j3q 3 ปีที่แล้ว +1

      @@jonathanmuckell9827 huh, I did not expected that it enforced by hardware. Thank you for clarification.
      One last question, which is outside of video topic I think.
      What if I have two programs, HelloWorld and FooBar, running at the same time, probably on different threads, on a single computer. How OS would manage memory for them in this case?
      Divide memory in half between this two programs?
      Or place both stacks/heap/static near each others?
      Or that too specific case that have its own specific rules?

    • @jonathanmuckell9827
      @jonathanmuckell9827  3 ปีที่แล้ว +5

      @@Manuel-j3q That's another really good question. There are a couple different parts to the question. The first part relates to whether you have separate threads or processes. Threads share a common memory address space. Two threads tend to have 2 different stacks, but will can share other variables in heap or static memory regions. Processes have completely separate addresses spaces and will have independent stacks, heaps, static memory regions. The OS uses a concept called Virtual Addresses to divide the memory space between different processes. More information on that topic can be found here: en.wikipedia.org/wiki/Virtual_address_space

  • @guliyevshahriyar
    @guliyevshahriyar ปีที่แล้ว

    Thank you very much teacher.

  • @bwzd
    @bwzd ปีที่แล้ว

    the stack vs static memory example at 10:00 really made something click for me

  • @rodolfokey7835
    @rodolfokey7835 2 ปีที่แล้ว

    Hi Jonathan, thank you for the video. It's very didactic. I have a question: can you access a location in the heap memory (previously allocated with malloc or using "new" in C++) from any function in any class different from the one where the allocation was made? If so, what is the syntax. Thank you

  • @mutalibgozalov7208
    @mutalibgozalov7208 8 หลายเดือนก่อน

    Thank U sir, that was helpful

  • @fc3fc354
    @fc3fc354 ปีที่แล้ว

    But can't you update the value of a static variable?I thought it was impossible to change the value of a static variable

  • @arslanarslan9432
    @arslanarslan9432 2 ปีที่แล้ว

    Thank u, you are amazing !!!!!!!

  • @firedragonmangaming2410
    @firedragonmangaming2410 2 ปีที่แล้ว

    Wait im confused. Does malloc return a void pointer ?

  • @spacewad8745
    @spacewad8745 2 ปีที่แล้ว

    23:00 sizeof is an operator

  • @itsam941
    @itsam941 10 หลายเดือนก่อน

    sizeof is an operator not a function

  • @drogeanunicusor7715
    @drogeanunicusor7715 2 ปีที่แล้ว

    sizeof is not a function, it's an operator

    • @jonathanmuckell9827
      @jonathanmuckell9827  2 ปีที่แล้ว

      Yes, you are correct. Thanks for the clarification. Additional details can be found here: www.geeksforgeeks.org/sizeof-operator-c/

  • @王志伟-y8x
    @王志伟-y8x 3 ปีที่แล้ว

    hello

  • @swedishpsychopath8795
    @swedishpsychopath8795 7 หลายเดือนก่อน

    Explaining this without mentioning the activation record is a waste of time.

  • @dixztube
    @dixztube 10 หลายเดือนก่อน

    Use rust lol

  • @nononnomonohjghdgdshrsrhsjgd
    @nononnomonohjghdgdshrsrhsjgd 2 ปีที่แล้ว

    very boring presentation

    • @osamaragab3325
      @osamaragab3325 2 ปีที่แล้ว

      then you are in the wrong place ! you could just don't watch the video, this man is a legend !!