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

Поделиться
HTML-код
  • Опубликовано: 29 ноя 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

  • @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.

  • @tlwilkinson
    @tlwilkinson 2 года назад

    This is the best video online explaining heap and stack.

  • @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

  • @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!

  • @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!

  • @redmundperrz7234
    @redmundperrz7234 4 месяца назад

    Finally its crystal clear now for me this topic

  • @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!

  • @douglasthomson2986
    @douglasthomson2986 2 года назад

    Great explanation and super useful. Thanks from the UK.

  • @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.

  • @BigManTing506
    @BigManTing506 3 года назад +2

    You're so good at explaining concepts!

  • @ProfessorLinux
    @ProfessorLinux Год назад

    I appreciate this series. You are a great teacher.

  • @sallaklamhayyen9876
    @sallaklamhayyen9876 3 года назад

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

  • @ArunKumar-ll9lo
    @ArunKumar-ll9lo 4 года назад +2

    A very good informative session. Keep it up..

  • @rajeshhariharan7575
    @rajeshhariharan7575 2 года назад

    Wonderfully explained; thanks a lot Prof. Jonathan Muckell

  • @acluster3411
    @acluster3411 11 месяцев назад

    Great teaching!

  • @andyburris2555
    @andyburris2555 2 года назад

    Great video for reviewing these concepts. Correct and concise.

  • @jakenguyen2584
    @jakenguyen2584 2 года назад

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

  • @John.C.chhuak
    @John.C.chhuak 2 года назад

    Best explanation! And I like the visual aids.

  • @MrOse-nh5pd
    @MrOse-nh5pd 3 года назад

    You're truly a good teacher. 👍

  • @lunaxiao9997
    @lunaxiao9997 Год назад

    Great video! Well explained

  • @THawpeek2006
    @THawpeek2006 3 года назад +2

    Nice video with good explanation thanks bro for this

  • @m.houdeib
    @m.houdeib 3 года назад

    Perfectly explained.

  • @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?

  • @bwzd
    @bwzd Год назад

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

  • @onuryorutken5540
    @onuryorutken5540 3 года назад +1

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

  • @denjiuzumaki4309
    @denjiuzumaki4309 4 месяца назад

    nice examples!!

  • @oliverkobing4726
    @oliverkobing4726 3 года назад

    What a great teacher!

  • @aminedoughmi8582
    @aminedoughmi8582 3 года назад

    excellent course ! keep up the good work

  • @guliyevshahriyar
    @guliyevshahriyar Год назад

    Thank you very much teacher.

  • @hubercats
    @hubercats 3 года назад

    Thanks for sharing this very clear, helpful video!

  • @jackm.1628
    @jackm.1628 3 года назад

    Fantastic explanations, thank you professor!

  • @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

  • @ii7mdj_353
    @ii7mdj_353 11 месяцев назад

    Yoooooooooooooooooooo, that makes so much sense now.

  • @mutalibgozalov7208
    @mutalibgozalov7208 8 месяцев назад

    Thank U sir, that was helpful

  • @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 ?

  • @echoptic775
    @echoptic775 3 года назад +1

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

  • @spacewad8745
    @spacewad8745 2 года назад

    23:00 sizeof is an operator

  • @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

  • @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

  • @firedragonmangaming2410
    @firedragonmangaming2410 2 года назад

    Wait im confused. Does malloc return a void pointer ?

  • @arslanarslan9432
    @arslanarslan9432 2 года назад

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

  • @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 !!