Your Third Day in C (Understanding stack and heap memory) - Crash Course in C Programming

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

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

  • @armandotadeudelcol9306
    @armandotadeudelcol9306 ปีที่แล้ว +12

    Hi Mike, i'm from Brazil and in my 7 years as a software developer, studying by different resources (courses, documentations, books...) you and your channel are the best resource to study that i found. Thank you!

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

      Cheers, thank you for the kind words!

  • @OmniscientOCE
    @OmniscientOCE 2 ปีที่แล้ว +10

    This is a really high quality series. Thank you Mike.

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

      You're most welcome!

  • @mozanapon
    @mozanapon 3 หลายเดือนก่อน +1

    Hi bro! You are really good at teaching, I´ve seen some videos related to memory in C and thanks to yours I finally got it! I will continue learning with your videos!

    • @MikeShah
      @MikeShah  3 หลายเดือนก่อน

      Thank you for the kind words! Enjoy the series!

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

    Hi Mike and thanks for your precious job. I’m learning a lot from your lessons with a clarity I haven’t found anywhere else. I appreciate it very much. A question: why don’t you do in-depth courses on other platforms like Udemy or on your site?

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

      Thank you for the kind words! I will add more courses on courses.mshah.io over time 🙂

  • @uvideo100
    @uvideo100 3 หลายเดือนก่อน +1

    Hey Mike, Just a typo in your drawing at 2:57. You mentioned storage space for type integer is 4 bytes, but you drew a box of 5 bytes. It is a pretty obvious typo, but could confuse some new learners.
    I am trying to get from intermediate to expert at C, and find your videos very useful.
    By the way, does integer type always require 4 bytes of storage, or is it compiler dependent?. Do some compilers allocate 8 bytes to store an integer? Thanks!

    • @MikeShah
      @MikeShah  3 หลายเดือนก่อน

      Good catch! Generally speaking, it is compiler dependent on the size of the 'int' type. There exist 'fixed-width types' that may be better for portability that otherwise are consistent -- you can learn about them here :) th-cam.com/video/XR6ofUpjfB4/w-d-xo.html

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

    thank you so much bro, this is gold

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

      Cheers, glad that this was helpful!

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

    Very neatly explained. Yes, Stackoverflow is second fav now after this channel :). I'm loving C lang watching your videos. It's great to understand and see things from system admins perspective. Thank You.

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

      Thank you for the kind words! :)

  • @aghiadalzein3069
    @aghiadalzein3069 11 หลายเดือนก่อน +1

    This Video is great.
    I feel a lot of accumulated information which I've got over years are organizing in my brain in the most clear way .
    Thanks a lot

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

      Cheers!

  • @rudolfbouzek6944
    @rudolfbouzek6944 8 หลายเดือนก่อน +1

    Question: When I use "malloc", will the data be contiguous in memory or scattered around wherever there is space available?

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

      To us as the programmer, it is contiguous. In reality, a 'physical address' could be scattered, but our operating system will make it appear as if the addresses are contiguous.

    • @rudolfbouzek6944
      @rudolfbouzek6944 8 หลายเดือนก่อน +1

      @@MikeShah Thank you, so on microcontrollers where there is no OS, this could be a problem (fragmentation and inability to find space).

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

      Indeed could be a problem for space. That said, the memory returned even on embedded from malloc will always be contiguous or otherwise return null(if memory cannot be allocated). ​@@rudolfbouzek6944

  • @QWin-ir6yq
    @QWin-ir6yq 5 หลายเดือนก่อน +1

    Great video. I learned C++ first, now backfilling concept gaps with C. Your C and C++ have videos have been critical in my development. I will continue to promote you but let me know of additional ways I can support you. Also, I see you are also doing videos on D. I’m not familiar with that language. Is that the next logical evolution from c or c++?

    • @MikeShah
      @MikeShah  5 หลายเดือนก่อน

      Cheers, thanks for the kind words! Feel free to share as you like! D is indeed the next evolution in the C-family of languages. I've found it allows me to think in different paradigms (specifically functionally, concurrent programming, and generic programming) more efficiently. Neat thing about D is it also has a C compiler built-in, so all the knowledge you otherwise learn here applies :)

  • @slavapavlov9473
    @slavapavlov9473 2 ปีที่แล้ว +4

    Hey Mike, thank you for the lesson.
    It was a good call to *printf* the stack and heap addresses inside and outside of the scope of the *allocateOnHeap*
    Sorry for a silly question but what happens if we don't *free(heapData)* in our main function?
    Does it mean the memory stays allocated until we reboot our system?
    Or the effect is less drastic and the memory stays allocated until our program is closed?

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

      The operating system will eventually reclaim the memory once the process terminates. So where this truly becomes a problem is if you have long running programs (e.g. a web browser, a server, a game), that is slowly (or quickly) leaking memory over time. :)

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

      @@MikeShah Thank you for clarifying!

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

    I think well understanding of pointers and memory allocation is crucial ( Key ) to be good at Data structures ?

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

      Correct, at least in C!

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

    Hey thanks a lot for a great series! It would be cool to see video about memory arenas style if programming in c, and how you can basically create stacks for yourself dynamically, or at least its how I understood it)

    • @MikeShah
      @MikeShah  4 หลายเดือนก่อน +1

      Cheers! That's a nice idea -- I have some materials on this for C++, so I can consider a video like this in the future

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

    great video Mike😍

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

    stack overflow ( our favorite website 🤣🤣🤣🤣 )

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

    So what about global variables? Are they just stack allocated and cleared when the process ends? Instead of being cleared when leaving a specific scope I mean.

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

      global variables are baked into the binary file (i.e. object file or 'executable object file' that you double-click). They're always present.

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

      @@MikeShah ah, so when the process is run they are stored with the source code?

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

      @@Rocketmang Stored in the executable, so the address is loaded when the program starts, correct.

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

      @@MikeShah Ah ok. Cool! Thanks for clearing that up for me! Loving this series so far!

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

      @@RocketmangCheers!

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

    Life saver! Tysm 😍

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

      You are most welcome!

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

    Hi,
    good material, Thank You.
    I have also question about free() function. How free knows how many bytes should be released ? It is only a pointer to the first element of block, it does not contain any information about block size.

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

      It's actually allocating a few more bytes in a header block to keep track of this :)

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

    Thanks for the video, very helpful, but why we don't use free() inside the function? Is it because we can't return the pointer of array.

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

      Cheers! Can you provide a timestamp?

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

      25:17@@MikeShah

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

      int* allocateOnHeap(int x){
      int* array = (int*)malloc(sizeof(int) * 50000);
      free(array); // Like this
      return array;
      }@@MikeShah

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

      @@specialx9856 Ah, so in this example I'm demonstrating that heap allocated memory (the chunk of memory from malloc) is stored in the address of a pointer. The point is that we need to have a valid pointer to that same address (whether in the function, or in another pointer which is what is returned from the function -- effectively another copy of the original pointer that pointed to a chunk of memory). So as long as we are free'ing a chunk of memory once, we can do it within the function or outside. Generally speaking, we do have to think carefully about how we manage our memory.

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

      Thanks, now I understand. @@MikeShah

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

    13:57 I glitched for a sec here. I was like what is going on?
    How can we use the function *bar()* before declaring it?
    But since our program actually starts with the main() function, everything's fine.

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

      It appears the compiler was smart enough to figure this out, but I probably should at least provide the declaration of bar() before foo().

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

    13:31 You bet!

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

      Hehe :)