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

preview_player
Показать описание
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 ⬇️

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.

I use it to enhance the performance, features, and support for C, C#, and C++ development in Visual Studio.
It is a powerful, secure text editor designed specifically for programmers.

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:
Assignment Operators - will be linked soon

Download PVS-Studio for free here:

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:

*******CODE IS IN THE COMMENTS*******
Рекомендации по теме
Комментарии
Автор

📚 Learn programming with these Free E-Books ⬇
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 <iostream>
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 << "STUDENT " << i + 1 << "\t";
cout << students[i].Name << "\t";
cout << students[i].Age << "\t";
cout << students[i].Gender << "\t";
cout << students[i].ProgrammingGrade << "\n";
}
}
int main()
{
int size;
cout << "Number of students: ";
cin >> size;

Student* students = new Student[size];

for (int i = 0; i < size; i++) {
cout << "STUDENT " << i + 1 << endl;

Student s;
cout << "Name: ";
cin >> s.Name;
cout << "Age: ";
cin >> s.Age;
cout << "Gender: ";
cin >> s.Gender;
cout << "Programming grade: ";
cin >> s.ProgrammingGrade;

students[i] = s;
}

char choice;
cout << "Do you want a bigger array?";
cin >> choice;
if (choice == 'n' || choice == 'N') {
delete[]students;
return 0;
}

int newSize;
cout << "Enter new size: ";
cin >> newSize;

Student* newStudents = new Student[newSize];
//memcpy(newStudents, students, sizeof(Student) * size);
for (int i = 0; i < size; i++) {
newStudents[i] = students[i];
}

delete[]students;
students = newStudents;

for (int i = size; i < newSize; i++) {
cout << "STUDENT " << i + 1 << endl;

Student s;
cout << "Name: ";
cin >> s.Name;
cout << "Age: ";
cin >> s.Age;
cout << "Gender: ";
cin >> s.Gender;
cout << "Programming grade: ";
cin >> s.ProgrammingGrade;

students[i] = s;
}

printStudents(students, newSize);

delete[] students;
cin.get();

}

CodeBeauty
Автор

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

heikin
Автор

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.

aaronsj
Автор

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. ❤

omercandemirci
Автор

the explanation about memcpy, deep and shallow copy was 🔥

D
Автор

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

tumwesigyeisaac
Автор

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

carmelostagno
Автор

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

handover
Автор

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

lickguitars
Автор

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

xmirgvu
Автор

I'm looking forward watching the video

kamyarbasiri
Автор

great job, thank you Saldina, this is very helpful <3

swbqrkc
Автор

very helpful, thank you so much <3

deanmorrison
Автор

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

carlosalbertoavilapalacios
Автор

34:43 Practical task for you (with instructions)

Student* getInput(Student* students, int start, int end) {

for (int i = start;i < end; i++) {
cout << "enter info of STUDENT " << i+1 << " name, age, gender, grade: ";
cin >> students[i].Name >> students[i].Age >>
students[i].Gender >> students[i].ProgrammingGrade;
cout << "\n";
}

return students;
}

rezafarokh
Автор

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!

vetledeilkas
Автор

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

lpmc
Автор

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.

MikeysLab
Автор

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

nicholaskomsa
Автор

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

valjo