Good practice for freeing memory in C

  Рет қаралды 13,113

CodeVault

CodeVault

4 жыл бұрын

Check out our Discord server: / discord

Пікірлер: 31
@fusca14tube
@fusca14tube 2 жыл бұрын
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 2 жыл бұрын
That's definitely a nice solution
@sidekick3rida
@sidekick3rida 5 ай бұрын
Why the do/while?
@doodocina
@doodocina 2 ай бұрын
@@sidekick3rida absolutely no point. do {} while(0) used in asserts in case u want to break from assert statement
@OrcsBR
@OrcsBR 2 жыл бұрын
Man, great videos and explanation!!! You have hit the right balance with them. Thanks
@ysl8104
@ysl8104 Жыл бұрын
I really like your videos! Thanks so much for the explanation!!
@LDdrums20
@LDdrums20 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
@alvarobrandao6910
@alvarobrandao6910 Жыл бұрын
thankyou thats gold content!
@eva42sh
@eva42sh 2 жыл бұрын
love you man thank you so much
@willofirony
@willofirony Жыл бұрын
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.
@zaabimahdi
@zaabimahdi 4 жыл бұрын
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 4 жыл бұрын
Thank you! And alright, we'll release a video next week about double pointers.
@wizardatmath
@wizardatmath 8 ай бұрын
Sweet. Thanks.
@brennethd5391
@brennethd5391 2 жыл бұрын
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.
@wlcrutch
@wlcrutch 2 ай бұрын
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 2 ай бұрын
That's also a good way of managing responsibility
@damouze
@damouze 7 күн бұрын
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.
@nimasamadi1312
@nimasamadi1312 2 жыл бұрын
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 2 жыл бұрын
Yes, you can no longer set that arr pointer (defined in main) to NULL if you only pass a simple pointer
@NikolaNevenov86
@NikolaNevenov86 Жыл бұрын
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 Жыл бұрын
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
@magicredragon2541
@magicredragon2541 8 ай бұрын
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 6 ай бұрын
Yes, that would be a good solution
@togosakutaro5882
@togosakutaro5882 2 жыл бұрын
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 Жыл бұрын
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 2 жыл бұрын
When you write in the function *arr = NULL; in the main arr is now contained in the stack,isn’t it?
@CodeVault
@CodeVault 2 жыл бұрын
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 2 жыл бұрын
@@CodeVault Thank you👍
@dakunskye
@dakunskye Жыл бұрын
Why doesn't the compiler check this?
@CodeVault
@CodeVault Жыл бұрын
The C compiler (and language) is very simple. It gives you more control but you have to be more careful
Memory leaks and how to prevent them
10:49
CodeVault
Рет қаралды 24 М.
Dynamic Memory Allocation | C Programming Tutorial
31:51
Portfolio Courses
Рет қаралды 80 М.
Inside Out Babies (Inside Out Animation)
00:21
FASH
Рет қаралды 23 МЛН
1,000 Diamonds! (Funny Minecraft Animation) #shorts #cartoon
00:31
toonz CRAFT
Рет қаралды 40 МЛН
Amazing weight loss transformation !! 😱😱
00:24
Tibo InShape
Рет қаралды 65 МЛН
Master Pointers in C:  10X Your C Coding!
14:12
Dave's Garage
Рет қаралды 293 М.
Why Function Pointers are Awesome
11:11
Jacob Beningo
Рет қаралды 6 М.
you will never ask about pointers again after watching this video
8:03
Low Level Learning
Рет қаралды 2,1 МЛН
Dynamic Memory with Malloc - Everything you Need to Know
13:51
Caleb Curry
Рет қаралды 8 М.
Dynamic memory allocation in C - malloc calloc realloc free
17:35
mycodeschool
Рет қаралды 880 М.
why do void* pointers even exist?
8:17
Low Level Learning
Рет қаралды 344 М.
Dynamic 2d array allocation and deallocation in C
9:09
Jat in the box
Рет қаралды 19 М.
Running "Hello World!" in 10 FORBIDDEN Programming Languages
18:07
Be Careful When Using scanf() in C
12:22
NeuralNine
Рет қаралды 127 М.