Dynamic 2d array allocation and deallocation in C

Dynamic 2d array allocation and deallocation in C

Jat in the box

3 года назад

19,383 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@udarue
@udarue - 06.12.2023 21:45

Excellent explanation

Ответить
@1337-rw6xh
@1337-rw6xh - 19.11.2023 09:49

perfect video

Ответить
@1337-rw6xh
@1337-rw6xh - 19.11.2023 09:39

why did u stop posting ? 😥

Ответить
@joncyprus1304
@joncyprus1304 - 13.11.2023 19:51

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!

Ответить
@yogeshchauhan9401
@yogeshchauhan9401 - 09.11.2023 17:32

Very informative 🔥

Ответить
@Antialles069
@Antialles069 - 06.11.2023 03:01

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

Ответить
@CastleBomber
@CastleBomber - 28.06.2023 10:34

this is the one

Ответить
@danieldomin3956
@danieldomin3956 - 28.06.2023 04:18

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 - 14.06.2023 01:08

This is a life saver. Thank you!

Ответить
@mansbjork5721
@mansbjork5721 - 11.06.2023 16:40

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

Ответить
@jamane4855
@jamane4855 - 25.05.2023 20:03

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

Ответить
@ian6273
@ian6273 - 30.04.2023 23:35

Amazing explanation way more clear than my college professors

Ответить
@ochiengkodongo83
@ochiengkodongo83 - 26.04.2023 00:14

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.

Ответить
@raptoress6131
@raptoress6131 - 13.04.2023 15:52

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

Ответить
@sivaramakrishnajuvanaboina9635
@sivaramakrishnajuvanaboina9635 - 06.04.2023 18:16

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

Ответить
@ebennkansahantwi5827
@ebennkansahantwi5827 - 17.03.2023 18:33

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

Ответить
@Ian-bb7vv
@Ian-bb7vv - 20.02.2023 16:01

thank you for the effort creating this wonderful video

Ответить
@rohanghoshdastidar1047
@rohanghoshdastidar1047 - 28.01.2023 08:59

B E A U T I F U L EXPLANATION

Ответить
@fathihamdi2630
@fathihamdi2630 - 11.01.2023 20:32

best explanation , keep it going !

Ответить
@JB-iz8bi
@JB-iz8bi - 16.12.2022 14:44

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

Ответить
@atharvapawar9970
@atharvapawar9970 - 19.11.2022 17:37

make more videos please

Ответить
@atharvapawar9970
@atharvapawar9970 - 19.11.2022 17:37

thanks a lot that was quite helpful.

Ответить
@typehint
@typehint - 01.11.2022 02:10

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!

Ответить
@krizh289
@krizh289 - 28.10.2022 23:52

legend

Ответить
@zen.ali238
@zen.ali238 - 25.10.2022 21:12

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 🤠

Ответить
@brunorenostro
@brunorenostro - 24.10.2022 01:06

this video is so good!Thanks!

Ответить
@headcrabn5347
@headcrabn5347 - 03.10.2022 01:17

you're a fuckin lifesaver, man

Ответить
@gelfetogebre8301
@gelfetogebre8301 - 22.09.2022 16:39

i love this

Ответить
@abhishekpandey71
@abhishekpandey71 - 12.09.2022 12:43

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.

Ответить
@abdoosama8337
@abdoosama8337 - 11.09.2022 14:08

thank you sir!

Ответить
@somethingeasy4996
@somethingeasy4996 - 16.08.2022 11:46

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.

Ответить
@cj-nr5ni
@cj-nr5ni - 20.07.2022 23:04

This was gem

Ответить
@captainarvoy2551
@captainarvoy2551 - 13.07.2022 04:23

Clearly understood! Thank you sir.🙇

Ответить
@DrunkyDude97
@DrunkyDude97 - 04.07.2022 18:11

Best video on this topic !! Thanks alot.

Ответить
@ridoychandradey8337
@ridoychandradey8337 - 02.07.2022 05:53

Thank you very much for this wonderful content.

Ответить
@bradleywellington9807
@bradleywellington9807 - 19.06.2022 22:48

thank you sir.

Ответить
@tomaszstanislawski457
@tomaszstanislawski457 - 01.06.2022 17:29

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.

Ответить
@wrosen03
@wrosen03 - 16.04.2022 07:55

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

Ответить
@jecodedoncjesuis875
@jecodedoncjesuis875 - 11.02.2022 18:11

You're the best

Ответить
@mongraal2272
@mongraal2272 - 09.02.2022 20:57

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

Ответить
@luigisolaro1782
@luigisolaro1782 - 08.02.2022 01:02

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?

Ответить
@samarlabidi2863
@samarlabidi2863 - 04.02.2022 00:13

amazing explanation!! Thanks alot really

Ответить
@zemikel91
@zemikel91 - 31.01.2022 21:42

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

Ответить
@mongraal2272
@mongraal2272 - 30.01.2022 01:11

thanks bro!

Ответить
@liadp5117
@liadp5117 - 18.12.2021 21:09

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

Ответить
@alpharussel653
@alpharussel653 - 07.12.2021 14:51

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

Ответить
@Nick-mv7hv
@Nick-mv7hv - 21.11.2021 06:20

great video

Ответить