By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,504 Members | 1,724 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,504 IT Pros & Developers. It's quick & easy.

dynamic memory allocation

P: n/a
I need to dynamically allocation memory at run time for the number of
student's records and their test scores for the program code below. I don't
understand what the 3 errors i got. I can anyone help.

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

// structure declaration
struct student_record
{
char name[20];
int test_scores;
float average_ts;
};

// function prototypes passing structure into function
void get_dynamic_testscores(int , int , int );
void main()
{

int number_records, number_testscores, *records;
get_dynamic_testscores(number_records, number_testscores, &records);
student_record student[records];

}
void get_dynamic_testscores(int number_records, int number_testscores, int
*records)
{
//int number_records, number_testscores;
cout<<"How many student do you want to average test scores? : ";
cin >>number_records;
records = new int[number_records];
cout<<"How many do you want to calculate average for this student? : ";
cin >>number_testscores;
//another dynamically allocation memory here just like records = new
int[number_records];

}
Nov 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Richard wrote:
// function prototypes passing structure into function
void get_dynamic_testscores(int , int , int );
void main()
{

int number_records, number_testscores, *records;
get_dynamic_testscores(number_records, number_testscores, &records);
The prototype expects an int as third parameter, you are passing int*
also, it makes no sense to pass uninitialized local variables by value.

student_record student[records];
The size of the array should be known at compile time. This is
non-standard code.
Further, the array size must be an integral constant, not a pointer.

}
void get_dynamic_testscores(int number_records, int number_testscores, int
*records)
{
//int number_records, number_testscores;
cout<<"How many student do you want to average test scores? : ";
cin >>number_records;
records = new int[number_records];
cout<<"How many do you want to calculate average for this student? : ";
cin >>number_testscores;
//another dynamically allocation memory here just like records = new
int[number_records];


you are usung number_records again. Did you actually mean
nunber_testscores?

as an aside, this program suffers from memory leaks etc. The pointers
to the allocated memory will be lost when control returns to main.

Nov 22 '05 #2

P: n/a
Richard wrote:
I need to dynamically allocation memory at run time for the number of
student's records and their test scores for the program code below. I don't
understand what the 3 errors i got. I can anyone help.

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

// structure declaration
struct student_record
{
char name[20];
int test_scores;
float average_ts;
};

// function prototypes passing structure into function
void get_dynamic_testscores(int , int , int );
void main()
{

int number_records, number_testscores, *records;
get_dynamic_testscores(number_records, number_testscores, &records);
student_record student[records];

}
void get_dynamic_testscores(int number_records, int number_testscores, int
*records)
{
//int number_records, number_testscores;
cout<<"How many student do you want to average test scores? : ";
cin >>number_records;
records = new int[number_records];
cout<<"How many do you want to calculate average for this student? : ";
cin >>number_testscores;
//another dynamically allocation memory here just like records = new
int[number_records];

}


Well you're doing the right thing by writing small amounts of code and
trying to get that working before moving on. But you've going some
learning to do about passing and returning information from a function.
And also about designing suitable functions.

I think this code should be rewritten, but here's some tips

When you write a function think about what information needs to passed
into the function from outside. These become function parameters.

When you write a function think about what information the function will
return to the outside. This (usually) becomes the function return type.
Newbies often just try to write void functions, remember functions often
return values.

When you write a function think about what information the function will
use internally. These become local variables.

Looking at the code you wrote above I can see that you made several
wrong chioces. For instance 'number_of_students' is something that the
function asks for inside the function. Now this is possibly a right or
wrong choice. But having made that choice it means that this information
is not being passed in from outside, it is internal to the function, so
'number_of_students' becomes a local variable, not a parameter. To be
fair you do have it as a local variable but you have commented it out.

Also I think you are confused about what this function will actually do.
You ask for the number of students, but then you ask for the test scores
for one student. That doesn't make sense. I would write a function that
gets the test scores for one student first. Test that and get it
working. You can then put that function inside a loop so you get scores
for many students. Write that code once you've got the first part working.

john
Nov 22 '05 #3

P: n/a

"Richard" <no********@yahoo.com> wrote in message
news:-Y******************************@comcast.com...
I need to dynamically allocation memory at run time for the number of
student's records and their test scores for the program code below. I
don't understand what the 3 errors i got. I can anyone help.


If you want to know what those three errors mean, it would help if you told
us what the error messages were. Also, tell us which line of code they
refer to (and not by line number, but by putting a comment in the code you
post that say something like "<---all three errors refer to this line of
code").

-Howard
Nov 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.