Edit [2016-08-26-001]: I've corrected the errors as pointed out in the comments. As I mentioned below, I quickly grabbed and modified some of the code just to illustrate the problem, sorry :(
The following is a modified extract from my original source (which is still a mess!!!) but it should do:
#define STORAGE_LIMIT_NAME 64
#define DB_INIT_ENTRIES 100
struct entry {
char fname[STORAGE_LIMIT_NAME];
char sname[STORAGE_LIMIT_NAME];
};
struct database {
struct entry *data;
unsigned int entries;
unsigned int entrysz;
};
struct database mydb;
mydb.entrysz = sizeof(struct entry);
mydb.entries = DB_INIT_ENTRIES;
mydb.data = malloc(mydb.entrysz * DB_INIT_ENTRIES);
free(mydb.data);
I need to know the following:
1) Am I doing it right? That is, is the memory all being allocated to the heap and thus being freed? I don't want a memory leak.
2) Is there a more elegant solution that doesn't over-complicate things and still allows easy limiting of name lengths? Also, it would be preferably fast.
Sorry if this has been asked before, I've looked all over this site and Google, but all I've found is other people occassionally doing vaguely similar things while asking completely different questions. This was not included in any of the study guides I've looked at online so far. I need to understand this if I want to get better as a programmer and I'm self-taught so I don't have a lecturer to run to. By the way, any links to good free C (not C++) study material is welcomed. Thanks lots.