How to use dynamically allocated arrays

แชร์
ฝัง
  • เผยแพร่เมื่อ 16 ก.ย. 2024

ความคิดเห็น • 66

  • @cdoggy007
    @cdoggy007 4 ปีที่แล้ว +62

    You're videos deserve MANY more views. I never comment on videos, but this was incredibly helpful and by far the clearest explanation I have come across. Thank you for this!!

    • @AvikNayak_
      @AvikNayak_ 3 ปีที่แล้ว

      @Colt Anson It's cool but do you know how to invert a binary tree?

  • @ethnicsovereignty2369
    @ethnicsovereignty2369 3 ปีที่แล้ว +7

    Wonderful work, no plugging in your programs, straight to the point and well explained

  • @Anotherandoman93
    @Anotherandoman93 2 ปีที่แล้ว +2

    You gifted bastard thank you so much! I've been trying to understand an exercise for an hour or so, no idea we could do this.

  • @Erik-tp3ht
    @Erik-tp3ht 4 ปีที่แล้ว +15

    Very good explanation. It was quick and to the point. Glad I clicked on this video!

  • @xx_theartfuldodger_xx1105
    @xx_theartfuldodger_xx1105 3 ปีที่แล้ว +6

    What's great about your explanation you explain WHY you do it this way. When I understand the reasoning behind what I am doing I retain it so much better. Thank you!

  • @thesportsdrink
    @thesportsdrink 3 ปีที่แล้ว +13

    By far the clearest and most helpful C tutorial I've come across. I could count on one hand how many comments I've left on TH-cam, but I had to leave one here because of how informative it was.

  • @road2grandchamp238
    @road2grandchamp238 ปีที่แล้ว +1

    Best explanation of this concept I have seen on the internet, it's simple yet in depth and in 11minutes I learned with what I was struggling on for hours

  • @unpatientes1562
    @unpatientes1562 ปีที่แล้ว +1

    i watched this video in my bed sleepy and still understood everything, you good at explaining man. thanks

  • @spoofer9113
    @spoofer9113 3 ปีที่แล้ว +2

    This channel is growing to appear and I'm lucky to see it

  • @Icy_Republic
    @Icy_Republic 3 ปีที่แล้ว +2

    Best C tut I've come across. Great work !

  • @jahoopyjaheepu497
    @jahoopyjaheepu497 2 หลายเดือนก่อน

    Best explanation of this topic on the entire internet.

  • @7Mango033
    @7Mango033 3 ปีที่แล้ว +4

    Thank your for this video, it was very helpful. I'm still having problems with realloc(), but you helped me realize that it is not really necessary for my use case.

    • @CodeVault
      @CodeVault  3 ปีที่แล้ว +3

      Make sure you store the return value of realloc, that's a mistake I used to make

    • @7Mango033
      @7Mango033 3 ปีที่แล้ว +1

      @@CodeVault Thanks for your answer. Oddly, i have done this. And this seems to be the only solution i can find online for the error 'invalid next size'..

  • @T-She-Go
    @T-She-Go 3 ปีที่แล้ว +1

    You know what sense is? It’s you. Thank you 🌸

  • @mrdiabeatis9943
    @mrdiabeatis9943 ปีที่แล้ว

    ok thanks for the actually really good explanation on arrays and memory

  • @Recordingization
    @Recordingization 3 ปีที่แล้ว +1

    Cool,nice lecture!I also saw your video about malloc

  • @mandymadman
    @mandymadman 3 ปีที่แล้ว +1

    best explanation on youtube so far!

  • @kratostheinevitable3932
    @kratostheinevitable3932 2 ปีที่แล้ว +2

    you explain so well. thanks.

  • @sotirisioannou324
    @sotirisioannou324 3 ปีที่แล้ว +2

    You are the BEST!!!! I mean it. You have been of great assistance for my understanding of C!!!! THANK YOU ->BEST!!! Wish I had 10000 likes

  • @VictorKokhanov
    @VictorKokhanov 3 ปีที่แล้ว +1

    Great explanation!

  • @TheFlipsta97
    @TheFlipsta97 3 ปีที่แล้ว +5

    A masters in engineering, working for a billion dollar company, yet once again, a random on youtube (no offence) has beat them all.

  • @pustai1627
    @pustai1627 3 ปีที่แล้ว +1

    mi-am dat seama din primele 10 secunde ca esti roman:)), bune tutoriale, mersi fain de ele!

    • @alexandruserban473
      @alexandruserban473 3 ปีที่แล้ว

      Eu chiar ma intrebam ce nationalitate are 🤣🤣

  • @MastrPotato
    @MastrPotato 2 ปีที่แล้ว

    This video is pure gold.

  • @andrewmasden6352
    @andrewmasden6352 3 ปีที่แล้ว +1

    Thank you! Awesome video!

  • @SirAbuser
    @SirAbuser 3 ปีที่แล้ว +4

    How would one go about shrinking an array length and removing the excess elements/index’s? I’ve tried realloc but it doesn’t seem to work properly when giving it a smaller size. Example: array size of 9, populate each index, now shrink it from size 9 to size 4 while keeping index’s 0-3 and discarding the rest 4-8

    • @CodeVault
      @CodeVault  3 ปีที่แล้ว +1

      Realloc should work... maybe you're doing the same mistake I did back in the day where you don't reassign the pointer. Needs to be done something like this:
      int* arr = malloc(sizeof(int) * 9);
      int* temp = realloc(arr, sizeof(int) * 4);
      if (temp == NULL) {
      perror("Failed to reallocate");
      exit(1);
      }
      arr = temp; // Reassign arr to the return value of realloc if reallocation was successful.

    • @SirAbuser
      @SirAbuser 3 ปีที่แล้ว

      @@CodeVault Problem with this is the rest of the index's/elements after 3 are still accessible are you reallocate. Therefore, it's not actually "shrinking" the array right? I'm trying to shrink arr from size 9 to size 4 while discarding the rest of the elements after 0-3. Any help to find a solution? Or, am I still able to access those index's because the system hasn't "reclaimed" that portion in memory and pulling those index's can cause undefined behavior?
      Thank you!

    • @CodeVault
      @CodeVault  3 ปีที่แล้ว

      You can still access them because the system hasn't done anything with the memory yet. This is something you should keep in mind in general: Whenever you deallocate/reallocate memory, you're only losing the rights to accessing it, the values are still there and you still might be able to access it for a while. But, by accessing it, you risk getting a segmentation fault since you don't know if another process claimed that memory in the mean time. So yes, for all intents and purposes, that memory is gone.

  • @AlessioTanzi
    @AlessioTanzi ปีที่แล้ว

    Great job very clear video bro

  • @TheScoobysteve
    @TheScoobysteve 2 ปีที่แล้ว +2

    I've seen this concept of realloc'ing memory by a factor of two until you have enough memory come up a fair bit. Is this some sort of best practice? Like a compromise between guaranteeing enough memory and not reallocating memory any more than necessary?

    • @CodeVault
      @CodeVault  2 ปีที่แล้ว +2

      I don't know if it's the best practice, although it is a sensible solution most of the time. The factor 2 here can be changed to whatever you think is more reasonable but this solution is way better than just incrementing the memory space by a fixed amount every time.

  • @mohammedalmutairi3155
    @mohammedalmutairi3155 5 หลายเดือนก่อน

    Thanks man 🙏🙏❤️

  • @georgichalakov6727
    @georgichalakov6727 3 ปีที่แล้ว +1

    Thank you!

  • @NeoxHBG
    @NeoxHBG 2 ปีที่แล้ว

    good video hermanito

  • @plugonien7011
    @plugonien7011 2 ปีที่แล้ว

    Thank you very much

  • @stonedcodingtom9097
    @stonedcodingtom9097 2 ปีที่แล้ว

    thx

  • @emptyFull123
    @emptyFull123 3 ปีที่แล้ว +1

    Why didn't you typecast the pointer returned by calloc and realloc?🙄

    • @CodeVault
      @CodeVault  3 ปีที่แล้ว +2

      That's only recommended in C++. In C it's actually NOT recommended to typecast the pointers from malloc, calloc and realloc.

  • @iex32
    @iex32 26 วันที่ผ่านมา

    3:00 Is the necessity of 2000 elements a reason for dynamic allocation?! Really?! Why? It's less than 8Kb of memory only.

    • @CodeVault
      @CodeVault  2 วันที่ผ่านมา

      For 2000 elements... not usually. But the stack has a limit of 1mb (usually), so I keep my arrays lower than that

  • @nmgdragon4710
    @nmgdragon4710 3 ปีที่แล้ว

    my complier is saying , [Error] invalid conversion from 'void*' to 'int*' [-fpermissive]

    • @nmgdragon4710
      @nmgdragon4710 3 ปีที่แล้ว

      can someone explain why this error is occuring in mine but not in his tutorial?

    • @CodeVault
      @CodeVault  3 ปีที่แล้ว +1

      You're probably using a C++ compiler which requires that you cast the result of calloc/malloc to the type you're assigning to. (int* in this case)

  • @mba2ceo
    @mba2ceo 6 หลายเดือนก่อน

    How do 3x4x5 ?

    • @CodeVault
      @CodeVault  5 หลายเดือนก่อน

      There is this video on the topic of multi-dimensional arrays: code-vault.net/lesson/smxk52wqjt:1603733529239

  • @ii7mdj_353
    @ii7mdj_353 2 หลายเดือนก่อน

    Realloc ❌
    Real lock ✅

  • @Mo3tez
    @Mo3tez 6 หลายเดือนก่อน

    can u use malloc ?

    • @CodeVault
      @CodeVault  5 หลายเดือนก่อน

      Yes, that would also work. calloc just sets everything to 0

  • @andreasandreou2651
    @andreasandreou2651 2 ปีที่แล้ว

    broooo pls dont make that accent video exelent only with subtitles

  • @calvyn743
    @calvyn743 6 หลายเดือนก่อน

    Hey man, you don't know me personally but I just wanted to say that the Lord Jesus loves you. He died for our sins so that we could get to know Him. He's waiting with open arms, just come to Him :)

    • @CodeVault
      @CodeVault  5 หลายเดือนก่อน

      I'm not religious, but thanks!

  • @kevinkkirimii
    @kevinkkirimii 3 ปีที่แล้ว +1

    Man pointers make some times the work easier. Java is a problem.

    • @CodeVault
      @CodeVault  3 ปีที่แล้ว +3

      What if I told you that Java uses pointers everywhere?

    • @kevinkkirimii
      @kevinkkirimii 3 ปีที่แล้ว

      @@CodeVault It does but I mean in terms of accessibility . I see this in C and Go.

    • @CodeVault
      @CodeVault  3 ปีที่แล้ว +1

      From experience I can say Java is easier to work with on larger projects. With C it's a bit more difficult, partially because of direct access to pointers (but it's also missing other core aspects). Although, depends on the project which one you should use.

    • @kevinkkirimii
      @kevinkkirimii 3 ปีที่แล้ว +1

      @@CodeVault true especially managing memory. Its just there are times I find that pointer like in Go would have been a great addition to java . Thank you for taking your time to answer . I hope you can do a lesson on Consistent hashing , DHTs et al in the future.

  • @zbigniewloboda3393
    @zbigniewloboda3393 ปีที่แล้ว

    Too much talking, too much festoons and NO CONTENT!

    • @CodeVault
      @CodeVault  ปีที่แล้ว

      Can you elaborate on this and how I can improve the content in the future? Thanks

    • @zbigniewloboda3393
      @zbigniewloboda3393 ปีที่แล้ว

      @@CodeVault
      Content:
      The format of your presentation should be much closer to format represented in edureka.
      Gestions:
      Your hand gestions are prevalent to Spanish and Italian culture and are not acceptable in conducting lecture due to destruction created. In other cultures hand gestions have high priority mining (compare to Latino culture). Therefore your hand gestions interfere with thoughts of students. That create your presentation useless if it is about of any degrees complexity.

    • @CodeVault
      @CodeVault  ปีที่แล้ว +1

      Well, for the content I don't try to copy other online educational courses (if you prefer them that's fine by me). I expect criticism regarding specifically to my content and how it can be improved. "What does edureka do better than in my videos?" and answers to such questions would really provide some good feedback.
      As for the hand gestures. This is the first time I saw someone complain about this. I'm not sure what you mean by "destruction created". Every time someone mentioned my hand gestures they said it helped them better understand what I was explaining. So, if 99% of my students benefit from these hand gestures and only about 1% don't, I will continue using them. Nothing works for everyone, it's about what works for the majority of people
      With that said, I will keep in mind what you said and see if there are ways I can improve things moving forward. Again, thanks for your feedback