Memory leaks and how to prevent them

  Рет қаралды 25,400

CodeVault

CodeVault

Күн бұрын

Пікірлер: 29
@jawwad4020
@jawwad4020 Ай бұрын
Thank you! This feels similar to the idea of dependency injection. Very helpful!
@NotMarkKnopfler
@NotMarkKnopfler 3 жыл бұрын
In addition to being a good lesson on malloc/free and the concept of memory leaks, it's also a useful discussion of factoring.
@miguelangelmartinezcasado8935
@miguelangelmartinezcasado8935 2 жыл бұрын
Men, thanks, I was having problems remembering how to use char arrays correctly, even though I did study on the university, i wasn't able to remember it. Your video helped me, thanks!
@naboulsikhalid7763
@naboulsikhalid7763 3 ай бұрын
as usual rich and well explained content, thank you
@grimvian
@grimvian Жыл бұрын
Again, a great video! I appreciate that you are taking the viewer inside the way of dealing with issues and the benefits of different approaches. I really like that you are using different IDEs. For now, I'm using CodeBlocks because I can't 'live' without a debugger and don't want to be assimilated into the MS universe... But DEVC is rather old, from 2015, but what the heck, hardcore C coders use C89, so... Now I have almost learned the core concepts of C. I was about to write about making a copy of a pointer to the allocated memory, but you covered it at the end of the video. Earlier, when experimenting with memory, my PC runs much smoother after a reboot. :o)
@gian6725
@gian6725 10 ай бұрын
Very clear and precise video, thank you so much !
@xcellsior3355
@xcellsior3355 2 жыл бұрын
appreciate the video. If it isn't too much issue, please consider making tutorials with a dark-theme on the IDE. It would appreciated!
@CodeVault
@CodeVault 2 жыл бұрын
Check out the latest videos, I already am using a dark theme.
@davidliverman4742
@davidliverman4742 Жыл бұрын
Always remember that if your program is short lived do not worry about it. Also alloc is a friend in a pinch. If you code is long lived, please be extra careful with calloc. I never use malloc. Rock on!
@ОлегТокмачев-в9ц
@ОлегТокмачев-в9ц 4 жыл бұрын
Thanks you for excellent video. I am new to C programming language and I was wondering what is more practical way to allocate memory and not to forget to free it and I found an answer in your video.
@TheKhanj
@TheKhanj Жыл бұрын
Considering all you've told, this question comes to my mind that when is actually needed to call malloc then? Is there a scenario that it is needed to allocate memory from the heap when it can be allocated from the stack in the caller function? Of course one of the reasons might be the need for large amount of memory, but is there any other reason?
@CodeVault
@CodeVault Жыл бұрын
You are right, a lot of the memory we usually need can be allocated on the stack. But there are some cases in which you can't avoid dynamically allocated memory. First, I guess, we should look at why we call memory allocated on the stack "static" and memory allocated on the heap "dynamic". The difference is two fold: 1) The memory on the heap is dynamic in the sense that you always control when you allocate and deallocate that memory. You are the one calling malloc and free. With memory on the stack, it's always allocated to you whenever you're calling the function and deallocated whenever you're returning from a function 2) The memory on the heap is dynamic in terms of size. How much dynamic memory you allocate during a function's execution is up to you and can only be known at run-time. Whereas the amount of memory allocated for a function's stack is already known at compile-time and **cannot** be changed at runtime A linked list is a good example. It's a standalone object to which you can add an arbitrary number of nodes, have to be linked with each other and be accessible at all times until deletion. Due to it having an arbitrary number of nodes, you can't use stack memory since its size cannot change at runtime. Pre-allocating a huge number of nodes on the stack could be an answer but, as you said, there are (quite strict) limits with the amount of stack memory you can use and it is very inefficient. So, here, dynamically allocated memory is a better choice. Another even simpler example is an array to which we can add any number of elements at runtime. This is impossible using just stack memory (at some point you will run out of space in a statically allocated array)
@yt-sh
@yt-sh 9 ай бұрын
thanks for the tips!
@theNeuo13
@theNeuo13 2 жыл бұрын
thank you very for the clear explanation. it was very useful. My question is what is the right way to pass and return map or vector to avoid memory leaks?
@CodeVault
@CodeVault 2 жыл бұрын
There is this video talking about this exact topic: code-vault.net/lesson/wb70kjprpx:1603820088877 Basically pass a reference to the array as a parameter instead of returning a pointer. That way you can also define the array however you want (static allocation, dynamic allocation etc.).
@theNeuo13
@theNeuo13 2 жыл бұрын
@@CodeVault thank you for the reply. I will check it out.
@benjaminyde
@benjaminyde Жыл бұрын
Very nicely explained!
@nellynelson965
@nellynelson965 Жыл бұрын
Hi. Thanks. Ive no clue how to code, I just wanted to know what a memory leak was and this tought me something. Thanks.
@finndemoncat9379
@finndemoncat9379 3 ай бұрын
The example on the video was the exact problem I was facing and how I solved it 🥹 Basically I had a function returning a pointer to another pointer wich already had memory allocated to it. The program would crash at random points because of it. Now I just use strdup on the string and use the void function to modify it. It would be best if the debugger would point out my stupid mistake. I hope it was the issue, I still have to test the program when I am done rewriting it. Update: the issue was exactly that, now it runs without issues.
@ゆきだるま-h6n
@ゆきだるま-h6n 3 жыл бұрын
10:25 for this what happen when you free(arr_copy) instead free(arr)? i was wondering cuz arr_copy is also the poiter of the heap memory right?
@CodeVault
@CodeVault 3 жыл бұрын
It's the same as free(arr). Nothing different at all.
@michael1789
@michael1789 10 ай бұрын
you the goat
@miriz_0p885
@miriz_0p885 3 жыл бұрын
excellent video!!
@amosnimos
@amosnimos 2 жыл бұрын
I wont press the like button since the likes are at 404 which is funny, but I liked the content, great video thanks for making it.
@Misc_useful
@Misc_useful 2 жыл бұрын
Super bro thankyou
@domcxz8643
@domcxz8643 3 жыл бұрын
Thanks :)
@sumanthjoglekar172
@sumanthjoglekar172 2 жыл бұрын
He s totla
@Jojo_clowning
@Jojo_clowning 2 жыл бұрын
Ngl, the hard accent made hard to follow, but i understood enough to continue searching.
Detecting memory leaks in Visual Studio
6:34
CodeVault
Рет қаралды 64 М.
When to free memory in C
13:45
CodeVault
Рет қаралды 10 М.
Osman Kalyoncu Sonu Üzücü Saddest Videos Dream Engine 269 #shorts
00:26
Help Me Celebrate! 😍🙏
00:35
Alan Chikin Chow
Рет қаралды 89 МЛН
兔子姐姐最终逃走了吗?#小丑#兔子警官#家庭
00:58
小蚂蚁和小宇宙
Рет қаралды 9 МЛН
Бенчик, пора купаться! 🛁 #бенчик #арти #симбочка
00:34
Симбочка Пимпочка
Рет қаралды 3,6 МЛН
Memory Leaks And How To Prevent Them | C Programming Tutorial
15:15
Portfolio Courses
Рет қаралды 9 М.
Are Lists Evil? Creator Of C++ | Prime Reacts
14:30
ThePrimeTime
Рет қаралды 102 М.
The Downsides Of C++ | Prime Reacts
21:23
ThePrimeTime
Рет қаралды 141 М.
Memory leak in C/C++
17:55
mycodeschool
Рет қаралды 244 М.
What are void pointers in C?
10:05
CodeVault
Рет қаралды 30 М.
find memory errors quickly. (-fsanitize, addresssanitizer)
9:44
Jacob Sorber
Рет қаралды 18 М.
How to properly deal with dynamically allocated memory
13:44
CodeVault
Рет қаралды 9 М.
Good practice for freeing memory in C
8:17
CodeVault
Рет қаралды 14 М.
C Dynamic Memory Debugging with Valgrind
17:51
Brian Fraser
Рет қаралды 134 М.
Detecting Memory Leaks With Valgrind
11:12
Walter Schilling
Рет қаралды 37 М.
Osman Kalyoncu Sonu Üzücü Saddest Videos Dream Engine 269 #shorts
00:26