M
mdrons
I have a structure defined as:
struct channel {
int chanid;
int channum;
char callsign[20];
char name[64];
};
I then have a global variable defined as:
static struct channel *chan=NULL;
I then malloc the space when I know the total number of rows (dynamic
variable Total_rows) I have:
if ( (chan=(struct channel*)malloc(sizeof(struct channel)*Total_rows))
=
= NULL) {
snprintf(buf, sizeof(buf),"Mallor Error.\n" );
free (chan);
goto out;
}
My issue as I fill the structure the program seg faults. If I change
the line to
if ( (chan=(struct channel*)malloc(sizeof(struct
channel)*Total_rows*sizeof(struct channel))) =
= NULL) {
It works, but I now have sizeof twice.... or if I make the Total_rows
variable 3 times as large.
Can anyone tell me what I am doing wrong?
Thanks Mike
struct channel {
int chanid;
int channum;
char callsign[20];
char name[64];
};
I then have a global variable defined as:
static struct channel *chan=NULL;
I then malloc the space when I know the total number of rows (dynamic
variable Total_rows) I have:
if ( (chan=(struct channel*)malloc(sizeof(struct channel)*Total_rows))
=
= NULL) {
snprintf(buf, sizeof(buf),"Mallor Error.\n" );
free (chan);
goto out;
}
My issue as I fill the structure the program seg faults. If I change
the line to
if ( (chan=(struct channel*)malloc(sizeof(struct
channel)*Total_rows*sizeof(struct channel))) =
= NULL) {
It works, but I now have sizeof twice.... or if I make the Total_rows
variable 3 times as large.
Can anyone tell me what I am doing wrong?
Thanks Mike