Dynamic Memory Allocation in C - malloc, free, and buffer overflows

preview_player
Показать описание
When writing programs in the C programming language there are many occasions when you need to use dynamic memory allocation, to ask the OS for some memory. In C you do this with malloc() and you return it with free(). However C doesn't check what you do with the memory, which is why there is a risk of buffer overflows or buffer overruns.




#garyexplains
Рекомендации по теме
Комментарии
Автор

For so long I've struggled to understand malloc and free (at no fault to my professor, I sadly suffered a case of I-want-to-skip-class-today), but within the first few minutes you provided a clear and helpful description. Thank you so much!

IdiotGoblin
Автор

Sir, You are doing a very noble task here.
I do not know of any Educational Institution that has an Instructor, who delivers information like you do.
Keep up the good work and May GOD bless You!!
- Subhashis

subhashisghose
Автор

I am really enjoying your programming videos, they are very in depth. Please keep doing more of these. Thank you

Djzaamir
Автор

Interesting way of explaining the concept.
Thanks.

Chitranshleo
Автор

Nice.. video.. I had always thought why C is preferred for low memory allocation applications.. now I got it..

sheikhhafijulali
Автор

Nice and helpful garysplaining. Thanks!

h.m.
Автор

Seriously u need to teach in a university!

vinayaknavada
Автор

The error infer is catching at 9:34 is a different one than the one you point out. Infer is complaining because you didn't check if the pointer returned by malloc is maybe NULL because allocation failed. If you wrap line 21 in an if (ptr) {} block, it should complain about the use after free.

louiscloete
Автор

Thank you Gary well explained, thank you for providing this for free

GowthamRam
Автор

Essential knowledge for those ones who want to understand how memory works, what is memory allocation and what garbage collectors in other languages do.

smthsmth
Автор

Thanks Gary, Suberb explanation 👌 Please make more on C videos.

venkataravieluri
Автор

Thanks you Sir 👍, this is very handful

dmitxd
Автор

both infer and valgrind seem very helpful thanks for showcasing them...

spandanghosh_
Автор

Had to implement malloc in my systems course

disterben
Автор

In the malloc3.c example, I think the infer error is saying that the return value of malloc() is not checked for NULL before passing it to fill_buffer(). It does not seem to be catching that the pointer being passed to printf() references freed memory.

You can verify this by adding a NULL check to the code and see if the infer error goes away.

danielyoffie
Автор

*GARY!!!*
Good Afternoon Professor!*
Good Afternoon Fellow Classmates!*
*CLASS IS IN SESSION!*

MarkKeller
Автор

Nice.... Well explained although wsl terminal font seems a bit too large but ig would look good on mobile screens...

spandanghosh_
Автор

I tend to just allocate my own blocks and write my own GC manager on top....I've done that since before C++ was even a thing, back in the days of c64 asm. I'm used to it.

sabriath
Автор

When I tried it on my old Ubuntu 16.04.6 LTS (with gcc version 5.5.0-12ubuntu1), running the "free twice" program resulted in a core dump. Error message had the problem stated: "double free or corruption". The "use after free" example was different. It printed ""fill_buffer returned: fill_buffer returned:" (When this second was executed with valgrind, the printf-ed output was the same as in the video.)

silurust
Автор

When you free() you’re just marking memory space on the heap as deallocated. The memory still belongs to your process.

volzr