473,396 Members | 1,608 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 2536
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

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

Similar topics

14
by: Peter Olcott | last post by:
I want to be able to efficiently build data structures at run-time. These data structures need to be accessed with minimal time. The only a few ways that come immediately to mind would be some...
3
by: Tony Johansson | last post by:
Hello Experts!! When you instansiate varaibles(object) you can do so in four different scops which are. Within a block which is called Local or block scope . Within a function which is called...
5
by: nmtoan | last post by:
Hi, I could not find any answer to this simple question of mine. Suppose I have to write a program, the main parts of it are as follows: #include <blahblah.h> struct {
37
by: yogpjosh | last post by:
Hello All, I was asked a question in an interview.. Its related to dynamically allocated and deallocated memory. eg. //start char * p = new char; ...
7
by: Michael | last post by:
Hi, What's the benefit to dynamically allocate memory? using namespace std; int main() { char* ptr; ptr="abc";
1
by: akywy | last post by:
Two questions about the following code sample: --- code begins --- //class IPv4 is defined elsewhere list<IPv4> ip_list; for (int i=1; i<=9; i++) { char addr; ...
94
by: smnoff | last post by:
I have searched the internet for malloc and dynamic malloc; however, I still don't know or readily see what is general way to allocate memory to char * variable that I want to assign the substring...
6
by: bwaichu | last post by:
Is my understanding of the allocation of these correct? I used fixed sized allocations for the example below, so I realize there is some optimization that can be done to those. I would like to...
15
by: tom | last post by:
why delete the dynamically allocated memory twice causes an error, see the code below: int _tmain(int argc, _TCHAR* argv) { int *pi = new int(12); cout<<*pi; delete pi; delete pi; }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.