hi every one
i have a problem with memory overlap for malloced struct.
I am trying to load dictionaries but,, after loading them i found that they have some problem
here is the example
seg.c
================
Code:
typedef struct
{
char word[30];
char pos[3];
}wordList;
wordList *epatterns1=NULL;
wordList *auxillary1=NULL;
int p_size=0,e_size=0,a_size=0;
load_dict(wordList **list1,char * filename,int *size){
FILE * fp;
char * pch;
wordList *list=NULL;
char temp_line[40];
unsigned int nrows=0; /* number of data rows in the file */
unsigned int comment=0; /* number of comment lines in file */
unsigned int i=0; /* counters */
fp = fopen( filename, "r");
if (fp == NULL)
{
printf("\nError, file not found\n");
}
else
{
// Process & close file
/* count the number of comment lines, and data lines */
while (fgets(temp_line,sizeof(temp_line),fp)!=NULL) {
if (temp_line[0]=='#') {
comment++;
} else {
nrows++;
}
}
/* allocate string array to write all the lines from the file */
*size=comment+nrows;
list = malloc((*size)*sizeof(struct wordList*));
if (list==NULL) {
printf("\nCould not allocate pointer array");
}
i=0;
rewind(fp);
memset(temp_line, 0, 30);
while (fgets(temp_line,sizeof(temp_line),fp)!=NULL) {
pch=strchr(temp_line,'\t');
strcpy(list[i].pos,pch+1);
list[i].pos[strlen(pch)-2]='\0';
strncpy (list[i].word,temp_line,pch-temp_line+1);
list[i].word[pch-temp_line]='\0';
i++;
memset(temp_line, 0, 30);
}
fclose(fp);
}
*list1=list;
}
main(argc, argv)
int argc;
char **argv;
{
load_dict(&epatterns1,"epatterns.dict",&e_size);
load_dict(&auxillary1,"auxilllary.dict",&a_size);
int i;
for (i=0; i<e_size; i++) {
printf("%s\t%s\n", epatterns1[i].word,epatterns1[i].pos);
}
}
===========
epatterns.dict
able JJ
al JJ
ance N
ate V
ation N
cy N
auxilllary.dict
am AV
are AV
be AV
been AV
can AV
when i try to print the ausillary dict stored in the wordList struct
program outputs
able
m V
re V
e V
een V
an V
please help... why this problem happened
thanks in advance
prakash