During class i had to write simple program that uses array of structs, said program needs to add elements and display one or all elements. Teacher said that we can't use vector and that we must use pointers so I can't use something like array[0] = x
or x.name
.
Problem is that I can't access array in add_element function and after returning from or going into other function I can't access it. From what I found problem is related to realloc but i can't figure it out.
I have two days to send it to teacher so please help.
#include <iostream>
#include <stdlib.h>
#include <string>
#include <unistd.h>
using namespace std;
struct student {
string name, last_name, date_of_birth;
unsigned int index;
char gender;
};
void add_element(student tab[], int &size) {
int new_size = size + 1;
tab = (student*)realloc(tab, sizeof(student) * new_size);
student *x = new student;
cout << "set name: ";
cin >> x -> name;
cout << "set last name: ";
cin >> x -> last_name;
cout << "set date of birth: ";
cin >> x -> date_of_birth;
cout << "set gender: ";
cin >> x -> gender;
cout << "set index: ";
cin >> x -> index;
tab[size] = *x;
size = new_size;
cout << tab -> name << endl; //here i can access
delete x;
}
void display(student tab[], int *size) {
for (int i = 0; i < *size; i++)
cout << (tab + i) -> name << ' ' << (tab + i) -> last_name << ", " << (tab + i) -> date_of_birth << ", " << (tab + i) -> gender << ", " << (tab + i) -> index << endl;
}
void display(student tab[], int *size, int i) {
if (i < *size) {
cout << (tab + i) -> name << ' ' << (tab + i) -> last_name << ", " << (tab + i) -> date_of_birth << ", " << (tab + i) -> gender << ", " << (tab + i) -> index << endl;
}
}
int main() {
int size = 0;
student* students = (student*)malloc(sizeof(student) * size);
int option;
do {
cout << "\x1B[2J\x1B[H";
cout << "choose what to do\n";
cout << "0 - add element\n1 - display all elements\n2 - display specific element\n3 - exit\n";
cin >> option;
switch (option) {
case 0:
add_element(students, size);
cout << students -> name << endl; //after leaving function can't access
sleep(2);
break;
case 1:
display(students, &size);
sleep(2);
break;
case 2:
cout << "provide index for element to display\n";
int n;
cin >> n;
display(students, &size, n);
sleep(2);
break;
case 3:
break;
default:
cout << "option doesn't exist\n";
sleep(2);
}
}
while (option != 3);
delete [] students;
return 0;
}
I tried changing how i pass array into function from reference to pointer but apart from that some small changes that doesn't change anything.
std::vector
instead of raw owning array.std::cin
might be replaced by setting hard coded value, no need of menu, and so on...tab[size] = *x;
is a disaster waiting to happen.tab[size]
is raw memory; there is nostudent
object there. You cannot assign astudent
object to it. The underlying problem is the use ofmalloc
andrealloc
; they don't work well with C++ objects, because they don't initialize the allocated memory correctly. Usenew student[new_size]
to create an array ofstudent
objects.malloc
andrealloc
) you should take that as a sign that you do something wrong. If you're forced by your assignment to use pointers and explicit memory allocation, usenew
andnew[]
. And remember that arguments to functions are passed by value, meaning the function will have a copy of the value. Modifying a copy (like assigning to it) will not modify the original.