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

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

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

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

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

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

  • @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 :) ruclips.net/video/XR6ofUpjfB4/видео.html

  • @sallaklamhayyen9876
    @sallaklamhayyen9876 2 года назад +2

    great video Mike😍

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

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

  • @flowrinrecordsofficial8412
    @flowrinrecordsofficial8412 2 года назад +1

    Life saver! Tysm 😍

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

      You are most welcome!

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

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

  • @wassisoult5800
    @wassisoult5800 Год назад +1

    stack overflow ( our favorite website 🤣🤣🤣🤣 )

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

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

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