C++ Programming Tutorials - 30 - C++ Dynamic Arrays - Eric Liang

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

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

  • @truthspeaks651
    @truthspeaks651 6 лет назад +37

    Hi, I am a new student of C++, just learning the basics of the language. Your lesson was clearly presented and easy to follow through. Thank you, keep up the good work.

    • @ewliang
      @ewliang  6 лет назад +1

      Thanks Boninkoshi! Good luck on your C++ coding journey!
      Yeah, I'm very busy with in-real-life stuff so I don't have much time to make RUclips videos. I do plan on restarting again with a new series so... be on the lookout. ;)

  • @allphotoscloud6733
    @allphotoscloud6733 Год назад +5

    You are an absolute genius. My college professor just talks all the time with one slide on the screen but you actually know how to teach. It definitely is true that teaching is a skill only a few can acquire. You are gifted.

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

      Thank you! 🙂 I'm glad my video helped you out.

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

    I really like the addition of the "How to make a dynamic array" slide. It's a nice touch that breaks it down. Really well-done video.

  • @derekmontross1081
    @derekmontross1081 2 года назад +7

    awesome video, you're the only person I've seen that explains this in a way most people can understand. Thank you!

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

      Glad my videos helped. :)

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

    You teaches so good,but there is problem when I delete the program the program does not deletes........

  • @treeceu
    @treeceu 5 лет назад +6

    Great, easy-to-learn, commentary/instruction! Thanks for the help!

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

    I want to thank you very much i was lost in this topic and you started by saying wat we will learn and you were so organized and i understood everything

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

      No problem. Glad that my videos helped you out! 🙂 Happy holidays!

  • @unasgaki8403
    @unasgaki8403 11 месяцев назад +2

    this is the only video that helped me understand it easily

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

      Glad it helped!

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

    Thank you so much ! Watched a lot of videos but nobody explained it this well 😊

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

      No problem, glad it helped! :)

  • @anawahssimpson9249
    @anawahssimpson9249 7 месяцев назад +1

    You're a great teacher, thank you so much

    • @ewliang
      @ewliang  7 месяцев назад

      No problem, I'm happy that my videos helped! 🙂

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

    I was having trouble to understand this but thanks to you I understood it now❤.

  • @ruppeshchoudhari5923
    @ruppeshchoudhari5923 3 года назад +1

    you r a good teacher friend... keep it up...

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

    A cool application: I am using a dynamic array to store the joint positions of a 5dof robot arm, where each row is a position target and each column holds the orientation of a different joint. The column dimension is fixed at 5, but the row dimension must be dynamic to accommodate the varying number of target points stored for a toolpath. I then have functions to parse thru the array and move the different joints based on the stored data. Thank you for the well done video.

    • @ewliang
      @ewliang  11 месяцев назад

      That is cool! I never knew that they would be used in that type of application.

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

    Great Explanation sir🎉

  • @atillathehun6434
    @atillathehun6434 3 года назад +1

    Really good video man! Liked the way you structured it, well done!

  • @LPloppG
    @LPloppG 2 месяца назад

    Didn't you have to delete the dynamic array again after you set it to 5 elements? At 8:26

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

    Do we always need to delete the array?

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

      It's important to delete since memory space is being used to store the information. If you don't delete, eventually, the memory will run out of free space and that will result in errors occurring in your program or potentially crash during runtime. 🤪

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

    Thank you Eric

  • @samdanisatti2600
    @samdanisatti2600 6 лет назад +4

    bro for two dimensional array what changes i have to do.....?

  • @blind_neighbourhoodNerd
    @blind_neighbourhoodNerd 5 лет назад +1

    Excellent video. Thanks so much!!

    • @ewliang
      @ewliang  5 лет назад

      No problem. Glad it helped! :)

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

    Thank you so much!
    Your video was awesome!🤩
    It was clearly presented and easy to understand right away!
    I hope you get a million subs!

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

      Thank you for your kind words. Wish you the best on your programming journey! 🙂

  • @developerakhter______0076
    @developerakhter______0076 4 года назад

    *The Real Teacher The way of your teaching skill is excellent... & Unique... I found no channel in RUclips who made ths topic clearly*

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

    got exactly what i wanted, in this video

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

    perfect we need more videos

  • @stevebim000
    @stevebim000 4 года назад +1

    Holy shit! Thank you so much man! I was so confused with this topic, you saved me! Thank you!!

    • @ewliang
      @ewliang  4 года назад

      No problem, glad my video helped. 🙂

  • @DanT-iu6oc
    @DanT-iu6oc 5 лет назад +1

    Just subscribed. Great explanation, very simple and clean. Keep it up!

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

    This was an amazing tut thanks a lot. I watched the whole, it felt like a full lecture. My professor could use some tips 😂

    • @ewliang
      @ewliang  9 месяцев назад

      No problem. Glad my video helped even after so long. :)

  • @CliffysRant
    @CliffysRant 4 года назад +1

    Maaaaate you are an absolute legend !

    • @CliffysRant
      @CliffysRant 4 года назад

      keep making videos please :)

  • @darshandhande8903
    @darshandhande8903 5 лет назад +2

    Awesome. Could you make a video on 2D Dynamic Arrays?

  • @senghle
    @senghle 4 года назад +1

    You do really good job bro Thank.

  • @nooralnasser4588
    @nooralnasser4588 3 года назад +1

    Thank you, you made this so clear 🦋💕

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

      Np, glad it helped. 🙂

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

    Hi! Your video is PERFECT! I've watched some videos about dynamic arrays on Russian. Understanding came only after your video). Thank you.

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

      No problem. Wish you the best on your coding journey. :)

  • @rchendrix7835
    @rchendrix7835 4 года назад +1

    Hello Eric. Quick Syntax error I am running into is with the for loop for inputting the values into the array. I am getting an error saying that "identifier 'k' is undefined. Being new to C++ I am not sure as to what the issue is between the code you have and the one I am adapting it to for my class use. I also think I will be tuning into more of your C++ videos due to the clarity of presentation and ease of information intake.

    • @ewliang
      @ewliang  4 года назад

      Hmm. Did you remember to do int k = 0 in your loop setup?

    • @rchendrix7835
      @rchendrix7835 4 года назад +1

      Eric Liang I did. The loop is: for (k =0, k

    • @ewliang
      @ewliang  4 года назад +1

      @@rchendrix7835 Make sure the k = 0 has the 'int' in front, so it shows for(int k = 0; k < count; k++) It is important to include it since C++ is a typed language. Adding int tells the system that the variable k is an integer aka a whole number.

    • @rchendrix7835
      @rchendrix7835 4 года назад

      Eric Liang yeah sorry forgot to put that in there on last comment but I have that in there as well and still same issue

    • @26manasviverma14
      @26manasviverma14 Год назад

      @@rchendrix7835 so u learnt c++ or what

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

    what if i don't want to specify the size of array? can i make an array which inputs data from user that exceeds the defined number of array size

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

      I personally like using vectors. You'll need to add
      #include
      at the top of your file where #include is at, but on a new line.
      This will enable you to create vector variables. Vectors have a bunch of built in functions that will make your life so much easier when it comes to manipulating data stored inside it.

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

    Its very clear thank you , but what is the purpose of delet[] ?

  • @snehanujhi1782
    @snehanujhi1782 4 года назад

    very helpful! thanks a ton!

  • @vinineathvuth1743
    @vinineathvuth1743 4 года назад

    Thank for this video !!!

  • @tatkonerez
    @tatkonerez 4 года назад

    Excellent

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

    Cant you do this with the static array? whats the point?

  • @PrakrutiYadavMusic
    @PrakrutiYadavMusic 3 года назад +1

    but that we can do with static arrays(no pointer) as well.. taking count from user and creating array of that 'count' size.. how can we create array if we want it to have as many elements as we keep putting in it without user or anyone specifying the size.. one that is completely dynamic?

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

      Yeah, when i started out, I found this to be an extremely weird way of approaching dynamic arrays too, especially since it asks for a size in the beginning.
      Realistically, most people use vectors which is what you're looking for.
      You will need to add this to the top of the page:
      #include
      Here's a link www.google.com/amp/s/www.bitdegree.org/learn/c-plus-plus-vector/amp

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

      Yeah, vectors can be useful! Thanks.

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

    can you not edit the array from having 10 values to 11 after?

  • @user-jr6zr9rc2w
    @user-jr6zr9rc2w 3 года назад +1

    This is not working in visual studio code I am talking abt these , help
    Char *a;
    A=new char[5];
    If I run these my visual studio code shuts down

    • @ewliang
      @ewliang  3 года назад +1

      I've never done any C++ development on Visual Studio Code, so sadly, I'm unable to help you on this one. The program I used in this video was the regular "Visual Studio".

    • @user-jr6zr9rc2w
      @user-jr6zr9rc2w 3 года назад +1

      @@ewliang no prob
      😊

  • @TrushSeeker22
    @TrushSeeker22 4 года назад +1

    thank you

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

    Question : How to make a new array in a function such it can be used in main afterwards ?

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

      Hmm, depending on your needs, maybe it needs to be a global variable. (Put the array outside of the function and update its values from wherever you need to).
      However, if you're doing object oriented programming, it can be one of the data members inside your object's class (probably a public variable depending on your needs).

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

      @@ewliang my situation would be something like:
      I have a text file, inside is a list of food items ID and their prices, the first row show how many types of food they are, then the rest is their ID and price, something like
      4
      3567 8.9
      2468 8.3
      2856 9.1
      1156 2.3
      the goal would be make a function reading the textfile, knowing the required size of the array, read the file into the arrays, and pass the arrays back into main to it can be used afterwards.

  • @marcol6268
    @marcol6268 4 года назад +1

    This is THE perfect solution for what I was looking for. Not that I'm only amazed that someone presented the EXACT problem that I had, no, but you also explained it so clearly and comprehensivly, wow....this is just amazing. Kudos!
    I will probably use different loops like while (k < count)
    { blabla} but its just a matter of style at that point.
    Thank you &subbed!

    • @ewliang
      @ewliang  4 года назад +1

      No problem. I'm glad my video helped. 🙂

    • @marcol6268
      @marcol6268 4 года назад

      @@ewliang I stumbled upon one more problem though, that I cannot figure out how to solve. Its actually more like 2 problems, but for the sake of new years, lets call it 1,5 :)
      1st Problem: If i set my initial user_input_value to 1 then it "skips" the value "0" in the array, therefore allowing just to store 4 numbers, if the user types in 5. I can change that by just setting the input to 0 but then my cout starts with value 0. (Which kinda makes no sense for a user).
      1,5nd Problem: How do i display all inputs after they are done? (Like a list e.g.?) Because for whatever reason, it doesnt do it in my code.
      The code:
      #include
      using namespace std;
      int main()
      {
      int amount;
      int i = 1;
      cout amount;
      int *DynArray;

      DynArray = new int[amount];
      while (i < amount)
      {
      cout

    • @marcol6268
      @marcol6268 4 года назад

      @Eric Liang: Ok nvm one problem i figured out. its "while ( i < = amount)"

    • @marcol6268
      @marcol6268 4 года назад

      And I figured out my output problem, but somehow it displays the memory adress instead of the actual value.
      for (int k = 0; k < amount; k++)
      {
      cout

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

    It's pretty good to solve my problem about how to resize the dynamic array. But I still have a question: the book said that static array allocates memory during compilation and dynamic array allocates during runtime. But when I try to use cin to allocate a size to the static array from user input. It also works fine. Why is that?

  • @thesageofwisdom3763
    @thesageofwisdom3763 4 года назад +1

    Thank you 😭

  • @AnuMessi10
    @AnuMessi10 4 года назад

    In the ending you took the input from the user depending upon how many arrays they want...right??Now if I want to set the size of those arrays individually...how'd I do that??
    for eg. I want 10 arrays and in the 3rd one , I want to set it's size to 5 elements only...

  • @mchardybecoy1580
    @mchardybecoy1580 9 месяцев назад

    thanks a lot

  • @bradwang3648
    @bradwang3648 6 лет назад +2

    Still confusing.. I can do the exact same thing using static arrays...

    • @ewliang
      @ewliang  6 лет назад

      Depends on use case and scenarios. :)

    • @theseptimel
      @theseptimel 4 года назад

      you can´t in c/c++, you have to know the size of an array before runtime....if you just
      int k;
      int arr[k];
      then it wouldn´t even compile...k must be a constant int

  • @mrfancyp4ntz855
    @mrfancyp4ntz855 6 лет назад

    good video, thanks.

  • @parthshinde8716
    @parthshinde8716 11 месяцев назад

    very happyyyyyy !!!

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

    arent vectors just better cause they automatically adjust in size easilly ?

  • @wagsman9999
    @wagsman9999 11 месяцев назад

    Why not just use std::vector ??

  • @walidsaidi6077
    @walidsaidi6077 4 года назад +1

    I have a question if you are still here,
    Why does my code say 'the readablesize is 4bytes,but 8 bytes may be read' when I use dynamic arrays. Anyone please answer🙏🙏🙏

    • @ewliang
      @ewliang  4 года назад +1

      I'm not sure, I've never ran into such an issue before. 🤔 Maybe if you'll share some of your code, that will give a better hint as to what the problem might be. I would guess it has to do with something involving delete.

    • @walidsaidi6077
      @walidsaidi6077 4 года назад +1

      Yes it was the delete part. Thank you though. Great content and teaching skills.👍👍🙏🙏

    • @ewliang
      @ewliang  4 года назад +1

      @@walidsaidi6077 Ah, ok 👌.
      No problem, glad my videos helped.🙂

    • @walidsaidi6077
      @walidsaidi6077 4 года назад

      Is there a website where I can find c++ exercises that you would recommend someone to use.
      I am trying to put my knowledge to work so that I can be better at programming.
      Please help if possible

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

    is defining array size necessary in dynamic arrays or can we leave it empty like A= new int[ ] in the step 4 of my code
    int n
    Cin>> n
    int *A
    A= new int[n]

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

    why does C++ make this so complicated? in python this takes 2 seconds. couldnt they provide an in built function?

    • @ewliang
      @ewliang  4 года назад

      Ikr

    • @xx-wp3mq
      @xx-wp3mq 3 года назад +1

      Because high-level languages like python fully use automatic garbage collection. However, if you wish to manually manage memory (say for optimal speed) then this is why. Pythons automation means that runtime and memory storage is usually slower. CPP allows far more versatility in terms of options when addressing memory at the cost of slightly more complexity.

  • @yuhanliu6338
    @yuhanliu6338 5 лет назад

    Question, what is preventing use from making a dynamic array with out the new keyword? For example
    int x;
    cin >> x;
    int someArray[x];
    ?
    I get that the heap have advantages, as well as being able to delete, but can we not do what I just did above?

    • @bhrigukansra
      @bhrigukansra 4 года назад

      Yes you are correct but if the user initallly say like someArray[10], so you can only store 10 values in that array but using dynamic array we can add another element(i.e. 11th) using new int array[new count]. if you did like static (as you wrote) you'd have to create a new array with 11 elements, copy your earlier 10 element and then take the newer element as input which a hell lotta work as compared to dynamic .

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

    perfect!

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

      Glad my video helped. 🙂

  • @Mahnoor_076
    @Mahnoor_076 10 месяцев назад

    I don't get this. If static has fixed value then why are we giving a specific value to dynamic array.?

    • @ewliang
      @ewliang  3 месяца назад

      Sorry for the late reply, life's been busy. I'll still leave this reply if anyone stumbles across the video.
      Static arrays define the size of the array at compile time. You can think of it like saying... hey, I'm going to build a house on this land. Based on my blueprints, the house will take up 600 sq ft of land. Now let's go build the house.
      Dynamic arrays size is determined during runtime, but because everything data related has to take up space, this is why you specify a size value when you code. You can think of it like saying... hey, I am going to build a house on a plot of land. I don't know how much of the land I my house will take up, BUT at least I can provide you the border size (formally speaking, the property lines) of my land (let's say 2000 sq ft). Ok, let's go build the dream house!
      Computer memory is limited in size. So in the case of static vs dynamic arrays, static array basically is like a reserve a spot in your computer's memory how much space it'll potentially use up.
      Dynamic array is like a more flexible version, it's just that you have to still specify the max limit. How much of that will actually be used is dynamic. ;)

  • @КостКостелло
    @КостКостелло Год назад

    awesome video, but i still do not get why should we use "*" before "myDynamicArray" could somebody explain it to me)

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

      Normal static arrays, you are saying hey, this piece of land where I'm drawing a box around, it belongs to me.
      In the case of dynamic arrays, the '*' represents a pointer. Dynamic arrays in this case is like saying, hey, this part of the land i'm pointing to with my finger, that's where I'm going to start building my property on, and wherever it ends at is where ever I stop pointing to. Also the pointer is a point of reference.
      Hopefully the metaphor helps a bit. 🤐

  • @realthug1530
    @realthug1530 4 года назад

    I see both dynamic arrays and linked lists doing same things, the only diff being here we are just allocating them contiguously.

    • @ewliang
      @ewliang  4 года назад

      stackoverflow.com/questions/35401508/dynamic-array-vs-linked-list-in-c

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

    this was so clear, thank you. BUT "int i" wasnt declared at the top, how was it still accepted as a variable?

    • @ewliang
      @ewliang  3 года назад +1

      It's declared in the for loop.

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

      In c you can't declare i in the for loop, but in c++ you can 🙂

  • @trialaccount2244
    @trialaccount2244 4 года назад

    int size;
    cin>>size;
    int arr[size];
    Why this is not a dynamic memory allocation?🙄

    • @ewliang
      @ewliang  4 года назад

      Since I haven't touched C++ for a while, the following might/might not be right. But give it a shot and let me know if it works or not:
      int size;
      cin >> size;
      int *arr = new int[size];
      for(let i = 0; i < size; i++) {
      arr[i] = i; // Assigning array with dummy data from 0 to size - 1.
      cout

  • @abhashsingh1802
    @abhashsingh1802 3 года назад +1

    ATTENTION: just go and watch some videos on memory allocation in c++ (stack vs heap) before going through this video to understand it in a better way.
    link: ruclips.net/video/_8-ht2AKyH4/видео.html

  • @changenoways9555
    @changenoways9555 5 лет назад +1

    The video starts at 9:00 .SMDH

  • @TanvirAhmed-p4u
    @TanvirAhmed-p4u Год назад +1

    How it could be a dynamic array? Are you kidding?
    It should not need to mention the size. It will increase its size automatically. -_-

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

      I agree. I had the same thought when I was first taught this. One way to mitigate this to make it less manual is by using pointers to auto adjust the capacity when the capacity is near its limits. Another way, which is more real world use case is to use the std::vector way of doing things.

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

    these dynamic arrays aren't so "dynamic" are they?
    just use fkin vectors instead lol

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

      Back in the days, I had a plan to slowly introduce things from the simpler concepts to the more interesting stuff, that's why I didn't make a video about vectors this early into the series back then.
      But yes. Vectors are more practical in my opinion.