How to create a pointer to pointer array to struct? - c++

I want to create a dynamic array of pointers that each one of them points to a struct. In the program there is an option to add structs and if the counter reaches the last the value of the array, the array expands.
struct student
{
string id;
string name;
};
int N=5;
int counter=0;
student **big=new student *[N]; //a ptr to an array of ptr's.
void add_student (int &counter,student **big)
{
int i;
if (counter==0)
{
for (i=0; i<N; i++)
{
big[i]=new student;
}
}
if (counter==N)
{
N+=5;
student **temp=new student *[N];
for (i=counter-1; i<N; i++)
{
temp[i]=new student;
}
for (i=0; i<counter; i++)
{
temp[i]=big[i];
}
delete [] big;
big=temp;
}
cout<<"Enter student ID: "<<endl;
cin>>(*big)[counter].id;
cout<<"Enter student name: "<<endl;
cin>>(*big)[counter].name;
counter++;
}
When I run the program it crashes after I try to add more than one student. Thanks!

Try this code. Main problem was that you write to (*big)[counter].id without this being valid memory. In my function below a student object is created first, then writen to.
PS: I have not tested the code, let me know if it has problems with it.
struct student {
string id;
string name;
};
int N=5;
int counter=0;
student **big = new student *[N]; //a ptr to an array of ptr's.
// Variable big and counter is global, no need to pass as argument.
void add_student (student *new_student) {
// Resize if needed
if (counter==N) {
int i;
student **temp=new student *[N+5];
// Copy from the old array to the new
for (i=0; i<N; i++) {
temp[i]=big[i];
}
// Increase maximum size
N+=5;
// Delete the old
delete [] big;
big=temp;
}
// Add the new student
big[counter] = new_student;
counter++;
}
// Function called when we should read a student
void read_student() {
student *new_student = new student;
cout<<"Enter student ID: "<<endl;
cin>>new_student->id;
cout<<"Enter student name: "<<endl;
cin>>new_student->name;
// Call the add function
add_student (new_student);
}

I just tried. the error is because you are not handling pointer to pointer to a struct correctly. to pass a pointer to pointer to something means the function can change the pointer address not only the thing it points to. so declaring a pointer to pointer in function is okay but declaring a global p to p to s doesn't make much sense. You can achieve the same effect using &ptr. for p to p to s i.e. pass the address of pointer to function. I made some changes but I'm not sure if it works. I will try again after 4/5 hours and will check the issue in detail. for the time being please content yourself with the following. (might be some errors below so be careful)
struct student
{
string id;
string name;
};
int N=5;
int counter=0;
student *big=new student[N]; //a ptr to an array of ptr's.
void add_student (int &counter,student **ppBig)
{
int i;
if (counter==0)
{
for (i=0; i<N; i++)
*ppBig[i]=new student;
}
if (counter==N)
{
N+=5;
student *temp=new student [N];
for (i=counter-1; i<N; i++)
temp[i]=new student;
for (i=0; i<counter; i++)
temp[i]=*ppBig[i];
delete[] *ppBig;
ppBig=temp;
}
cout<<"Enter student ID: "<<endl;
cin>>(*big)[counter].id;
cout<<"Enter student name: "<<endl;
cin>>(*big)[counter].name;
counter++;
}

Related

Can return two arrays in struct but not in class C++

