malloc() memory corruption

Hi to all!!.. I have a problem sometimes in this function..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
int readStopList(FILE *stopFile, char ***stopList)
{

  int w;
  char wrd[MAX_STR_LEN]; // #define MAX_STR_LEN 128 (defined above)
  /* count words in stoplist */
  w=0;
  while (nextWord(stopFile,wrd)!=EOF) w++;

	*stopList=(char **)calloc(w,sizeof(char*));

	rewind(stopFile);
	w=0;

  while (nextWord(stopFile,wrd)!=EOF) {

		unsigned int c;

		(*stopList)[w]=(char *)calloc(strlen(wrd),sizeof(char)); // Here's the problem

		for (c=0; c<strlen(wrd); c++) (*stopList)[w][c]=wrd[c];
		(*stopList)[w][c]='\0';

      w++;
    }
  return w;
}


The problem seems to be in calloc () but I tried to free() and delete[] "wrd" and "stopList" but nothing.. It is very rare, sometimes works and sometimes not. Please any help...

Cheers

Mac
It seems that in thsi statement

(*stopList)[w]=(char *)calloc(strlen(wrd),sizeof(char));

you should use (strlen(wrd) + 1 instead of (strlen(wrd).
Hi my friend vlad from moscow... That seems to have solved the problem, I'll keep it under observation a few hours :)

Thanks!!...

Mac

Topic archived. No new replies allowed.