Become a Malloc() Pro

Поделиться
HTML-код
  • Опубликовано: 25 янв 2025

Комментарии • 51

  • @bayroot7385
    @bayroot7385 4 дня назад

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

  • @duarteribeiro1520
    @duarteribeiro1520 Месяц назад +6

    2:48 freaky

  • @sharokhkeshawarz2122
    @sharokhkeshawarz2122 Месяц назад +3

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

  • @mbn-code
    @mbn-code 2 месяца назад +10

    Insanely good examples and very easy to understand.

  • @empathy_monster
    @empathy_monster 2 месяца назад +42

    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));

    • @thedoubleeguy
      @thedoubleeguy  2 месяца назад +17

      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!

    • @ilikebananas342
      @ilikebananas342 2 месяца назад +13

      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++

    • @thedoubleeguy
      @thedoubleeguy  2 месяца назад +2

      @@ilikebananas342 thanks for the feedback!

    • @Brad_Script
      @Brad_Script Месяц назад +7

      ​@@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.

    • @santitabnavascues8673
      @santitabnavascues8673 Месяц назад

      ​@@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

  • @john9francis
    @john9francis Месяц назад +1

    Perfect video. Learned a ton here.

  • @pound9799
    @pound9799 2 месяца назад +8

    This video is so underrated

  • @yashraj3005
    @yashraj3005 2 месяца назад +2

    You explained so well !

  • @modellatore
    @modellatore 2 месяца назад +4

    Nice!

  • @chrisgeorgiou8680
    @chrisgeorgiou8680 Месяц назад +1

    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?

  • @breachbase
    @breachbase 2 месяца назад +2

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

    • @thedoubleeguy
      @thedoubleeguy  2 месяца назад

      Yes it is, the library is fantastic

  • @xj77ziad
    @xj77ziad 2 месяца назад +4

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

    • @thedoubleeguy
      @thedoubleeguy  2 месяца назад

      of course, how would you like to receive it?

    • @xj77ziad
      @xj77ziad 2 месяца назад

      @thedoubleeguy anyway ig
      Even a youtube conment would be good

    • @luv8365
      @luv8365 2 месяца назад +1

      @@thedoubleeguy jus comment it

    • @thedoubleeguy
      @thedoubleeguy  2 месяца назад

      @@luv8365 great suggestion, all the code is commented on this video

  • @mrglick5050
    @mrglick5050 Месяц назад

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

  • @explqicot3293
    @explqicot3293 2 месяца назад +1

    Do a video on arena pool

  • @alxbudn
    @alxbudn 2 месяца назад +2

    you dont need to cast malloc in c

  • @tinkertaps
    @tinkertaps 2 месяца назад +1

    Bro what software do you use make the animations

    • @thedoubleeguy
      @thedoubleeguy  2 месяца назад +1

      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.

  • @Montazeran8
    @Montazeran8 Месяц назад

    ❤❤❤

  • @miguevr7852
    @miguevr7852 27 дней назад

    Just change ps1 next time so that no blur si needed

  • @brotherpeter00
    @brotherpeter00 2 месяца назад +2

    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.

  • @SergLapin
    @SergLapin Месяц назад +1

    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.

    • @Miles-co5xm
      @Miles-co5xm Месяц назад

      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
      @Miles-co5xm Месяц назад

      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

    • @SergLapin
      @SergLapin Месяц назад

      ​@@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.

    • @Miles-co5xm
      @Miles-co5xm Месяц назад

      @@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.

    • @SergLapin
      @SergLapin Месяц назад

      @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.