I can return two arrays by 'struct' with below codes; but can't translate the code to "class". the "class" code and error areattached also.
please shed lights on it. I have to use "class" and mutiple arrays in my project.
1) with "struct"
struct strA{
int *p;
int *p1;
};
strA setValue(int n)
{
strA strB;
strB.p=new int[n];
strB.p1=new int[n];
for (int i=0; i<n;i++)
{
strB.p[i]=i;
strB.p1[i]=i*2;
}
return strB;
}
int main(){
const int N=3;
strA strC;
strC=setValue (5);
for (int i=0; i<N;i++)
{
cout<< strC.p[i]<<endl;
cout<< strC.p1[i]<<endl;
}
return 0;
}
with "class". it turned out "error C3867: 'strA::setValue': function call missing argument list; use '&strA::setValue' to create a pointer to member"
class strA{
public:
int *p;
int *p1;
public:
strA();
~strA(){delete p, delete p1;}
strA setValue(int n);
};
strA strA::setValue(int n)
{
strA strB;
strB.p=new int[n];
strB.p1=new int[n];
for(int i=0; i<n;i++)
{
strB.p[i]=i;
strB.p1[i]=i*2;
}
return strB;
}
int main(){
const int N=3;
strA strC;
strC.setValue (N);
for (int i=0; i<N;i++)
{
cout<< strC.setValue<<endl;
cout<< strC.p1[i]<<endl;
}
return 0;
}
I will first address the error you have mentioned. There are other issues with this code as well.
The error is because of this line in main:
cout<< strC.setValue<<endl;
setValue is a function and it has to be called with arguments like this:
strC.setValue(N);
Other issues:
You cannot use cout to print the object returned from setValue
unless you have overloaded the << operator for the class strA.
In the setValue function you have defined an object strB and you assign memory to its members. This memory is not freed. What you are freeing are the members of the object strC defined in main. Look at the destructor of strA and you will understand.
The main in the first ("struct") version of the code can be used in the second ("class") version because p and p1 are public.
First, As the answer of P.W. You will meet a compile error in this line
cout<< strC.setValue<<endl; because you forgot to pass argument for the function setValue(int n).
Second, it is not suitable idea to write setValue(int n) function as what you have written. I recommend to you to write the function as following:
void ::setValue(int n)
{
this->p=new int[n];
this->p1=new int[n];
for (int i=0; i<n;i++)
{
this->p[i]=i;
this->p1[i]=i*2;
}
}
I think you are newbie and you should read more about Object Oriented Programming.

Nested structure function in c++

I'm having trouble with one of my assignments (or maybe I'm overthinking it?)
I need to create
a function to take integer parameters for number of students and tests.
Allocate the memory needed for the array of students and the array of test scores for each student.
Return a pointer to the array of Student structures. No display output is done in this function.
int main()
{
int studentNum;
int testNum;
cout << "How many students are there?\n";
cin >> studentNum;
cout << "How many tests are there?";
cin >> testNum;
system("pause");
return 0;
}
my function
GradeBook *initArrays(int s, int t)
{
GradeBook *ptr;
// Allocate the array of Student structures.
ptr = new GradeBook[s];
// Allocate an array of ints (to hold test scores)
// for each element of the array of Student structures.
for (int count = 0; count < s; count++)
{
ptr[count].tests = new int[t];
}
// Return a pointer to the array of structures.
return ptr;
}
edit: I have edited my function, could i get some opinions on that?
if you are writing this in c++, use classes. if i understand correctly, you should create a structure to save a students id,name,or something and a corresponding grade?
something like:
class Test{
public:
int id;
int grade;
Test(int id, int grade){
this->id = id;
this->grade = grade;
}
};
class Student{
public:
int id;
std::string name;
std::vector<Test> tests;
Student(int id, std::string name)
{
this->id = id;
this->name = name;
}
};
int main(){
vector<Student> students;
int studentnum;
for (int i = 0; i < studentnum; i++){
students.push_back(Student(i, "name"));
//insert all tests of the student by calling students[i].tests.push_back(Test(id, grade))
}
}
this way you don't have to allocate memory, which you can easily overlook freeing.
edit:
this is very basic and not a sophisticated solution, as the properties of the classes are all public.
edit 2:
typedef struct Test{
int id;
int grade;
}Test;
typedef struct Student{
int id;
std::string name;
Test * tests;
}Student;
int main(){
Student * students;
int studentnum;
students = (Student*)malloc(sizeof(Student)*studentnum);
for (int i = 0; i < studentnum; i++){
students[i]->id = id;
students[i]->name = "name";
student[i]->tests = (Test*)malloc(sizeof(Test)*numberofgradesofthatstudent);
for (int j = 0; j < numberofgradesofthatstudent; j++)
{
students[i]->tests[j]->id = testid;
students[i]->tests[j]->grade = grade;
}
}
}
this is schematic! new and malloc reserve memory on the heap, do not forget to free everything when you are done with it.
As said a little above, be careful using brackets {} to delimit your blocks.
Secondly,the syntax:
array[studIndex].Tests
supposes that the value array[studIndex] (here an integer) has a member value named Tests. But in this case it doesn't.
Think about your problem: you need to store two values "connected" to one another in a static array. The way I see it, you should try on with two dimensional arrays:
int 2dArray[nbStudents][nbTests];
If you don't want to bother with 2dimensional arrays, you can also try
int 2dArray[nbStudents * nbTests];
But for conveniance, it is often better to use 2d arrays.
Also, think about declaring your array before the for loops in your function.
Then concatenate two for loops as you did and I'll let you think about the rest...

