Good practice for freeing memory in C

Sdílet
Vložit
  • čas přidán 29. 08. 2024
  • Check out our Discord server: / discord

Komentáře • 32

  • @fusca14tube
    @fusca14tube Před 2 lety +22

    Hi... to avoid dangling pointers, you could define a macro like "#define FREE(ptr) do { free(ptr); ptr = NULL; } while (0)" or "#define FREE(ptr) ({ free(ptr); ptr = NULL; })" and call FREE() instead of free(). Thanks.

    • @CodeVault
      @CodeVault  Před 2 lety +5

      That's definitely a nice solution

    • @sidekick3rida
      @sidekick3rida Před 6 měsíci +4

      Why the do/while?

    • @doodocina
      @doodocina Před 3 měsíci

      @@sidekick3rida absolutely no point. do {} while(0) used in asserts in case u want to break from assert statement

  • @wlcrutch
    @wlcrutch Před 3 měsíci +3

    Wouldn’t it just be a good idea to manage responsibility better? As in, don’t have a function that processes the array free the memory, but rather, you allocated in main function-let main function be responsible for destruction as well.

    • @CodeVault
      @CodeVault  Před 2 měsíci +2

      That's also a good way of managing responsibility

    • @damouze
      @damouze Před měsícem +1

      Or write constructors and destructors for the memory blocks those pointers point towards. For instance if you have a struct X { int y, int z };, declare a constructor for it that allocates and initalizes the memory which returns a pointer to the newly allocated memory block on success or NULL on failure and also declare a destructor that handles freeing the memory and returns NULL on succes or the original pointer on failure.

  • @LDdrums20
    @LDdrums20 Před 2 lety +2

    I've been struggling with this recently. That's a really smart way to find issues with double free. I wonder why I haven't thought about it

  • @willofirony
    @willofirony Před rokem +3

    It is possible that the second attempt to free a none nulled pointer you won't get an exception and that is WORSE! This is because the first freeing action was successful and in the meantime a second allocation assigns the SAME address to a totally different use. So, the second attempt to free the original blob of memory successfully frees the second blob, without error. The program happily continues until that second blob is accessed and good luck in debugging that exception. So, always NULL a pointer to freed memory, at least until C/C++ gets an erase operator for native types.

  • @scottfranco1962
    @scottfranco1962 Před 9 dny

    I don't do this, but it is because I prefer to be warned when there is a double free in the code. My personal preference is to create wrappers for each data element, and include double free checking in that, since C does not have it. Other languages do.

  • @OrcsBR
    @OrcsBR Před 2 lety

    Man, great videos and explanation!!! You have hit the right balance with them. Thanks

  • @alvarobrandao6910
    @alvarobrandao6910 Před rokem +1

    thankyou thats gold content!

  • @brennethd5391
    @brennethd5391 Před 2 lety

    thanks man, our school teacher only told us that setting a pointer to NULL after freeing is a good practise but not why, so i thought it was just waste of performance.

  • @ysl8104
    @ysl8104 Před rokem

    I really like your videos! Thanks so much for the explanation!!

  • @NikolaNevenov86
    @NikolaNevenov86 Před rokem +1

    Ha....recently I've been doing dynamic array and I kept getting my memory overwritten by something. In the end I was told to use realloc and that fixed the issue. Looking back now...when I was resizing the the array I freed but never set the old pointer to NULL. I assumed free() auto invalidates the pointer. Thanks for this video

    • @CodeVault
      @CodeVault  Před rokem +2

      A similar situation happened to me with a 3D engine I was building. The byproducts of that were very strange and difficult to track down, lost hours of work debugging that. Hence why I mentioned the realloc assignment in the video

  • @eva42sh
    @eva42sh Před 2 lety

    love you man thank you so much

  • @zaabimahdi
    @zaabimahdi Před 4 lety

    Heyy !! i really like your videos so much ! and hope all the best for your channel !
    Please , could make a tutorial about the double pointer (the trick that you showed about **arr and how we passed an array into it ... )
    Thank you !!!

    • @CodeVault
      @CodeVault  Před 4 lety +1

      Thank you! And alright, we'll release a video next week about double pointers.

  • @wizardatmath
    @wizardatmath Před 9 měsíci

    Sweet. Thanks.

  • @nimasamadi1312
    @nimasamadi1312 Před 2 lety

    Thank you for the great explantation of double freeing the same pointer. But, I was thinking that you could just write the process_arr function as below to release the memory allocated on heap in the main:
    void test(int* ptr){
    free(ptr);
    }
    And there is no need to send pointer to the arr pointer into the process_arr function. Because the only thing free() method needs is a pointer to the first place of the allocated memory on the heap. However, it is not possible to set the pointer to NULL as we only have a copy of the arr in the function. Am I right?

    • @CodeVault
      @CodeVault  Před 2 lety +2

      Yes, you can no longer set that arr pointer (defined in main) to NULL if you only pass a simple pointer

  • @togosakutaro5882
    @togosakutaro5882 Před 2 lety

    Sergiu. Does free() set the ptr to NULL? How is it that it does nothing if the ptr is NULL? Is it maybe because a NULL ptr is already freed?

    • @CodeVault
      @CodeVault  Před 2 lety +1

      free() doesn't set anything:
      int* m = malloc(sizeof(int)); // m has the value 156, being the address where that int is stored
      free(m);
      // m will still be 156 here
      And regarding the NULL aspect. It's just for convenience's sake. An address will never be 0 (0 is the value NULL btw) so it simply ignores it gracefully than bothering the programmers with an error (there might be other historical reasons I don't know about too)

  • @jeffbezos3942
    @jeffbezos3942 Před 2 lety

    When you write in the function *arr = NULL; in the main arr is now contained in the stack,isn’t it?

    • @CodeVault
      @CodeVault  Před 2 lety

      arr is ALWAYS contained in the stack of the main function. *arr (the address at which arr is pointing towards) was dynamically allocated

    • @jeffbezos3942
      @jeffbezos3942 Před 2 lety

      @@CodeVault Thank you👍

  • @magicredragon2541
    @magicredragon2541 Před 9 měsíci

    i have one question ,in terms to not get double free it wouldnt be better to do every time we free this:
    if(arr!=NULL){
    free(*arr) ;
    arr=NULL;
    }
    ?

    • @CodeVault
      @CodeVault  Před 7 měsíci

      Yes, that would be a good solution

  • @dakunskye
    @dakunskye Před rokem

    Why doesn't the compiler check this?

    • @CodeVault
      @CodeVault  Před rokem

      The C compiler (and language) is very simple. It gives you more control but you have to be more careful