Become a Malloc() Pro

preview_player
Показать описание
#dynamicmemoryallocation #malloc In this video, I go over 3 situations where I would use malloc(), calloc(), and realloc(). I also briefly explain the importance of free(). *IMPORTANT NOTE* In modern C and according to much of the current C Coding standards, malloc() is not casted because the void pointer is automatically promoted to any other pointer type without that cast. This is a required action in C++, therefore I left it in this code for a new learner. However, keep in mind that this practice is looked down on in C due to suppressing compiler diagnostics and readability issues.
00:00 Dynamic Memory Allocation Intro
00:23 Explanation
01:07 malloc()
03:39 calloc()
05:28 realloc()
06:23 Recap/Conclusion
Рекомендации по теме
Комментарии
Автор

Dude you're the best, your videos are easy to watch, captivating and examples are very depictive, thank you!

bayroot
Автор

Bloody hell, I understood the alloc's in 6 min while I was struggling for months in my CS class ;-; love you <3

sharokhkeshawarz
Автор

Insanely good examples and very easy to understand.

mbn-code
Автор

You shouldn't cast malloc, realloc, etc., to a pointer. You lose type information. MISRA C guidelines warns against this. The compiler will automatically handle the cast for you. Ex: int *a = malloc(sizeof(int));

empathy_monster
Автор

Didn't know that calloc() also initializes the memory block to zero, I thought it just returned a pointer aligned to a multiple of the items' size, eg for int's (4 bytes) it will be a multiple of 4, and I consider this its most important feature actually. Btw, do you know whether floating point (double) items should best be 8-byte aligned? They are not integers, they contain sub-parts, and the processor will likely access them byte by byte, but on the other hand double operations are carried out by the processor (its FPU) so maybe the whole variable can be transferred in a single read or write step. But then there's the CPU's cache... So do you know if double variables should be 8-byte aligned, ie is there any performance benefit? We are talking about marginal differences of course, probably not even measurable, but theoretically, should we care?

chrisgeorgiou
Автор

Huh, I always subconsciously read 'calloc' as 'clear alloc' since it set the allocated space to zero (and thus 'cleared' it).

mrglick
Автор

This is really good. Is this 3b1b's anim library?

breachbase
Автор

Could you please provide the source code for the last example?

xjziad
Автор

Just change ps1 next time so that no blur si needed

miguevr
Автор

Bro what software do you use make the animations

tinkertaps
Автор

Rule#1 don't use dynamic memory. Especially for short living buffers, among everything else it is very expensive syscall operation. Stack memory access is free. Rule#2 if you have to use it, allocate memory in one place, and release in the same place, never ever take allocated memory from southbound calls, because you will have to release it, and nobody knows who else may release it in some large codebase. Rule#3, never use void type pointers, you WILL lose track of data size at some point.

SergLapin
visit shbcf.ru