Segmentation fault with dynamically allocated arrays as members of structure in c++?

I am writing a program using an array of structures to store a name, id number, and an array of test scores for a certain amount of students. Both the array of structures and the array of test scores member need to be dynamically allocated. I've gotten down to the function that allows the user to input test scores for each student, however I am having problems with the cin in the last function (getScores function). When using Linux I get a segmentation fault, so I'm assuming it has something to do with the dynamically allocated tests array that is a member of the structure, I just can't see it. I'm wondering how I can go about debugging it and an explanation of why this is actually occurring so I can avoid it in the future.
//Preprocessor Directives
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
//Structure declaration
struct Students
{
string name; //Student name
int idNum; //Student ID number
double *tests; //Pointer to an array of test scores
};
//Function prototypes
Students *getStudents(int &);
double *getTests(int &);
void getInfo(string &, int &, int);
void getScores(double &, string, int);
//Main program section
int main ()
{
int numStudents = 0;
int numTests = 0;
Students *studentFiles = NULL;
//Call the getStudents function
studentFiles = getStudents(numStudents);
//Call the getTests function
studentFiles->tests = getTests(numTests);
for(int i = 0; i < numStudents; i++)
{
//Call the getInfo function
getInfo(studentFiles[i].name, studentFiles[i].idNum, i+1);
}
for(int i = 0; i < numStudents; i++)
{
for(int j = 0; j < numTests; j++)
{
getScores(studentFiles[i].tests[j], studentFiles[i].name, j);
}
}
delete [] studentFiles;
delete [] studentFiels->tests;
return 0;
}
Students *getStudents(int &numStudents)
{
Students *studentFiles = NULL;
//Prompt the user for the number of students
cout<<"Enter the number of students: ";
cin>>numStudents;
//Dynamically allocate an array of structs, one for each student
studentFiles = new Students[numStudents];
return studentFiles;
}
double *getTests(int &numTests)
{
double *tests = NULL;
//Prompt the user for the number of tests
cout<<"Enter the number of tests: ";
cin>>numTests;
cin.ignore();
//Dynamicall allocate an array of integers, one for each test
tests = new double[numTests];
return tests;
}
void getInfo(string &name, int &idNum, int index)
{
//Prompt for each student's name and id number
cout<<"Enter the name of student #"<<index<<": ";
getline(cin, name);
cout<<"Enter the id number of student #"<<index<<": ";
cin>>idNum;
cin.ignore();
}
void getScores(double &test, string name, int numTest)
{
cout<<name<<endl;
cout<<numTest<<endl;
//Prompt for each test score for each student
cout<<"Enter "<<name<<"'s score for test #"<<numTest+1<<": ";
cin>>test;
}
One error is that you access a member of deleted object studentFiles. Reverse the lines to fix that:
delete [] studentFiles->tests;
delete [] studentFiles;
Ideally, use std::vector<> instead of dynamically allocating and releasing memory manually.
Also note, that the code only initializes Student::tests of the first member of the array, the rest of Student objects have this member uninitialized. The result of expression studentFiles[i].tests[j] is undefined and is likely to cause a crash.
You need to initialize Student::tests member of each Student. And when done, deallocate Student::tests of each Student.

How to clear memory allocated for an array that's pointed to by a pointer correctly in C++?

