473,320 Members | 1,732 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,320 software developers and data experts.

dynamic memory allocation

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
3 1629
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Tom | last post by:
What I mean is why can I only allocate const size stuff on the stack in C++? If I want to allocate a variable amount I need to use the OS API (Win32 in my case). Thanks, Tom.
6
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory...
6
by: Sandeep Chikkerur | last post by:
Hi, If the entire heap memory for dynamic allocation is not available, does the compiler always return NULL ? eg: char *s; s = (char *)malloc(...);
13
by: xian_hong2046 | last post by:
Hello, I think dynamic memory allocation is supposed to be used when one doesn't know in advance how much memory to allocate until run time. An example from Thinking in C++ is to dynamically...
11
by: toton | last post by:
Hi, I have little confusion about static memory allocation & dynamic allocation for a cluss member. I have class like class Bar{ public: explicit Bar(){ cout<<"bar default"<<endl; }
24
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
3
by: ranjeetasharma81 | last post by:
Hi all, I have a big C-cod, in which there are lots of dynamic memory allocation used. I want to replace dynamic memroy allocation by static arrays. The following are the problems that i am...
14
by: vivek | last post by:
i have some doubts on dynamic memory allocation and stacks and heaps where is the dynamic memory allocation used? in function calls there are some counters like "i" in the below function. Is...
10
by: swornavidhya.mahadevan | last post by:
Which allocation (Static / Dynamic) is suitable for the situation when we are trying to allocate for a overloaded memory when the memory is full and no space to allocate. What will happen if both...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.