dynamic memory allocation

R

Richard

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];

}
 
N

Neelesh Bodas

Richard said:
// 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.
 
J

John Harrison

Richard said:
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
 
H

Howard

Richard said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

dynamic memory 1
dynamic memory 2
Dynamic programming 3
Dynamic allocation of memory 7
Lexical Analysis on C++ 1
Chatbot 0
Memory allocation problem 3
memory allocation using new 1

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top