Jump to content
  • Advertisement
Sign in to follow this  
link161

Help with an array of pointers to a struct and coping to another array from the heap in c++.

This topic is 2613 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, I have to write a program that in main declares an array of pointers to a structure, than passes that to a function that

dynamically allocates memory for each new element in the array and copies it to the passed array. I usually would find the answer sooner or later but this one is seriously giving me a problem.

When the code executes, I'm able to put all of the input in but then it will crash I think when it returns to main.

here is the code:

#include<iostream>
#include<string>

using namespace std;

struct Class //Class structure
{
string title;
int units;
char grade;
};

struct Student //Student structure
{
string name;
double gpa;
Class classes[10];
};

int read(Student**, int, int); //Function that reads the input and fills the students array; returns number of students inputted

int main()
{
const short S_SIZE = 100,C_SIZE = 10; //Size of max amount of students, Size of max amount of classes

int n_size = 0;
Student *students[S_SIZE] = {NULL}; // Initializing 100 Student pointers set to NULL

n_size = read(students,S_SIZE,C_SIZE);

for(int i = 0; i < n_size; i++)
{
cout<<students->name<<endl<<endl; //Just to see if it worked right
}


system("pause");
}


int read( Student** students, const int S_SIZE, const int C_SIZE) //The bulk of code is just so when you press enter it will mean you're done
{
bool quit = false;
int count = 0,csub = 0;
Student *temp[100] = {NULL};
for(int i = 0; i < S_SIZE; i++)
{
do
{
temp = new Student;
cout<<"\n\nPlease Enter Student's full Name: ";
getline(cin,temp->name);

if(temp->name!="")
{
do
{
cout<<" \nPlease Enter Class Name: ";
getline(cin,temp->classes->title);


if(temp->classes->title != "")
{
cout<<"\nEnter units for "<<temp->classes->title<<": ";
cin>>temp->classes->units;

cout<<"\nEnter Grade for "<<temp->classes->title<<": ";
cin>>temp->classes->grade;
cin.ignore();
csub++;
}
}while(temp->classes->title != "" && csub < 10);

count++;

}
else
quit = true;
}while(quit == false);

if(quit)
break;
}

for(int i = 0; i < count; i++)
{
students = temp;
}


delete [] temp;


return count;
}



Any Help is greatly appreciated :D

Share this post


Link to post
Share on other sites
Advertisement
Do you think people here would do your homework for you?

Well maybe you'll find someone to help you out.

Share this post


Link to post
Share on other sites
No, I don't want anyone to do my homework for me. I want a push in the right direction on a single subject. I know what it most look like though :/
I just can't figure this one out for the life of me.

Share this post


Link to post
Share on other sites
I didn't look at the code, but if it's crashing, start by stepping through the code in the debugger to see if you can identify the statement that's causing the crash. (If you've entered 'undefined behavior' land for one reason or another the behavior might not be entirely predicable, but stepping through in the debugger would be a good start nonetheless.)

Share this post


Link to post
Share on other sites
So I'm looking at the code... you have an array of students, fine...
Then you have an array of 'temp', fine...
You allocate memory individually for each student in 'temp', fine...
You then point the original student array to the temp array, fine...
You then delete the only allocated memory! And then try to access this memory!

Why are you deleting?

Sidenote: You are only modifying classes[0] in your read() function
Instead of classes->
Shouldn't it be classes[csub]. ?

Share this post


Link to post
Share on other sites
Ah jnmacd, That was it. But I guess my confusion came from, where am I suppose to delete the temp memory if it's local to that function?

And for the classes sub part, I haven't finished with that part of the code yet. I knew it wasn't affecting it though because I got the exact same access violation with the class part not included. I just copied the full part of my code by mistake.


Share this post


Link to post
Share on other sites
Here are some suggestions that will help you find the problem:

Make more functions. Make the code for reading into a single student a function. Make the code for reading into a single "Class" a function.

Don't allocate the "temp" array, just assign to the array you've passed in.

Ideally, you wouldn't dynamically allocate these student objects because they are not polymorphic and there is no reason to do so. This would simplify a lot of your code. Indeed, it would make more sense to dynamically allocate the array because you don't know in advance exactly how many students there are. I wouldn't worry about this at the moment try to get it working how you have it, but it is something to think about.

Share this post


Link to post
Share on other sites
The reason it's a bit crazy like this is just because it's specified in my assignment I'm doing. We can't really deviate from what is specified.

And just for the record I do understand dynamically allocated memory, and wasn't trying to get someone to do my homework, I'm just new with it and as jnmacd pointed it out I was deleting the memory I was pointing to lol so thank you all who gave the helpful comments :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!