PDA

View Full Version : c memory allocation question


pepsi_max2k
03-15-2005, 10:19 PM
Hi guys, i'm a n00b c programmer trying to produce somthing for a uni assignment, just wondered if you could look over the following and let me know if it's ok...

/* Allocate memory to create entry. */
current_entry = (struct entry *) malloc(sizeof(struct entry));

current_entry_ptr = current_entry;


/*
* Get competitor details until end of the file is reached.
* Add each set of details to an entry strut.
*/

while(!feof(input_file)) {

fgets(current_name, MAX_INPUT_LINE_SIZE, input_file);
strcpy(current_entry_ptr -> name, current_name);


/* Unless end of file has been reached, add struc to array. */
if (!feof(input_file)) {
current_entry_ptr++;
}
}


basically its part of my program where i'm trying to create numerous entries (structures) using alocated memory. eventually they'll form part of a binary tree but i wanna make sure this lot's ok first. it's all working and everything, but i just wondered if the memorie's actually being allocated correctly, cos from what i can see it allocates enough for one entry, adds details to it via the current_entry_ptr, then increments current_entry_ptr to create a new entry, but i'm not sure if that means it gets allocated more memory or not. i'm kinda confused by it all. still, java's worse :p and i'm still not sure how to store a tree :rolleyes:

Unit
03-15-2005, 11:48 PM
You need to allocate a new entry each time you want to add one.

Another way is to allocate the memory sufficient for all the entries at the beginning.

int no_of_entries = 200;
/* Allocate memory to create entry. */
current_entry = (struct entry *) malloc(sizeof(struct entry)*no_of_entries);

current_entry_ptr = current_entry;

/*
* Get competitor details until end of the file is reached.
* Add each set of details to an entry strut.
*/

int count=0;
while(!feof(input_file)) {

fgets(current_name, MAX_INPUT_LINE_SIZE, input_file);
strcpy(current_entry_ptr -> name, current_name);
count ++;
/* Unless end of file has been reached, add struc to array. */
if (!feof(input_file)) {
current_entry_ptr++;
}
/* break if you used up all the entries that you allocated. */
if (count>=no_of_entries)
break;
}

Also, I assume the structure has name defined as a char array. If not, you have to allocate memory for that too!

Also, do not forget to free this memory when you are done using it.

you can free it by using
free(current_entry);

pepsi_max2k
03-16-2005, 07:18 AM
ah yes, should've mentioned the size... "You must not ``hard code" the number of competitors into your program." ;) originally i tried with the allocation within the while loop, but i think that overwrote the previous entry each time it ran, at least didn't work anyway...

here's the full structure, i left out most of the it in the previous code to avoid complications.

/* Structure to contain competitor's entry details. */
struct entry {
char name[79];
char address[79];
char telephone[79];
int competitor_num; /* Automatically generated by program. */
int river_weight;
int sea_weight;
int fly_weight;
struct entry *left_node;
struct entry *right_node;
};

i've got error checking for the allocation and memory freeing in there too.

aman
03-16-2005, 10:17 AM
Theres a pointer to left_node and right_node, so you should probably be googling up the terms "doubly linked list" for information on allocating memory for this structure.