CompuServe Thread

#MAllocate help

5 messages in this thread
#44417From: Henry WilliamsNov 30, 1994 9:05 PM
Hello people, I'm trying to allocate memory and fill it at the start of the program. When I get to the printf statement my array elements come out null. Does anyone have an idea what is wrong here. The data in my Names file is ok. Thank you for your advice. Henry #include <stdio.h> struct record{ char name[36]; int family; }; struct record *array; main() { FILE *in; int a=0, n=1; if ((array = (struct record *) malloc(33300)) == (FILE *) NULL){ fprintf (stderr, "\n Couldn't allocate memory!\n"); exit(10); } if (( in = fopen ("Names", "r")) == (FILE *) NULL ){ fprintf (stderr, "\n Couldn't read Name file!\n"); exit(10); } while (n == 1){ n = fread ( &array[a], sizeof (struct record), 1, in ); printf (" %-36s %d\n", array[a].name, array[a].family); a++; } free (array); fclose (in); }
#44427From: Theo KlaassenDec 1, 1994 5:07 AM
Hello Henry, like I see your Programm, there is the bug in the while-loop. Because you only do have a pointer on a structure and not a vektor of pointers on that structure. Even you can't copy all the datas at once into a structure, you have to do it for each element in the structure. example : struct record *array[VALUE]; … while( n==1 ) { /* this is only for example */ fgets( name, 256, in ): fgets( family, 256, int ); strcpy(array[n]->name, name ); array[n]->family = atol( family ); …. n++; /* but I am not sure, if it works with the allocated memory */ } And finaly how I do understand your Programm, you want to have a couple of entries, which can be accssed by the arraynumber. But that is not the classical way how to manage that problem. ( see K&R The C Programming Language Chapter 6.3 and 6.4 ). I would use that kind of structure : struct record { struct record *succ; struct record *pred; char name[36]; int family; int nummer; } And then build a list. |======== | |=========| |=========| |1st record | ——> | 2nd record| ——> ….. —–> |nth record | | ========| <—– |========= | <—– <—– |=========|
#44428From: Mal LansellDec 1, 1994 8:46 AM
Theo, There is no bug in the while loop (apart from the last printf when n==0). Struct record *array is treated in the program as an array of record structs. Therefore array[n].name is the correct notation to use. Henry's code works fine if you compile it, as long as you keep the size of the malloc below 32768. If I remember correctly, the malloc function takes a signed int as its argument, so his compiler must be taking 33300, treating it as a signed int (ie -32236), and then allocating a memory area of 32236 bytes. This would account for the last few values being corrupted, and causing the crashes that he complained of a few days ago. The solution is either to have a smaller array, or to allocate the memory as part of the program with struct record array[892]; The rest of the program can remain unchanged. Mal Binary Dreams
#44659From: Theo KlaassenDec 6, 1994 6:40 AM
Hi Mal, I red your answer, and you are right. I never compiled the source, because I was sure that this would not work. I see that there is still something to learn for me. Good work ! see you Theo
#44429From: Mal LansellDec 1, 1994 8:47 AM
Henry, See my reply to Theo's response for another explanation. Mal Binary Dreams