Dynamic 2d array allocation and deallocation in C

Поделиться
HTML-код
  • Опубликовано: 20 окт 2020
  • Code explanations and diagrams illustrating the use of pointers, malloc and free to allocate and free memory for a two dimensional array on the heap

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

  • @pedrobispo8400
    @pedrobispo8400 4 дня назад +2

    perfect. the only one that explained clearly so far.

  • @alpharussel653
    @alpharussel653 2 года назад +24

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

  • @loaf_iii9558
    @loaf_iii9558 2 года назад +23

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

  • @daisytran4813
    @daisytran4813 Год назад +13

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

  • @ArpanChakraborty-do6yz
    @ArpanChakraborty-do6yz Месяц назад +1

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

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

    thank you for the effort creating this wonderful video

  • @joncyprus1304
    @joncyprus1304 6 месяцев назад +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!

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

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

  • @somethingeasy4996
    @somethingeasy4996 Год назад +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.

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

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

  • @udarue
    @udarue 6 месяцев назад +2

    Excellent explanation

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

    Best video on this topic !! Thanks alot.

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

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

  • @yogeshchauhan9401
    @yogeshchauhan9401 7 месяцев назад +2

    Very informative 🔥

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

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

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

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

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

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

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

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

    Thank you so much for the explanation!!

  • @danieldomin3956
    @danieldomin3956 11 месяцев назад +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.

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

    This is a life saver. Thank you!

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

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

  • @Antialles069
    @Antialles069 7 месяцев назад +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

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

    Amazing explanation way more clear than my college professors

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

    best explanation , keep it going !

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

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

  • @typehint
    @typehint Год назад +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!

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

  • @taha7shaikh
    @taha7shaikh 5 месяцев назад +1

    Thank you so much man! This was great.

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

    amazing explanation!! Thanks alot really

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

    Clearly understood! Thank you sir.🙇

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

    Thank you very much for this wonderful content.

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

    this video is so good!Thanks!

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

    You're the best

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

    thanks bro!

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

    great video

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

    This was gem

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

    TE AMO GRACIAS !!!

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

    B E A U T I F U L EXPLANATION

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

    This was a great video!

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

      Thanks for taking the trouble to comment - appreciated.

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

    thanks a lot that was quite helpful.

  • @1337-rw6xh
    @1337-rw6xh 6 месяцев назад +1

    perfect video

  • @zen.ali238
    @zen.ali238 Год назад +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  Год назад +1

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

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

      @@jatinthebox8574 willwait for more.

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

    thank you sir!

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

    you're a fuckin lifesaver, man

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

    thank you sir.

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

    i love this

  • @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?

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

    legend

  • @unwarysage05w32
    @unwarysage05w32 2 года назад +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

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

    this is the one

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

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

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

      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 Год назад +1

    make more videos please

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

  • @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 6 месяцев назад +1

    why did u stop posting ? 😥

  • @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 Год назад

    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.