468,765 Members | 950 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,765 developers. It's quick & easy.

Dynamically Allocated Memory

Have been working on this project for a week, unfortunately missed the lesson on it and have been struggling to figure it out.

These are the instructions and the point at which I'm up to.

Specify a structure called Date with day, month and year as its three int members.

Specify a structure called Student with the following data:
name: string which could include spaces for last_name, first_name
id: int
units: int
gpa: float
date: Date

Keep reading Student structures from keyboard until a NULL string is entered for student name. As each Student is entered, dynamically allocate memory for it and store its pointer returned by new in an array of Student pointers.
So essentially we don't know the size of the array and the user doesn't specify size, it's all done on the fly.

Point I'm up to:

#include<iostream>
#include<string>
using namespace std;

struct Date
{
int month, day, year;
};

struct Student
{
string first_name, last_name;
int id, units;
float gpa;
Date date;
};

int main()
{
Student *std;
string test_string;
int count = 0;
char slash;

std = new Student;

cout << endl << "First Name: ";
getline(cin, test_string);

while(test_string != "")
{
std->first_name = test_string;

cout << "Last Name: ";
getline(cin, std->last_name);
cout << "Student ID: ";
cin >> std->id;
cout << "GPA: ";
cin >> std->gpa;
cout << "Enrolled [mm/dd/yyyy]: ";
cin >> std->date.month >> slash >> std->date.day >> slash >> std->date.year;
cin.ignore();

cout << endl << "First Name: ";
getline(cin, test_string);

if(test_string != "")
{
count++;
std = new Student;
}
}

return 0;
}
What I can't figure out is where or how to stick the dynamically allocated memory continually back into an array of student pointers.
Dec 3 '06 #1
3 2254
DeMan
1,806 1GB
I think the intention is for the memory for each "Student" to be allocated (look for a tutorial on malloc() if you need more help to do this), and the pointer to each student can be stored in an array of pointers(which so long as you know how many students, can be of a fixed size).

If you don't know how many students one (not necessarily efficient) method to Dynamically change the size of an array, is to initially fix it to a given size (say 100), and if we manage to reach that size, replace it with a new (bigger) array, copying each of the elements accross. I'm sure there is a neater solution (It sounds like you can't use a data structure other thanan array), which someone else may be able to suggest......
Dec 3 '06 #2
sivadhas2006
142 100+
Have been working on this project for a week, unfortunately missed the lesson on it and have been struggling to figure it out.

These are the instructions and the point at which I'm up to.



So essentially we don't know the size of the array and the user doesn't specify size, it's all done on the fly.

Point I'm up to:



What I can't figure out is where or how to stick the dynamically allocated memory continually back into an array of student pointers.
Hi,

Try to use linked list to achieve your goal.

Regards,
M.Sivadhas.
Dec 4 '06 #3
horace1
1,510 Expert 1GB

What I can't figure out is where or how to stick the dynamically allocated memory continually back into an array of student pointers.
as you don't know how much data the best thing would be to use some dymnamic data structure like a vector. However, the specification states that you have to use an array so the first thing is to allocate an array of a specific a size, say 100, and if you exceed that stop reading. When that is working you could add code to create a new larager array, copy the existing data into it, and then carry on reading.
Dec 4 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

14 posts views Thread by Peter Olcott | last post: by
3 posts views Thread by Tony Johansson | last post: by
6 posts views Thread by bwaichu | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.