473,698 Members | 2,932 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamically Allocated Memory

1 New Member
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<iostre am>
#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_stri ng != "")
{
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 2554
DeMan
1,806 Top Contributor
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 New Member
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 Recognized Expert Top Contributor

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
3544
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 sort of dynamically allocated array of : (1) void pointers, that must be cast into the desired types. (2) union of the desired pointers. (3) An Inheritance hierarchy (4) Possibly a union of the desired data types.
3
3609
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 function scope. Whithin a class which is called class scope. Outside a function which is called global or file scope. Now to my question when you allocate memory dynamically you can't say
5
5592
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
2763
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
2805
by: Michael | last post by:
Hi, What's the benefit to dynamically allocate memory? using namespace std; int main() { char* ptr; ptr="abc";
1
1712
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; sprintf(addr, "%d.%d.%d.%d", i,i,i,i);
94
4729
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 that I found inside of a string. Any ideas?
6
2623
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 use these in a linked list for something else I am working on, but I want to make sure my understanding of the concept is correct. For example, from the code below string would equal 'h' after
15
1787
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
8683
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8611
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9031
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8876
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7741
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6531
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5867
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4624
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.