Dynamic 2d array allocation and deallocation in C

Sdílet
Vložit
  • čas přidán 20. 10. 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

Komentáře • 74

  • @pedrobispo8400
    @pedrobispo8400 Před měsícem +3

    perfect. the only one that explained clearly so far.

  • @loaf_iii9558
    @loaf_iii9558 Před 2 lety +25

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

  • @inbluegirl
    @inbluegirl Před 5 dny +1

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

  • @daisytran4813
    @daisytran4813 Před rokem +14

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

  • @alpharussel653
    @alpharussel653 Před 2 lety +25

    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 Před 7 dny

      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 Před 8 měsíci +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 Před rokem +1

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

  • @ArpanChakraborty-do6yz
    @ArpanChakraborty-do6yz Před 2 měsíci +1

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

  • @Ian-bb7vv
    @Ian-bb7vv Před rokem +3

    thank you for the effort creating this wonderful video

  • @somethingeasy4996
    @somethingeasy4996 Před rokem +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.

  • @zemikel91
    @zemikel91 Před 2 lety +4

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

  • @mikedoeren5960
    @mikedoeren5960 Před rokem +2

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

  • @ebennkansahantwi5827
    @ebennkansahantwi5827 Před rokem +3

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

  • @liadp5117
    @liadp5117 Před 2 lety +4

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

  • @DrunkyDude97
    @DrunkyDude97 Před 2 lety +3

    Best video on this topic !! Thanks alot.

  • @ochiengkodongo83
    @ochiengkodongo83 Před rokem +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.

  • @Antialles069
    @Antialles069 Před 8 měsíci +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

  • @abdelhadi72
    @abdelhadi72 Před rokem +1

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

  • @raptoress6131
    @raptoress6131 Před rokem +1

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

  • @sivaramakrishnajuvanaboina9635

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

  • @danieldomin3956
    @danieldomin3956 Před rokem +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 Před rokem +1

    This is a life saver. Thank you!

  • @tassneemaltaf2001
    @tassneemaltaf2001 Před 5 měsíci +1

    Thank you so much for the explanation!!

  • @ian6273
    @ian6273 Před rokem +1

    Amazing explanation way more clear than my college professors

  • @typehint
    @typehint Před rokem +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 Před 7 měsíci +2

    Excellent explanation

  • @yogeshchauhan9401
    @yogeshchauhan9401 Před 8 měsíci +2

    Very informative 🔥

  • @fathihamdi2630
    @fathihamdi2630 Před rokem +2

    best explanation , keep it going !

  • @mansbjork5721
    @mansbjork5721 Před rokem +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 Před 7 měsíci +1

    Thank you so much man! This was great.

  • @ridoychandradey8337
    @ridoychandradey8337 Před 2 lety +1

    Thank you very much for this wonderful content.

  • @captainarvoy2551
    @captainarvoy2551 Před 2 lety +2

    Clearly understood! Thank you sir.🙇

  • @brunorenostro
    @brunorenostro Před rokem +1

    this video is so good!Thanks!

  • @samarlabidi2863
    @samarlabidi2863 Před 2 lety +2

    amazing explanation!! Thanks alot really

  • @braian1619
    @braian1619 Před 3 měsíci +1

    TE AMO GRACIAS !!!

  • @jecodedoncjesuis875
    @jecodedoncjesuis875 Před 2 lety +2

    You're the best

  • @SpectreXPlays
    @SpectreXPlays Před rokem +1

    thanks a lot that was quite helpful.

  • @abdoosama8337
    @abdoosama8337 Před rokem +1

    thank you sir!

  • @rohanghoshdastidar1047
    @rohanghoshdastidar1047 Před rokem +2

    B E A U T I F U L EXPLANATION

  • @cj-nr5ni
    @cj-nr5ni Před 2 lety +1

    This was gem

  • @mongraal2272
    @mongraal2272 Před 2 lety +2

    thanks bro!

  • @prakhargupta2960
    @prakhargupta2960 Před 3 lety +2

    This was a great video!

    • @jatinthebox8574
      @jatinthebox8574  Před 3 lety

      Thanks for taking the trouble to comment - appreciated.

  • @bradleywellington9807
    @bradleywellington9807 Před 2 lety +1

    thank you sir.

  • @Nick-mv7hv
    @Nick-mv7hv Před 2 lety +3

    great video

  • @1337-rw6xh
    @1337-rw6xh Před 8 měsíci +1

    perfect video

  • @headcrabn5347
    @headcrabn5347 Před rokem +1

    you're a fuckin lifesaver, man

  • @gelfetogebre8301
    @gelfetogebre8301 Před rokem +1

    i love this

  • @zen.ali238
    @zen.ali238 Před rokem +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  Před rokem +1

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

    • @zen.ali238
      @zen.ali238 Před rokem +2

      @@jatinthebox8574 willwait for more.

  • @unwarysage05w32
    @unwarysage05w32 Před 2 lety +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

  • @krizh289
    @krizh289 Před rokem +1

    legend

  • @CastleBomber
    @CastleBomber Před rokem +1

    this is the one

  • @luigisolaro1782
    @luigisolaro1782 Před 2 lety +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?

  • @JB-iz8bi
    @JB-iz8bi Před rokem +2

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

    • @jatinthebox8574
      @jatinthebox8574  Před rokem

      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 🙂

  • @mongraal2272
    @mongraal2272 Před 2 lety +1

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

    • @jatinthebox8574
      @jatinthebox8574  Před 2 lety +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?

  • @tomaszstanislawski457
    @tomaszstanislawski457 Před 2 lety +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  Před 2 lety

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

    • @jaykoranga4859
      @jaykoranga4859 Před rokem

      @@jatinthebox8574 sure sir

  • @SpectreXPlays
    @SpectreXPlays Před rokem +1

    make more videos please

  • @1337-rw6xh
    @1337-rw6xh Před 8 měsíci +1

    why did u stop posting ? 😥

  • @wrosen03
    @wrosen03 Před 2 lety

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

    • @jatinthebox8574
      @jatinthebox8574  Před 2 lety

      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 Před rokem

    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.