Memory Leaks And How To Prevent Them | C Programming Tutorial

Поделиться
HTML-код
  • Опубликовано: 11 дек 2024

Комментарии • 21

  • @metmay
    @metmay Год назад +7

    this is one of the best, if not the best tutorials to explain memory leaks, thank you so much for the great content

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

      You're very welcome, I'm really glad you enjoyed the tutorial! :-)

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

    Thanks for the great explanation on memory leaks! Going to fix my code now. I'm managing a small amount of data but I need to build good habit patterns.

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

    You have such a deep knowledge of C. Thank you very very interesting.

  • @dopetag
    @dopetag 11 месяцев назад +1

    Good explanation and examples

  • @ramakrishna4092
    @ramakrishna4092 Год назад +2

    Hi sir
    I have one doubt how does free function know how much memory should be deallocate in c programming language and how to free the memory without using free function
    Can you pls do a video on this topic's it will helpful sir..
    Thank you...

    • @PortfolioCourses
      @PortfolioCourses  Год назад +2

      Regarding how does free know how much memory to deallocate, typically when malloc/etc are used to allocate memory a bit of extra data is used to specify the size of the memory allocated, and free() can use this to free the memory. See: stackoverflow.com/questions/1518711/how-does-free-know-how-much-to-free. In terms of freeing memory without using free, we could actually use realloc() to set the block of memory to have a new size of 0. :-) If we're trying to implement functions like free/malloc/etc that is much more involved, for example: www.sunshine2k.de/articles/coding/cmemalloc/cmemory.html.

  • @mb59621
    @mb59621 15 дней назад

    I actually have a question. How big is your memory that you have installed on this machine you are working?
    4*10^12 bytes allocated for 34 iterations means 136*10^12 bytes total consumed by this process , which in computing terms if you divide by 2^40 , you would get a little less than 124 terabytes .
    It shows 112 TiB in virtual memory, but it should be not any less than 123 TiB . Regardless, do such big storage disks even exist ? And what happened to the other things you might have in your storage disk?

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

    I have a question does memory leak still happen when we close that program?

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

      No, once the program finishes executing any memory is returned “to the operating system” essentially. :-)

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

    I noted that the allocate function didn't free the memory after it was over, but when memory was allocated in main function it was cleared after the program was done running.
    So does this mean that when a program is terminated entirely that all the memory it allocated is automatically cleared?
    I'm wondering, if when I work on a large program that will inevitably will crash from time to time as I test it, if I might be left with memory left allocated on the heap.
    This happened to me a month or so ago when I was messing with Opengl in Python and didn't use the proper command to clear it's buffers and a circle was left on my screen and my sound stopped working until I rebooted lol.

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

      When a program is done running all the memory it was allocated is going to be "released", which means it can be used for other purposes/programs. There are technically some really specialized systems where this is not the case, but this is how it works in modern operating systems (Windows, Linux, MacOS, etc.). Whether it's cleared or not by setting all values to 0, I think would ultimately depend on the OS. And I don't think an OS will spend the time to "zero the memory" either as that would take time to do it in all cases. superuser.com/a/621384

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

      @@PortfolioCourses Thanks!

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

    Thank you

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

    Thank you

  • @Tu10248
    @Tu10248 7 месяцев назад

    👏👏

    • @Tu10248
      @Tu10248 7 месяцев назад

      do{
      data = malloc(data_size);
      iterations++;
      } while(data != NULL);