English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

http://www.freewebs.com/weirdslogans/database.c

this program sets up a database of student records. But when I try to run it with a size of 3 or more, it crashes(see comment). Does anyone know why it does this and how to fix it?

2007-07-10 16:11:37 · 3 answers · asked by Anonymous in Computers & Internet Programming & Design

3 answers

You create one studentDB in main() and allocate students for the one there, but create a completely different studentDB in initDB, and never allocate any memory to whole->students.

whole->students points off into random space.

I think you want to pass database in as an argument to initDB, or if you want initDB to allocate the memory, move the creation of database and allocation of students into initDB().

2007-07-10 16:38:03 · answer #1 · answered by McFate 7 · 0 0

First, you create a new database
with the line
database=New(studentDB);
Then you allocate students with
database->students=malloc(size*sizeof(student));

Then you forget the reference to it with the line
database=initDB(size);

The initDB function allocates a database and a single student. It does not even associate the allocated student record with the database variable.

I don't know where to start.

I would make the following changes:

first in your typedefs change these lines.
typedef struct
{
string name;
string address;
string birthDate;
int mathGrade;
bool teamMember;
}*student;

typedef struct
{
student *students;
int numStudents;
}*studentDB;

to
typedef struct
{
string name;
string address;
string birthDate;
int mathGrade;
bool teamMember;
} student;

typedef struct
{
student *students;
int numStudents;
} studentDB;

Then change the InitDB to pass it the database.

There is a difference between allocating a pointer to type student and allocating a student. You are allocating typed pointer and using the pointers to pointers as if they were pointers to data.

I changed the code to this
http://www.thehansens.com/c-code/database.c

This should work better.

2007-07-11 00:37:15 · answer #2 · answered by Dave H 4 · 0 0

hard to explain i wont bother

2007-07-11 08:17:19 · answer #3 · answered by Anonymous · 0 0

fedest.com, questions and answers