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));
You are correct, in C this is often considered redundant given the fact that a void* is automatically and safely promoted to any other pointer type without a cast. The reason I casted it in this case was to be a little more explicit to programmers who aren't as familiar with C, or programmers who will use this type of syntax in C++ (although in C++ malloc() is not that common). The coding standard at some companies vary and in some cases you may be required to use this syntax. Thank you for the feedback!
This is not "redundant", this is wrong. One should never cast the result of any *alloc function in C. Casting supresses compiler diagnostic (hides warnings). In C++ you have to do this, but C++ is entirely different language. You're probably doing something wrong if you're using void pointers and libc allocators in C++
@@ilikebananas342 casting malloc in C was probably a thing in older versions, I'm pretty sure they did that in the K&R books and people kept doing it because of it. Casting void pointer to anything shouldn't give you any warning, that's literally the whole point of void pointer, you can't do anything with it without casting.
@@ilikebananas342 you're just allocating memory, but not calling any constructors with any *alloc call in C++, the same as releasing memory with free, it doesn't call any destructor. That's the purpose of the new/delete operators, which, in their simplest implementations just do that, allocate memory and call the constructor upon that space, or call the destructor of an object prior to deallocating its space
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?
You would never call any of these functions directly. Most are going to be wrapped in a user-defined function and most will involve looking at syllabic memory allocation on top of some kind of best fit algorithm.
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.
Most of your points don't work in real life. Memcpy ing large stack variables is costier than passing ptr around. Malloc isn't a syscall, and malloc is cheap but free is costly.
@@Miles-co5xm ohh yes, it is a syscall. And who said about mem copy? The main problem is that you WILL lose track of pointers. Sooner or later. Unless you always pass the resources down to be used and avoid getting memory from southbound. As you say in real life it is not working, yes, because everybody says "ohh, I will simply do this". Booom, use after free, you are welcome.
@@SergLapin When you pass a stack variable instead of ptr to function, It will most likely trigger memcpy. AND PLEASE CHECK FOR YOURSELF MALLOC IS NOT A SYSCALL. Also ptrs exist in first place to go southbound, you cannot send your local stack references southbound, And that is why languages like rust have come to existence forsolving memory issues. before you tell someone malloc is a syscall, please check it for your own knowledge.
@Miles-co5xm passing pointer to a variable on the stack will not copy the value in the arguments. And malloc uses futex/mutex which is a syscall, check for your own knowledge.
Dude you're the best, your videos are easy to watch, captivating and examples are very depictive, thank you!
2:48 freaky
Bloody hell, I understood the alloc's in 6 min while I was struggling for months in my CS class ;-; love you
Insanely good examples and very easy to understand.
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));
You are correct, in C this is often considered redundant given the fact that a void* is automatically and safely promoted to any other pointer type without a cast. The reason I casted it in this case was to be a little more explicit to programmers who aren't as familiar with C, or programmers who will use this type of syntax in C++ (although in C++ malloc() is not that common). The coding standard at some companies vary and in some cases you may be required to use this syntax. Thank you for the feedback!
This is not "redundant", this is wrong. One should never cast the result of any *alloc function in C. Casting supresses compiler diagnostic (hides warnings). In C++ you have to do this, but C++ is entirely different language. You're probably doing something wrong if you're using void pointers and libc allocators in C++
@@ilikebananas342 thanks for the feedback!
@@ilikebananas342 casting malloc in C was probably a thing in older versions, I'm pretty sure they did that in the K&R books and people kept doing it because of it. Casting void pointer to anything shouldn't give you any warning, that's literally the whole point of void pointer, you can't do anything with it without casting.
@@ilikebananas342 you're just allocating memory, but not calling any constructors with any *alloc call in C++, the same as releasing memory with free, it doesn't call any destructor. That's the purpose of the new/delete operators, which, in their simplest implementations just do that, allocate memory and call the constructor upon that space, or call the destructor of an object prior to deallocating its space
Perfect video. Learned a ton here.
This video is so underrated
You explained so well !
Nice!
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?
This is really good. Is this 3b1b's anim library?
Yes it is, the library is fantastic
Could you please provide the source code for the last example?
of course, how would you like to receive it?
@thedoubleeguy anyway ig
Even a youtube conment would be good
@@thedoubleeguy jus comment it
@@luv8365 great suggestion, all the code is commented on this video
Huh, I always subconsciously read 'calloc' as 'clear alloc' since it set the allocated space to zero (and thus 'cleared' it).
Do a video on arena pool
you dont need to cast malloc in c
Bro what software do you use make the animations
I use a python library called manim, I'll probably make a video tutorial on how I do some of the animations in the future.
❤❤❤
Just change ps1 next time so that no blur si needed
You would never call any of these functions directly. Most are going to be wrapped in a user-defined function and most will involve looking at syllabic memory allocation on top of some kind of best fit algorithm.
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.
Most of your points don't work in real life.
Memcpy ing large stack variables is costier than passing ptr around.
Malloc isn't a syscall, and malloc is cheap but free is costly.
Also there is mostly no point of mallocing if I am not moving ptr around the program, for locals I will use a reference to stack variable
@@Miles-co5xm ohh yes, it is a syscall. And who said about mem copy? The main problem is that you WILL lose track of pointers. Sooner or later. Unless you always pass the resources down to be used and avoid getting memory from southbound. As you say in real life it is not working, yes, because everybody says "ohh, I will simply do this". Booom, use after free, you are welcome.
@@SergLapin When you pass a stack variable instead of ptr to function, It will most likely trigger memcpy. AND PLEASE CHECK FOR YOURSELF MALLOC IS NOT A SYSCALL. Also ptrs exist in first place to go southbound, you cannot send your local stack references southbound, And that is why languages like rust have come to existence forsolving memory issues. before you tell someone malloc is a syscall, please check it for your own knowledge.
@Miles-co5xm passing pointer to a variable on the stack will not copy the value in the arguments. And malloc uses futex/mutex which is a syscall, check for your own knowledge.