Dynamic 2d array allocation and deallocation in C

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

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

  • @loaf_iii9558
    @loaf_iii9558 3 года назад +28

    Underrated video, you should be top search for c programming. It makes so much more sense to me now

  • @daisytran4813
    @daisytran4813 2 года назад +15

    To be honest, this is the most comprehensible explaination on youtube, thank you so much !

  • @alpharussel653
    @alpharussel653 3 года назад +27

    Just like professor Feynman said if you understand something well enough you’ll be able to explain it in the most simplest way.and that’s exactly you’ve done. Thank you for making this difficult topic understandable!!!

    • @grimvian
      @grimvian 6 месяцев назад +1

      Exactly and we all remember the teachers who did and did not!
      The best teaching videos in programming do not have disturbing background noises e.g. music and only have relevant information on the screen.

  • @joncyprus1304
    @joncyprus1304 Год назад +2

    Dude is a GOAT. As a grad student taking a CS class (non cs major) this has made it extremely clear how to allocate dynamic memory. Thanks!

  • @jamane4855
    @jamane4855 Год назад +1

    Wow ! Great explanation, i'm french and your english is very understandable ! Thanks a lot !

  • @somethingeasy4996
    @somethingeasy4996 2 года назад +6

    I went in with no expectation, I finished with complete understanding of what I should be doing, and also a slightly better idea on pointers/array. Thanks a bunch, appreciate your work greatly.

  • @ochiengkodongo83
    @ochiengkodongo83 Год назад +1

    This has been the most easy -to-understand tutorial ever since. Thanks a lot.
    Just a little something : on function free2dIntArray() we shouldn't include the third parameter dim2 on the function's prototype.

    • @jatinthebox8574
      @jatinthebox8574  Год назад

      Thanks for your kind words and yes you are correct.

  • @Antialles069
    @Antialles069 Год назад +2

    I know I'm a bit late to the party but goddammit that explanation is so good. pleace don't stop doings vids like this you've made a difference in someone's life

  • @pedrobispo8400
    @pedrobispo8400 7 месяцев назад +3

    perfect. the only one that explained clearly so far.

  • @zemikel91
    @zemikel91 3 года назад +4

    Finally some good explanation about 2D dynami array! Piece of gem! Thank you!

  • @mikeorioles
    @mikeorioles 2 года назад +2

    Great video to help shake out the cobwebs. My favorite on RUclips for the topic.
    Thank you!

  • @Ian-bb7vv
    @Ian-bb7vv Год назад +3

    thank you for the effort creating this wonderful video

  • @sivaramakrishnajuvanaboina9635
    @sivaramakrishnajuvanaboina9635 Год назад +2

    Very good explanation sir it's a superb video. Everyone can understand this concept without any doubt.

  • @ebennkansahantwi5827
    @ebennkansahantwi5827 Год назад +3

    please you are amazing , thnks a lot and you have made my difficulties become simple for me 💯🙏🙏

  • @abdelhadi72
    @abdelhadi72 Год назад +1

    man thank you so much this video I find it the best one on RUclips ♥

  • @liadp5117
    @liadp5117 3 года назад +4

    Very well explained , I'm glad I found your channel !

  • @inbluegirl
    @inbluegirl 6 месяцев назад +1

    You are a good teacher, thank you!
    I was struggling with this all day

  • @ArpanChakraborty-do6yz
    @ArpanChakraborty-do6yz 8 месяцев назад +1

    thank you man, you save me from my upcoming test, thanks a lot

  • @yogeshchauhan9401
    @yogeshchauhan9401 Год назад +2

    Very informative 🔥

  • @raptoress6131
    @raptoress6131 Год назад +1

    Thank you, this was an approachable explanation that helped me finally understand the concepts.

  • @fathihamdi2630
    @fathihamdi2630 2 года назад +2

    best explanation , keep it going !

  • @DrunkyDude97
    @DrunkyDude97 2 года назад +3

    Best video on this topic !! Thanks alot.

  • @ian6273
    @ian6273 Год назад +1

    Amazing explanation way more clear than my college professors

  • @mansbjork5721
    @mansbjork5721 Год назад +1

    exactly what I needed :) I'm working on a small project that solves a system of user input equations and this is the core of it

  • @danieldomin3956
    @danieldomin3956 Год назад +1

    Nice explanation! However there's an optimized way to allocate a 2d array
    int **p = (int **)malloc(rows * sizeof(int *));
    p[0] = (int *)malloc(rows* columns * sizeof(int));
    for (i = 1; i < rows; i++) {
    p[i] = p[i - 1] + columns;
    }
    free(p[0]);
    free(p);
    In this case, a single block of memory is allocated and rearrange the pointers to form an array. This is computationally better for a few reasons:
    1. The allocation engine does not perform as many allocations as before.
    2. As opposed to the conventional implementation, there's no extra bytes between rows to keep count of the memory blocks. That means the final 2d array is represented in a continuous memory block. Therefore, there's no need to make large moves between those positions in the memory.
    3. free() is called twice only. The conventional implementation depends on the 'rows' size.

  • @typehinter
    @typehinter 2 года назад +1

    I feel like I don't need to know this for the problem set I'm doing, but this was very helpful regardless. Thank you!

  • @udarue
    @udarue Год назад +2

    Excellent explanation

  • @potatobot2802
    @potatobot2802 4 месяца назад +1

    What an explanation! Thanks a ton man.

  • @loveafinni
    @loveafinni Год назад +1

    This is a life saver. Thank you!

  • @tassneemaltaf2001
    @tassneemaltaf2001 11 месяцев назад +1

    Thank you so much for the explanation!!

  • @ridoychandradey8337
    @ridoychandradey8337 2 года назад +1

    Thank you very much for this wonderful content.

  • @samarlabidi2863
    @samarlabidi2863 3 года назад +2

    amazing explanation!! Thanks alot really

  • @taha7shaikh
    @taha7shaikh Год назад +1

    Thank you so much man! This was great.

  • @braian1619
    @braian1619 9 месяцев назад +1

    TE AMO GRACIAS !!!

  • @rohanghoshdastidar1047
    @rohanghoshdastidar1047 2 года назад +3

    B E A U T I F U L EXPLANATION

  • @zen.ali238
    @zen.ali238 2 года назад +2

    Can you please make complete knowledge of c/cpp, You are way of teaching so good and you really get us into deep which really i want !!
    Thanks for this video 🤠

    • @jatinthebox8574
      @jatinthebox8574  2 года назад +1

      Thanks for your kind words Zuned. I'll try and upload some other material soon.

    • @zen.ali238
      @zen.ali238 2 года назад +2

      @@jatinthebox8574 willwait for more.

  • @captainarvoy2551
    @captainarvoy2551 2 года назад +2

    Clearly understood! Thank you sir.🙇

  • @brunorenostro
    @brunorenostro 2 года назад +1

    this video is so good!Thanks!

  • @jecodedoncjesuis875
    @jecodedoncjesuis875 2 года назад +2

    You're the best

  • @1337-rw6xh
    @1337-rw6xh Год назад +1

    perfect video

  • @prakhargupta2960
    @prakhargupta2960 3 года назад +2

    This was a great video!

    • @jatinthebox8574
      @jatinthebox8574  3 года назад

      Thanks for taking the trouble to comment - appreciated.

  • @Nick-mv7hv
    @Nick-mv7hv 3 года назад +3

    great video

  • @JB-iz8bi
    @JB-iz8bi 2 года назад +2

    why is (int**) debatable? is there something wrong with using it? thanks!

    • @jatinthebox8574
      @jatinthebox8574  2 года назад

      You just do not need it, but I think it makes the code more readable. I think it reminds people of Java, which they do not like, but I do 🙂

  • @SpectreXPlays
    @SpectreXPlays 2 года назад +1

    thanks a lot that was quite helpful.

  • @headcrabn5347
    @headcrabn5347 2 года назад +1

    you're a fuckin lifesaver, man

  • @cj-nr5ni
    @cj-nr5ni 2 года назад +1

    This was gem

  • @unwarysage05w32
    @unwarysage05w32 3 года назад +2

    I hate c keeps telling there is a leak and when i try to free that memory thats leaking i got an invalid free

  • @mongraal2272
    @mongraal2272 3 года назад +2

    thanks bro!

  • @luigisolaro1782
    @luigisolaro1782 2 года назад +1

    I finally understood why it works like that thanks to you :)
    Just got a question, in the freeing part (the correct one) wouldn't it be the same if I didn't pass dim2 as a parameter, since it's not needed for deallocating the memory?

  • @abdoosama8337
    @abdoosama8337 2 года назад +1

    thank you sir!

  • @gelfetogebre8301
    @gelfetogebre8301 2 года назад +1

    i love this

  • @bradleywellington9807
    @bradleywellington9807 2 года назад +1

    thank you sir.

  • @tomaszstanislawski457
    @tomaszstanislawski457 2 года назад +1

    Actually the true dynamic 2d array can be allocated with a pointer to array. Just do `int (*arr)[dim2] = calloc(dim1, sizeof *arr);`. And that's all. Just remember to call `free(arr)` at the end.

    • @jatinthebox8574
      @jatinthebox8574  2 года назад

      That's a really neat way of doing it. Do a video to explain how it works ? ;-)

    • @jaykoranga4859
      @jaykoranga4859 Год назад

      @@jatinthebox8574 sure sir

  • @CastleBomber
    @CastleBomber Год назад +1

    this is the one

  • @krizh289
    @krizh289 2 года назад +1

    legend

  • @mongraal2272
    @mongraal2272 2 года назад +1

    Can you please make a video about dynamic allocation for array of strings please

    • @jatinthebox8574
      @jatinthebox8574  2 года назад +1

      Thanks for commenting. If you are using fixed size strings you can simply substitute with throughout this video and it will work in the same way. In this case dim1 would be the number of strings and dim2 the maximum string length, including the end of string sentinel. If you want a jagged array (strings of different lengths), you’d need to do things a bit differently. Is the latter what you had in mind?

  • @1337-rw6xh
    @1337-rw6xh Год назад +1

    why did u stop posting ? 😥

  • @SpectreXPlays
    @SpectreXPlays 2 года назад +1

    make more videos please

  • @wrosen03
    @wrosen03 2 года назад

    Strictly speaking isn't this a lookup table not a true contiguously allocated 2d array?

    • @jatinthebox8574
      @jatinthebox8574  2 года назад

      As you say, it is not contiguously allocated 2d array: each row will be allocated to separate locations on the heap. Also, I get what you mean by lookup table: the "column array" of pointers to integers are used to "look up" each row. I wouldn't personally call it a lookup table, though. For me they (lookup tables) are a higher order construct used for a smaller set of specific applications.

  • @abhishekpandey71
    @abhishekpandey71 2 года назад

    It would have been nicer if you give meaningful names to parameters like instead of Dim1 Dim2 you can could have written Row and col, in video you are mentioning them as row and col why not name them the same.