Dynamic Arrays of Objects (Data Structures course, step-by-step)

แชร์
ฝัง
  • เผยแพร่เมื่อ 25 มิ.ย. 2024
  • In this video you will learn how to create both static and dynamic arrays of objects, how to insert data, copy from one array to another, and of course how to delete data from an array of objects. One very important topic that we will discuss in this video is memory management and how your application is using resources where I will point out some of the common mistakes. We will also learn the difference between deep and shallow copying of data, which is very important when working with arrays of objects and can be very tricky if you don't know it well. For that, I will show you different techniques like using built-in "memcpy" function as well as working with loops.
    As always, there are a few practical tasks included in this video just waiting for you to solve them so be sure to share your solution with others down in the comment section. Put on your learning hat and join me in this detailed video but also stay tuned for upcoming parts of this serial, because I'm probably working on them as you are watching this :D.
    📚 Learn how to solve problems and build projects with these Free E-Books ⬇️
    C++ Lambdas e-book - free download here: bit.ly/freeCppE-Book
    Entire Object-Pascal step-by-step guide - free download here: bit.ly/FreeObjectPascalEbook
    🚀📈💻🔥 My Practical Programming Course: www.codebeautyacademy.com/
    Experience the power of practical learning, gain career-ready skills, and start building real applications!
    This is a step-by-step course designed to take you from beginner to expert in no time!
    💰 Here is a coupon to save 10% on your first payment (CODEBEAUTY_YT10).
    Use it quickly, because it will be available for a limited time.
    ☕ If you've found my content helpful and would like to support me, you now have the option to buy me a coffee or a cookie! It's a small gesture of gratitude that means a lot to me and helps me keep creating free educational videos for you. You can use the link below to make a contribution: bit.ly/CodeBeauty_BuyMeACoffee
    However, please don't feel obligated to do so. I appreciate every one of you, and I will continue to share valuable content with you regardless of whether you choose to support me in this way. Thank you for being part of the Code Beauty community! ❤️😇
    Related videos:
    Array Data Structure (for absolute beginners) - • DATA STRUCTURES - How ...
    Arrays of objects, step-by-step, in-depth - • Arrays of objects - In...
    C++ Copy constructors - • C++ Copy constructors ...
    Exception handling in C++ - • Exception handling in ...
    Assignment Operators - will be linked soon
    Download PVS-Studio for free here:
    bit.ly/PVS-StudioFreeCB1
    Contents:
    00:00 Summary of what you'll learn (Dynamic Memory and Arrays of Objects)
    02:46 Example showcase
    03:28 Creation of static array of objects
    04:00 Why do we need dynamic arrays?
    04:38 Creation of dynamic array of objects
    06:55 Difference between static and dynamic arrays
    08:44 Deallocating memory
    10:42 Visual example of memory allocation (how arrays are allocated)
    12:29 Tools for code analyzing
    13:05 Entering data in an array using loops
    16:42 Arrays and functions (passing a dynamic array into a function)
    21:12 Changing size of the dynamic array at runtime (visual explanation)
    26:40 Changing the size of the dynamic array at runtime (code example)
    28:24 Copying elements from one array to another using "memcpy" function
    30:32 Copying elements from one array to another using loops
    31:50 Adjusting existing logic
    34:43 Practical task for you (with instructions)
    37:50 Problem with "memcpy" function
    41:00 Deep copy VS Shallow copy
    44:16 Second practical task for you
    45:41 Final words
    Add me on other platforms for more educational content:
    Instagram 📸 - / truecodebeauty
    Twitter 🐦- / truecodebeauty
    ******CODE IS IN THE COMMENTS******
  • วิทยาศาสตร์และเทคโนโลยี

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

  • @CodeBeauty
    @CodeBeauty  11 หลายเดือนก่อน +18

    📚 Learn programming with these Free E-Books ⬇
    C++ Lambdas e-book - free download here: bit.ly/freeCppE-Book
    Entire Object-Pascal step-by-step guide - free download here: bit.ly/FreeObjectPascalEbook
    🚀📈💻🔥 My Practical Programming Course: www.codebeautyacademy.com/
    Experience the power of practical learning, gain career-ready skills, and start building real applications!
    This is a step-by-step course designed to take you from beginner to expert in no time!
    💰 Here is a coupon to save 10% on your first payment (CODEBEAUTY_YT10).
    Use it quickly, because it will be available for a limited time.
    **CODE FROM THE VIDEO**
    #include
    using namespace std;
    class Student {
    public:
    string Name;
    int Age;
    char Gender;
    float ProgrammingGrade;
    };
    void printStudents(Student* students, int size) {
    for (int i = 0; i < size; i++) {
    cout

    • @awaism.a5192
      @awaism.a5192 7 หลายเดือนก่อน

      Absolutely superb ❤ but i need to know about female and boys array how to do that could just show me declaring it how to declare it i dont get any idea to do it

  • @omercandemirci6410
    @omercandemirci6410 10 หลายเดือนก่อน +5

    Thanks saldina for your helps. As an turkish electrical engineer student which want to learn Computer science, it is Hard to me learn something due to my language. Your language and explanation so clear that I can comprehend all what you want to teach in video. You are not just programmer teacher for me but also english teacher. ❤

    • @CodeBeauty
      @CodeBeauty  10 หลายเดือนก่อน +2

      You're very welcome! I'm genuinely glad to hear that my explanations are helping you grasp the concepts. I'm here to support your learning journey in both computer science and English. Your dedication and enthusiasm are truly commendable. Keep up the great work, and don't hesitate to reach out if you have more questions or need further assistance. Happy learning! 🌟

  • @rezafarokh8637
    @rezafarokh8637 10 หลายเดือนก่อน +2

    34:43 Practical task for you (with instructions)
    Student* getInput(Student* students, int start, int end) {
    for (int i = start;i < end; i++) {
    cout > students[i].Age >>
    students[i].Gender >> students[i].ProgrammingGrade;
    cout

  • @tumwesigyeisaac5073
    @tumwesigyeisaac5073 8 หลายเดือนก่อน +2

    hi Saldina, am an electrical engineering student at Kabale university in Uganda.
    i love the way you deliver programming content, you really made me fall in love with programing though am pursuing electrical engineering

  • @heikin5562
    @heikin5562 3 หลายเดือนก่อน +3

    Thank you so much for this free lesson. I can finally finish my project about array insertion and deletion, with the knowledge i gained here

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

      Glad it was helpful!

  • @aaronsj80
    @aaronsj80 10 หลายเดือนก่อน +5

    I like that you covered how the string is a pointer and also that you talked about the size of the student class. I noticed on the spreadsheet your addresses for each element were consecutive bytes, so it's good that you clarified it when getting to the copy code.

    • @CodeBeauty
      @CodeBeauty  10 หลายเดือนก่อน +1

      I realized that using Excel would help students visualize what I'm explaining, and I'm glad that it was helpful. :D

  • @handover007
    @handover007 หลายเดือนก่อน +1

    I want to see more videos like this.
    Thanks Saldina.

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

      You're welcome 😊 🥰

  • @874D8
    @874D8 2 หลายเดือนก่อน +1

    the explanation about memcpy , deep and shallow copy was 🔥

  • @htvital4184
    @htvital4184 11 หลายเดือนก่อน +3

    Thanks from bottom of my heart

  • @uzumakiibram1762
    @uzumakiibram1762 10 หลายเดือนก่อน +4

    Thanks mommy, i have learned a lot ❤

  • @carmelostagno6352
    @carmelostagno6352 10 หลายเดือนก่อน +2

    if the course you are preparing will be like these videos, then I can't wait. Thanks Saldina!

    • @CodeBeauty
      @CodeBeauty  10 หลายเดือนก่อน +1

      It will be better! I put so much work and effort into the course. I'm sad that I can not finish it faster, but quality requires time, and this has to be the best programming course ever. Coding will become your native language after this course. Whatever you can think of, you can create with your own hands and a keyboard! ❤️

  • @milkamilkica1935
    @milkamilkica1935 10 หลายเดือนก่อน +3

    the best teacher ever!

    • @CodeBeauty
      @CodeBeauty  10 หลายเดือนก่อน

      thank you :D

  • @kamyarbasiri959
    @kamyarbasiri959 11 หลายเดือนก่อน +2

    I'm looking forward watching the video

    • @CodeBeauty
      @CodeBeauty  10 หลายเดือนก่อน

      let me know if you find it helpful :D

  • @eliasrodriues6614
    @eliasrodriues6614 10 หลายเดือนก่อน +3

    You are a good teacher.

    • @CodeBeauty
      @CodeBeauty  10 หลายเดือนก่อน

      thank you :D

  • @user-xm7ir9gv1u
    @user-xm7ir9gv1u 10 หลายเดือนก่อน +1

    Thanks a lot! I have learned much C++ skills from you, I love you CodeBeauty!

    • @CodeBeauty
      @CodeBeauty  10 หลายเดือนก่อน

      Happy to hear that!

  • @zoldyck3526
    @zoldyck3526 10 หลายเดือนก่อน +1

    Thanks a lot you are a good teacher.

    • @CodeBeauty
      @CodeBeauty  10 หลายเดือนก่อน +1

      always good to hear :)

  • @user-sw8bq1rk9c
    @user-sw8bq1rk9c 10 หลายเดือนก่อน +1

    great job, thank you Saldina, this is very helpful

    • @CodeBeauty
      @CodeBeauty  10 หลายเดือนก่อน

      I'm so glad to hear that.

  • @user-lu6kv9kr8u
    @user-lu6kv9kr8u 10 หลายเดือนก่อน +1

    great value, thank you Saldina

    • @CodeBeauty
      @CodeBeauty  10 หลายเดือนก่อน

      Glad you think so!

  • @Tableskater
    @Tableskater 10 หลายเดือนก่อน +1

    You are so awesome ❤❤❤😊😊

  • @lickguitars1276
    @lickguitars1276 8 หลายเดือนก่อน

    Thank you for this concise tutorial on dynamic arrays! It made the concept much clearer for me, and im sure it will benefit others too

  • @deanmorrison6254
    @deanmorrison6254 10 หลายเดือนก่อน +1

    very helpful, thank you so much

    • @CodeBeauty
      @CodeBeauty  10 หลายเดือนก่อน

      You're so welcome!

  • @TrueNRG
    @TrueNRG 10 หลายเดือนก่อน +3

    Симпотная барышня😊

    • @CodeBeauty
      @CodeBeauty  10 หลายเดือนก่อน

      Спасибо :D

  • @amelccc
    @amelccc 10 หลายเดือนก่อน +1

    great job as always 👌👌

    • @CodeBeauty
      @CodeBeauty  10 หลายเดือนก่อน

      Thank you so much 😀

  • @codeoasis1180
    @codeoasis1180 10 หลายเดือนก่อน +2

    great explanation

    • @CodeBeauty
      @CodeBeauty  10 หลายเดือนก่อน

      Glad it was helpful!

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

    there is some std STL object, std::array, which would be used in place of a depreciated sort of c-array

  • @_-deep_a_b.862
    @_-deep_a_b.862 7 หลายเดือนก่อน

    thanks!

  • @shreyashpawar1689
    @shreyashpawar1689 10 หลายเดือนก่อน +1

    Love from India ❤🇮🇳

    • @CodeBeauty
      @CodeBeauty  10 หลายเดือนก่อน +1

      Love for India! ❤

  • @vetledeilkas538
    @vetledeilkas538 10 หลายเดือนก่อน

    When the program exits, all memory (both on the stack and on the heap) is deallocated automatically. So to delete the memory right before the program exits makes no difference, although it is good practice!
    Also, with the practice example, there's no need to return a pointer to the array of students, as you pass the pointer to the students array as an argument to the function. Returning void is sufficient. The same array will be modified in the function as the memory of the array is passed to the function.
    Please correct me if I said something wrong! Overall very good video!

    • @CodeBeauty
      @CodeBeauty  10 หลายเดือนก่อน

      In C++, memory management is more manual. You need to allocate and deallocate memory yourself. In some other languages like C# for example, memory management is more automated, especially for objects on the heap, thanks to the garbage collector, which automatically frees memory when objects are no longer needed. Stack memory in C# is managed similarly to C++, where it's automatically deallocated when the variable's scope ends. :D

    • @CodeBeauty
      @CodeBeauty  10 หลายเดือนก่อน +1

      One thing that you can do is use smart pointers. They are a container/wrapper for a raw pointer. In modern C++ smart pointers are defined in the std namespace in the memory header file.
      One big advantage of smart pointers is that they are responsible for deleting the memory that they use, which means that they automatically deallocate the memory when they go out of scope. :D
      You can check out my video about smart pointers if you want to learn more about this, here: th-cam.com/video/e2LMAgoqY_k/w-d-xo.html

  • @carlosalbertoavilapalacios5623
    @carlosalbertoavilapalacios5623 10 หลายเดือนก่อน +1

    Hi Saldina. Thanks for this video. Do you know of any extension for plotting data in Visual Studio 2022, for example, plotting a data array of velocity versus time? Excuse me for the question that is off-topic from the video. Thanks for your time. Best regards

    • @CodeBeauty
      @CodeBeauty  10 หลายเดือนก่อน

      I think that Microsoft Chart Controls could be of help to you. I'm sure you can find plenty of resources online regarding this topic. Hope that helps. Cheers.

  • @jaykay6249
    @jaykay6249 10 หลายเดือนก่อน

    Really helpful thanksksks

    • @jaykay6249
      @jaykay6249 10 หลายเดือนก่อน

      You could maybe also provide the code you wrote so we could poke around a bit.

  • @muhmedgamal5841
    @muhmedgamal5841 วันที่ผ่านมา

    goooooooooooooood

  • @anwar6971
    @anwar6971 10 หลายเดือนก่อน +1

    Thanks

    • @CodeBeauty
      @CodeBeauty  10 หลายเดือนก่อน

      Welcome :)

  • @MikeysLab
    @MikeysLab 4 หลายเดือนก่อน

    What is the efficency difference between changing student name to a fixed primitive (char Name[32]) and using memcpy, over doing a deep copy. The deep copy seems extremely inefficent especially in an embedded environment.

  • @lpmc1182
    @lpmc1182 10 หลายเดือนก่อน +1

    do you have any suggestion of projects that are job-ready and worth to be added on a cv in order to apply for jobs? I'm struggling in finding project ideas and can't come up with any on my own, and "to do list" type of projects I assume they aren't job ready

    • @CodeBeauty
      @CodeBeauty  10 หลายเดือนก่อน

      Well, depending on your current skill set there are many suitable ideas. But since you are on channel that is mainly focused on C++ and C# so far and given the fact that you are looking for a job, I will assume that you don't have that much experience yet so here are some of the projects that should not be too complex to develop but still very useful as showcase of your capabilities as well as good practicing materials:
      Simple Console Calculator:
      Create a console application that performs basic arithmetic operations like addition, subtraction, multiplication, and division based on user input.
      Guess the Number Game:
      Develop a game where the computer generates a random number, and the player has to guess it within a certain number of tries.
      Library Management System:
      Design a program that allows users to manage a library's inventory, including adding, searching, and borrowing books.
      Student Grade Tracker:
      Build an application to store and calculate student grades. Allow users to input and retrieve student data and calculate averages.
      Personal Expense Tracker:
      Develop a program that helps users track their daily expenses and presents them in a clear and organized manner.
      Address Book:
      Build an application that allows users to store and manage contacts' information, including names, phone numbers, and addresses.
      Also, be sure to sign up here bit.ly/SimplifyingCoding
      and I will let you know when my practical programming course is out, with a nice discount attached. It will serve you greatly in your search for a job.
      Hope you will find these useful. Good luck with finding the right job for you! :)

  • @branriv8417
    @branriv8417 10 หลายเดือนก่อน +1

    Hi Saldina. What program do you use to allow your viewers to see you and your screen simultaneously?

    • @CodeBeauty
      @CodeBeauty  10 หลายเดือนก่อน

      I'm filming me with the camera, separately from screen, which is recorded with OBS studio, then in editing I'm combining it into one video. Hope that helps.

    • @branriv8417
      @branriv8417 10 หลายเดือนก่อน

      @@CodeBeauty Thank you.

  • @valjo02
    @valjo02 10 หลายเดือนก่อน +2

    Would you provide pastebin of above code or we should put an efford and try rewrite in our IDE Saldina?

    • @CodeBeauty
      @CodeBeauty  10 หลายเดือนก่อน +1

      Hey, I pinned the code in the first comment. :D

    • @valjo02
      @valjo02 10 หลายเดือนก่อน +1

      Wow !I found it.Thanks a lot .Finally uderstand the main point of the code is to showcase how to dynamically allocate memory for an array of objects,
      resize the array based on user input, and copy the existing objects to the new array while adding new objects if required.
      👍👌💜@@CodeBeauty

  • @AbderrahmenBenabbas2001
    @AbderrahmenBenabbas2001 10 หลายเดือนก่อน +2

    Could you give me the names of C++ Books that you have read

    • @CodeBeauty
      @CodeBeauty  10 หลายเดือนก่อน

      check out these 2, they are free, and they are very good (one of them is for C++)
      bit.ly/freeCppE-Book
      bit.ly/FreeObjectPascalEbook

  • @xamgodoix
    @xamgodoix 10 หลายเดือนก่อน

    Hi! I think there is something "wrong" with the example:
    Student *students = new Students[3];
    The expression above already allocate space for 3 students. There is no need to create another Student inside for loop.
    Inside for loop could be something like this:
    Student &s = students[i];
    A reference to each student already allocated.

    • @CodeBeauty
      @CodeBeauty  10 หลายเดือนก่อน +1

      There are two options for adding data: you can create a new variable, allow the user to input data into it, and then insert this variable into the array. Alternatively, you can directly input the data into the array. It's a good exercise if someone wants to try and practice on his own. :D

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

    dynamic array is literally std::vector with fewer features, depreciated

  • @ross350tube
    @ross350tube 10 หลายเดือนก่อน

    or... You just use vectors, which have built functions that automatically manage much of this.

  • @christopherrice891
    @christopherrice891 10 หลายเดือนก่อน +3

    Saldina have you ever made a 3-D open world video game in C++? I'm asking because making games is why i want to learn C++ so badly.

    • @CodeBeauty
      @CodeBeauty  10 หลายเดือนก่อน +2

      I did my fair share of coding in gaming world as well and for sure I can see certain appeal in it. What are you mostly interested in?

    • @christopherrice891
      @christopherrice891 10 หลายเดือนก่อน

      @@CodeBeauty i am mostly interested in games like on Playstation 5 or Game Cube or X-BOX. I watch the beginning stages of making a game on TH-cam, but i never once saw a video game built from the ground up. I want to see a full game created.

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

    actually, calling delete on a c style array is Undefined Behavior, not a memory leak.

  • @deveshbhati8852
    @deveshbhati8852 10 หลายเดือนก่อน +1

    Can we ?

    • @CodeBeauty
      @CodeBeauty  10 หลายเดือนก่อน

      check your previous comment :)

  • @dattrax
    @dattrax 10 หลายเดือนก่อน

    I believe there was a technical error in this video. The reason for the delete is not correct, as without the [] it doesn’t run the destructors. This is where leaks occur, when the object contains another object, it wouldn’t get free’d. I also don’t believe it’s good practice to use naked new anymore but stl::vector would have been better in this instance.

    • @CodeBeauty
      @CodeBeauty  10 หลายเดือนก่อน +1

      One thing that you can do is use smart pointers. They are a container/wrapper for a raw pointer. In modern C++ smart pointers are defined in the std namespace in the memory header file.
      One big advantage of smart pointers is that they are responsible for deleting the memory that they use, which means that they automatically deallocate the memory when they go out of scope. :D
      You can check out my video about smart pointers if you want to learn more about this, here: th-cam.com/video/e2LMAgoqY_k/w-d-xo.html
      And regarding memory leaks, try this tool. I believe it should be free to download it via this link and it is static code analyzer, so you run the analysis of your code and it wil tell you if there are memory leaks and even suggest how to fix them: bit.ly/PVS-StudioFreeCB1
      Hope this helps :D

  • @deveshbhati8852
    @deveshbhati8852 10 หลายเดือนก่อน +2

    I am looking for connect with you

    • @CodeBeauty
      @CodeBeauty  10 หลายเดือนก่อน

      here you can find links to my social media:
      linktr.ee/codebeautyprogramming?fbclid=PAAabr49GuzhYS4Jfh0vw7nyDk_-uqYqKhkauLq6cqyCQSGZUH5_dVDH6-41A
      Cheers. :)

    • @deveshbhati8852
      @deveshbhati8852 10 หลายเดือนก่อน

      Can you please reply over Instagram ?

  • @AlexFranck-sx7tc
    @AlexFranck-sx7tc 10 หลายเดือนก่อน

    21:13 that's not an advantage.
    you can very much define the size of a "static" array at runtime. The term "static array" is common but I do not understand its meaning. I would simpy call it arrays stored on the stack or stack arrays.
    so-called dynamic arrays are simply arrays created in the heap memory. I would call them heap arrays.
    the size of an array cannot be changed.
    I believe what you are trying to say is: data stored in a heap array can always be relocated to accommodate new data.
    the size of an array does not change. rather the data can be moved to a larger or smaller array, or simply to another array.

    • @CodeBeauty
      @CodeBeauty  10 หลายเดือนก่อน

      If you have static array you cannot reallocate it to add more elements.
      The size that you define while you are writing the code is the size that array will have forever, and if your collection grows and you need to add more elements than the size of the array, you won't be able to store them. Also if you create very big static array, but big chunk of that memory is empty cuz you don't have elements to put into it, than you are simply wasting memory and memory management is not efficient.
      Because of that you can use dynamic arrays which can be deallocate and then you can allocate new (bigger, or smaller) space for them. That way your memory management is more efficient. :D

    • @AlexFranck-sx7tc
      @AlexFranck-sx7tc 10 หลายเดือนก่อน

      @@CodeBeauty you do not reallocate the array. you relocate the data. once you create an array, its size is final. it does not matter if you create it on the heap or on the stack.
      dynamic array is more like an umbrella term. when you get more data, you create a new heap array, you move the data to that new array, and release the memory areas occupied by the old array. the size of an array does not change. it never changes.

  • @robertbruce7686
    @robertbruce7686 10 หลายเดือนก่อน +1

    Too many ju.....mp cuts

    • @CodeBeauty
      @CodeBeauty  10 หลายเดือนก่อน +1

      you are missing the point of the video :D

  • @aapck
    @aapck 9 หลายเดือนก่อน

    In C++, the 'new' and 'delete' operators can be overridden, allowing us to execute our own code when allocating or deallocating memory. This is useful in situations where memory management is expected from you, such as in embedded systems where there is no operating system. In such cases, these operators are overridden.:o)

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

    void studentinfoin(Students* students,int start,int size){
    for(int i=start;i