M
Mike
Hi,
I am new to C and having problems with the following program.
Basically I am trying to read some files, loading data structures into
memory for latter searching.
I am trying to use structres and arrays of pointers to them.
I have gotten the program to compile with gcc on WinXP.
If the file i read doesnt have alot of records, it runs thru. But once
i add more, it dies. In this program i have 4 files setup to read. The
program dies on the second, the number of records it dies at varies
depending on which files i give it, usually around 30-40.
Seems like it is propably some kind of memoery issue. Sorry to post
such a big piece of code. If you guys could catch some problems i might
be having or even some suggestions for storing dynamic structures of
data. I have also tried linked list, but still seem to hit the same
problem. making me think the way i am allocating memory or using it is
causing th problems.
Thanks
#include "stdio.h"
int num_stations;
typedef struct sta {
char* name;
int num_cmpnds;
struct cmp **cmpnds;
} STA;
typedef struct cmp {
char* name;
struct blk **blocks;
} CMP;
typedef struct blk {
char* name;
char* type;
} BLK;
struct O_FILE {
int i0[2];
char spc[2];
char name[14];
int type;
int i1;
int i2;
int i3;
long wf_ptr;
int i4;
} cp_rec, cmpnd_rec;
struct cmp *new_cmp(char* name ) {
struct cmp *c = malloc(sizeof(struct cmp *));
sprintf (c->name, "%s", name);
printf ("\tCreating CMPND %s [%d]\n", name, c);
return c;
}
struct sta *new_sta(char* name ) {
int ret;
int num_rec;
int gDebug=1;
FILE *cp;
char filepath[256];
struct sta *s = malloc(sizeof(struct sta *));
if (s == NULL) return NULL;
s->name = name;
printf("Creating Station [%s] [%d]\n", s->name, s);
strcpy (filepath,name);
strcat (filepath,"/");
strcat (filepath,name);
strcat (filepath,".O");
if ((cp=fopen(filepath,"rd"))==NULL) {
printf("Error opening %s\n", filepath);
exit(1);
}
fseek(cp, 0, SEEK_END);
num_rec = ftell(cp) / sizeof(cp_rec);
printf ("Mem: [%d] [%d] [%d]\n", num_rec, sizeof(struct cmp *),
num_rec * sizeof(struct cmp *));
s->cmpnds = (struct cmp**) malloc(num_rec * sizeof(struct cmp *));
fseek(cp, 0, 0);
s->num_cmpnds = 0;
while (ret=fread((char*)&cp_rec,sizeof(cp_rec),1,cp)) {
s->cmpnds[s->num_cmpnds]=new_cmp(cp_rec.name);
s->num_cmpnds++;
if (s->num_cmpnds > 10) {
break;
}
}
num_stations++;
ret=fclose(cp);
return s;
}
void print_stations(struct sta **stations) {
int s = 0;
int c = 0;
for (s=0; s<=(num_stations-1); s++) {
printf("%0d Station [%d] [%d] #[%d]\n", s, stations->name,
stations, stations->num_cmpnds);
for (c=0; c<=( stations->num_cmpnds - 1); c++) {
printf("\t%0d %0d Cmpnd [%s] [%d]\n", s, c,
stations->cmpnds[c]->name, stations->cmpnds[c]);
}
printf("END OF CMPND
----------------------------------------------------------------\n");
}
}
int main(int argc, char *argv[]) {
struct sta **stations = (struct sta **) malloc(10 * sizeof(struct sta
*));
stations[0]=new_sta("CDCU21");
print_stations(stations);
stations[1]=new_sta("6DCU23");
print_stations(stations);
stations[2]=new_sta("CDCU21");
print_stations(stations);
stations[3]=new_sta("6DCU23");
print_stations(stations);
return (0);
}
I am new to C and having problems with the following program.
Basically I am trying to read some files, loading data structures into
memory for latter searching.
I am trying to use structres and arrays of pointers to them.
I have gotten the program to compile with gcc on WinXP.
If the file i read doesnt have alot of records, it runs thru. But once
i add more, it dies. In this program i have 4 files setup to read. The
program dies on the second, the number of records it dies at varies
depending on which files i give it, usually around 30-40.
Seems like it is propably some kind of memoery issue. Sorry to post
such a big piece of code. If you guys could catch some problems i might
be having or even some suggestions for storing dynamic structures of
data. I have also tried linked list, but still seem to hit the same
problem. making me think the way i am allocating memory or using it is
causing th problems.
Thanks
#include "stdio.h"
int num_stations;
typedef struct sta {
char* name;
int num_cmpnds;
struct cmp **cmpnds;
} STA;
typedef struct cmp {
char* name;
struct blk **blocks;
} CMP;
typedef struct blk {
char* name;
char* type;
} BLK;
struct O_FILE {
int i0[2];
char spc[2];
char name[14];
int type;
int i1;
int i2;
int i3;
long wf_ptr;
int i4;
} cp_rec, cmpnd_rec;
struct cmp *new_cmp(char* name ) {
struct cmp *c = malloc(sizeof(struct cmp *));
sprintf (c->name, "%s", name);
printf ("\tCreating CMPND %s [%d]\n", name, c);
return c;
}
struct sta *new_sta(char* name ) {
int ret;
int num_rec;
int gDebug=1;
FILE *cp;
char filepath[256];
struct sta *s = malloc(sizeof(struct sta *));
if (s == NULL) return NULL;
s->name = name;
printf("Creating Station [%s] [%d]\n", s->name, s);
strcpy (filepath,name);
strcat (filepath,"/");
strcat (filepath,name);
strcat (filepath,".O");
if ((cp=fopen(filepath,"rd"))==NULL) {
printf("Error opening %s\n", filepath);
exit(1);
}
fseek(cp, 0, SEEK_END);
num_rec = ftell(cp) / sizeof(cp_rec);
printf ("Mem: [%d] [%d] [%d]\n", num_rec, sizeof(struct cmp *),
num_rec * sizeof(struct cmp *));
s->cmpnds = (struct cmp**) malloc(num_rec * sizeof(struct cmp *));
fseek(cp, 0, 0);
s->num_cmpnds = 0;
while (ret=fread((char*)&cp_rec,sizeof(cp_rec),1,cp)) {
s->cmpnds[s->num_cmpnds]=new_cmp(cp_rec.name);
s->num_cmpnds++;
if (s->num_cmpnds > 10) {
break;
}
}
num_stations++;
ret=fclose(cp);
return s;
}
void print_stations(struct sta **stations) {
int s = 0;
int c = 0;
for (s=0; s<=(num_stations-1); s++) {
printf("%0d Station [%d] [%d] #[%d]\n", s, stations
stations
for (c=0; c<=( stations
printf("\t%0d %0d Cmpnd [%s] [%d]\n", s, c,
stations
}
printf("END OF CMPND
----------------------------------------------------------------\n");
}
}
int main(int argc, char *argv[]) {
struct sta **stations = (struct sta **) malloc(10 * sizeof(struct sta
*));
stations[0]=new_sta("CDCU21");
print_stations(stations);
stations[1]=new_sta("6DCU23");
print_stations(stations);
stations[2]=new_sta("CDCU21");
print_stations(stations);
stations[3]=new_sta("6DCU23");
print_stations(stations);
return (0);
}