Before you read my code, I want everyone to know that I am not using my code anywhere which is why it doesn't have much comments. It's just an example I wrote up to ask this question and it works the way I want it to. Feel free to try it out.
I have a question about my delete function. Does delete[] pTemp->top3Grades; actually delete/free all the array items? Does it know to stop after 3 elements? How? What if some data is right next to in memory which could be the same data (or same type of data) as the first 3? Is 3 passed secretly into the delete function so it knows when to stop? Or does it just delete pTemp->top3Grades[0], the first element?
And if possible, how would I check if it was deleted? I use XCode which come with a debugger if it helps.
#include <iostream>
using namespace std;
struct Students{
int idNum;
int age;
Students* pNext;
int* top3Grades;
};
void deleteAllStudents(Students* *head){
Students* pTemp = *head;
while (pTemp!=NULL) {
*head = pTemp->pNext;
delete[] pTemp->top3Grades; //Does this eliminate all 3 elments
//or just the first? How would you
//check if it is?
delete (pTemp); //Might as well ask...how I can see
//this was deleted/freed up?
pTemp=*head;
}
}
void addStudent(Students* *head, int id, int age, int grade1, int grade2, int grade3){
Students* studentEntry= new Students;
studentEntry->top3Grades= new int[3]; // Yes I could have set this as static when
// defining it in struct up above, but
// this way is related to my question later
studentEntry->top3Grades[0] = grade1;
studentEntry->top3Grades[1] = grade2;
studentEntry->top3Grades[2] = grade3;
studentEntry-> idNum = id;
studentEntry-> age = age;
studentEntry->pNext=NULL;
Students* pTemp;
pTemp = *head;
if (*head==NULL) {
*head = studentEntry;
} else{
while (pTemp->pNext!=NULL) {
pTemp= pTemp->pNext;
}
pTemp-> pNext= studentEntry;
}
}
void dispalyAllStudents(Students* *head){
Students* pTemp;
pTemp = *head;
while (pTemp!=NULL) {
cout<<"ID #: "<<pTemp->idNum<<" Age: "<<pTemp->age<<endl;
cout<<"Best grades are "
<<pTemp->top3Grades[0]<<" "
<<pTemp->top3Grades[1]<<" "
<<pTemp->top3Grades[2]
<<endl<<endl;
pTemp= pTemp->pNext;
}
}
int main()
{
int inputNum, studentID, studentAge, bestGrade1, bestGrade2, bestGrade3;
Students* pHead=NULL;
cout<< "How many records do you want to input? ";
cin >> inputNum;
for (int i = 0; i<inputNum; i++) {
cout<<endl;
cout<<"Enter ID Number: ";
cin>>studentID;
cout<<"Enter age: ";
cin>>studentAge;
cout<<"Enter first best grade: ";
cin>>bestGrade1;
cout<<"Enter second best grade: ";
cin>>bestGrade2;
cout<<"Enter third best grade: ";
cin>>bestGrade3;
addStudent(&pHead, studentID, studentAge, bestGrade1, bestGrade2, bestGrade3);
}
cout<< "--------"<<endl;
dispalyAllStudents(&pHead);
deleteAllStudents(&pHead);
return 0;
}
Yes, delete[] knows how many elements there are if you pass it a pointer that was returned from a call of new[]. In your code, you have essentially:
int * p = new int[3];
// ...
delete [] p;
That's exactly what it is designed to do.
How does it do that? That's not really your business. As for the present example, in the quasi-standard Itanium ABI for C++, there is nothing to do, and you essentially end up with a call of free() on the underlying memory. (So just use your existing understanding of free() to fill in the blanks.) If the type of the array element was not trivially destructible, the compiler would need to store the number of array elements somewhere in some "hidden" place (which the Itanium ABI specifies) so that it can call all the destructors.

c++ dynamic classes without using vector

