How to manage memory with malloc, calloc, realloc, and free in C

preview_player
Показать описание
Learn about basic memory management in C using the common malloc, calloc, realloc, and free functions.

Hope you enjoyed the video!

Check out this code here:

Join my Discord server to chat with me:

Check out some code on my GitHub:

Tweet me something funny on Twitter:

Say hi over at Facebook:

Sincerely,
Engineer Man
Рекомендации по теме
Комментарии
Автор

Thank you for getting to the point quickly while still being understandable.

ktri
Автор

I love that you comment in functions with the #include, wish all tutorials had this, I am also going to make it part of my practice!

JoelGarcia-mljx
Автор

so direct. Took my prof a one hour lecture to say exactly this. Thanks man.

kylesava
Автор

I had to watch this at 0.25 speed, but you explained everything in a way my professor wasn't able to. Thanks!

kurtrowland
Автор

Nice video. Clear and straight to the point introduction to memory allocation.
All of the other videos I've seen skip over details and try to throw to much info at once.
I'd like to see the next level. Dive in more on when to use calloc and malloc. More info pros and cons and industry standards.
How to get good at memory Management, any tips and tricks. Talk about some of the reasons people hate it.

If you already have a video for this, let me know

leadinmarketing
Автор

calloc is also good for safety since everything is zero'd(guarantees no garbage).

mavhunter
Автор

Short, to the point, and well-explained. Awesome.

RAndrewNeal
Автор

jesus, ive managed to get a 1.9/8 on my dynamic memory allocation quiz with studying for days and watching a bunch of hour long indian tutorials and still not understanding it. then i have to revisit the topic and i find this video, 5 minutes long...all the information and explaination i need. simply the best lol thanks again ur my savior

kylecondren
Автор

I think I've read somewhere than it's even a better practice to use the name of the variable for the sizeof... something like char *str = malloc(13*sizeof *str)... In this kind of line probably it won't matter, but if you call malloc in a different place that where you've defined the variable it won't matter if you for some reason decide to change the data type of the variable.

Almost forgot... great channel by the way.

lafdez
Автор

Best explanation of malloc ever come across in life!

ShamsuddeenAbdullahi-jy
Автор

Thanks! Super simple examples with great explanations.

tjdrifts
Автор

You did a name = realloc(name, ...). If name is the same pointer than free(name) works great, but if the 'name' pointer returned is a different pointer than the original name passed in will this be a leak or does realloc free(name) if it can't expand the memory allocated?

TreverShick
Автор

Free does de-allocate but there's no guarantee it's going to return anything to the OS. Most likely it's not going to return it to the OS. Pretty much standard behavior is to put the memory in the heap. So just to be clear, when you ask for memory via malloc, new, etc the call checks the heap first so it doesn't have to waste time asking the OS for memory. If there's enough memory there then the program will just get the memory from the heap. If not then the allocation function will request a chunk of memory (probably 4k) which will go into the heap and then get needed memory from the heap. Then when you're done with the memory and call delete/free the memory will go back into the heap. (Admittedly sometimes in debug builds the call can release the memory back to the OS but that's kind of slow.)

ddauria
Автор

Love the to the point videos. Engineer man for Engineering problems, and if you need a language brush-up, Derek Banas is very similar. They are the are the places to go!

matt-g-recovers
Автор

I learned something in my quest to understand pointers.
“malloc creates a pointer…”

jackgerberuae
Автор

Some miss. calloc allocate blocks while malloc allocate one block. So choose one couple: {a[n]<=>calloc} or {*(a+n)<->malloc}. Maybe you did not understand but anyway.

hacerdemirel
Автор

It would be a good practice cast the returned value of these functions with the type of the pointer, like:
char *name = (char *)malloc(13 * sizeof(char));

ricardo_oli
Автор

Thanks, very good explanation!

I just wish you would have lowered the monitor's resolution. I am assessing your video from a cell phone and it's quite hard to followup with the code on the screen. You could also try to magnify the desktop. I mean, if it makes sense to you. I would assume many users would use a smart phone, but I could be wrong.

guiller
Автор

I have a question for realloc.
In the case of your code, does the memory contents of the pointer ‘name’ - which is “engineer man” guaranteed to be preserved after the realloc?
Regardless of address change of the pointer ‘name’?
In other words, does realloc memcpy the previous contents if the returned pointer changes?
Thanks a lot for such an informative video!

TonyKimBest
Автор

I saw this pretty often in when using x265 encoder. It says "malloc of size" then the program shut down.

squarehole
visit shbcf.ru