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.
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...
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.
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?
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.
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
this is one of the best, if not the best tutorials to explain memory leaks, thank you so much for the great content
You're very welcome, I'm really glad you enjoyed the tutorial! :-)
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.
You’re welcome Neil! :-D
You have such a deep knowledge of C. Thank you very very interesting.
You’re very welcome! :-D
Good explanation and examples
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...
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.
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?
I have a question does memory leak still happen when we close that program?
No, once the program finishes executing any memory is returned “to the operating system” essentially. :-)
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.
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
@@PortfolioCourses Thanks!
Thank you
You're welcome! :-D
Thank you
You're welcome! :-D
👏👏
do{
data = malloc(data_size);
iterations++;
} while(data != NULL);