So I happened to finish my homework program but today during lecture my good ol professor told us we are not allowed to use STL as in vectors or list to create our database. We were also told we need all our variables private. I was doing this program completely wrong. This is what I have so far.
in class.h
class Student {
private:
string last;
string first;
int student_id;
int enroll_id;
int *grades;
}
class Gradebook {
public:
Gradebook();
void newCourse(Gradebook *info);
private:
string name;
int course_id;
int count_course;
int enroll;
Student *students;
public:
//Constructor
}
I know I can access private members of Gradebook by using a constructor so I can set every member in my Gradebook.
function to create a newCourse
Gradebook::Gradebook() {
students = new Student;
course_id=0;
count_course=0;
enroll = 0;
}
Gradebook::newCourse(Gradebook *info) {
int i, loop=0;
cout << "Enter Number of Courses: ";
cin >> loop;
info = new Gradebook[loop];
for(i=0; i<loop; i++) {
cout << "Enter Course ID: ";
cin >> info[info->count_course].course_id;
cout << "Enter Course Name: ";
cin >> info[info->count_course].name;
info->count_course++
}
}
So Courses are now sets. Since the variables in Student are private. I can't just use a pointer to the variables to access them. Can someone show me how to do this?
Ok I didnt know how to ask this question but I actually answered it. But I want everyones opinion on my method.
class Student {
public:
void setID(int ID){student_id = ID; };
int getID(){return student_id);
private:
string last;
string first;
int student_id;
int enroll_id;
int *grades;
};
class Gradebook {
public:
Gradebook();
void newCourse(Gradebook *info);
void newStudent(Gradebook *info);
private:
string name;
int course_id;
int count_course;
int count_student;
int enroll;
Student *students;
public:
//Constructor
}
void Gradebook::newStudent() {
int i, loop=0;
int student=0;
string lastName;
cout << "Enter number of students: ";
cin >> loop;
for(i=0; i<loop; i++) {
cout << "Enter Student ID: ";
cin >> student;
info->students[info->count_student].setID(student);
cout << "Enter Last Name: ";
cin >> lastName;
info->students[info->count_student].setLast(lastName);
info->count_student++;
}
}
Is there anything wrong of doing it this way?
edit: You can't use 'Student *students' for your container for multiple Students...
You could use your own lists. Something like
struct Participant{ // or class (and maybe more clever name)
Student *student;
Participant *prev;
Participant *next;
};
You have to do little pointer-acrobatics, but maybe that's the idea for this exercise..
And like in previous answer, use get and set functions in your Student class.
Ok, I'm sorry but your code is a mess...
I can't do this homework for you, but here is few tips that came in mind
Are you sure that it wouldn't be easier to make different classes for Student, Course and Gradebook? I don't know your homework specifications, so I can't be sure what it is that your program should actually do.
You can not use int *grades to store all of one students grades. Same goes for Student *students. You can not access iterate *students like an array students[i].something()
If you use some help class(or struct) like Participant, you have to find right student by iterating in loop. Notice, that you have store 'first' participant inside your class and 'int students' keep in your student count (also inside your class)
Participant *current = first;
int id = Id; // Id is from function/method call
for(unsigned int i = 0; i < students; ++i){
if(current->getId()== id){
// do something and
break;
}
else if(current->next != NULL){ // test this just in case
current = current->next;
}
else break; // something went wrong
}
It might be good idea to store all students in one list (that you make) and use pointers in Course or in Gradebook or where ever... If you know 'static' here is a hint
class Student{
public:
. // get and set functions
static Student* getStudent(int id); // return NULL if no student with id
private:
static Student *first;
static Student *last;
static unsigned int counter; // helps with loops, but you can always use next != NULL
Student *next;
Student *prev;
}
Put this inside the Student constructor
with first student that you create you set first and last to point him/her, and next and prev to NULL. After that, always when you create a new student you set
this->prev = last;
last->next = this;
last = this;
this->next = NULL;
Do not implement program logic inside class. Do it in main() or in some other function, but i think main() is fine for now. If you need to add new student in gradebook, ask all the necessary info in main() and make some function like Gradebook::newStudent(int &Id, string &last_name){ // store student info in gradebook }
Usually these homework programming exercises don't have be too fancy or streamlined or fully optimized. So don't overdo them ;)
class Student {
private:
string last;
string first;
int student_id;
int enroll_id;
int *grades;
public:
string &getLast(){ return Last; }
...
...
};
Call Student::getLast() when you need to access your last variable etc
or
void setLast(string sLast){last = sLast;} for writing and
string getLast(){return last;} for reading
And example of dynamic array:
struct Student;
int Count = 0;
Student *Students = nullptr;
int AddStudent(Student nStudent)
{
Student *Buffer = new Student[Count + 1];
for (int a = 0; a < Count; a++)
Buffer[a] = Student[a];
Buffer[Count] = nStudent;
if(Students)
delete[] Students;
Students = Buffer;
return ++Count -1
}
void RemoveStudent(int Index)
{
Student *Buffer = new Student[Count - 1];
for (int a = 0; a < Index; a++)
Buffer[a] = Students[a];
for (int a = Index; Index < Count - 1; a++)
Buffer[a] = Students[a - 1];
if (Students)
delete[] Students;
Students = Buffer;
}