Thank you! This feels similar to the idea of dependency injection. Very helpful!
@NotMarkKnopfler3 жыл бұрын
In addition to being a good lesson on malloc/free and the concept of memory leaks, it's also a useful discussion of factoring.
@miguelangelmartinezcasado89353 жыл бұрын
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!
@naboulsikhalid77636 ай бұрын
as usual rich and well explained content, thank you
@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 Жыл бұрын
Very clear and precise video, thank you so much !
@xcellsior33552 жыл бұрын
appreciate the video. If it isn't too much issue, please consider making tutorials with a dark-theme on the IDE. It would appreciated!
@CodeVault2 жыл бұрын
Check out the latest videos, I already am using a dark theme.
@davidliverman47422 жыл бұрын
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ц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.
@theNeuo132 жыл бұрын
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?
@CodeVault2 жыл бұрын
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.).
@theNeuo132 жыл бұрын
@@CodeVault thank you for the reply. I will check it out.
@ゆきだるま-h6n4 жыл бұрын
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?
@CodeVault4 жыл бұрын
It's the same as free(arr). Nothing different at all.
@benjaminyde2 жыл бұрын
Very nicely explained!
@yt-sh Жыл бұрын
thanks for the tips!
@nellynelson9652 жыл бұрын
Hi. Thanks. Ive no clue how to code, I just wanted to know what a memory leak was and this tought me something. Thanks.
@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 Жыл бұрын
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)
@michael1789 Жыл бұрын
you the goat
@finndemoncat93797 ай бұрын
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.
@miriz_0p8853 жыл бұрын
excellent video!!
@Misc_useful2 жыл бұрын
Super bro thankyou
@domcxz86433 жыл бұрын
Thanks :)
@amosnimos2 жыл бұрын
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.
@sumanthjoglekar1722 жыл бұрын
He s totla
@Jojo_clowning2 жыл бұрын
Ngl, the hard accent made hard to follow, but i understood enough to continue searching.