I want to read some words until the word "done" is introduced. These words are put into a dynamically allocated matrix,which is modified at each input received . The problem is that after 3 words the program crashes and the message with "Memory allocation failed" that i put as a warning appears. What is wrong with my reallocation ?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_DIM 100
int main()
{
char **words,buff[100];
int i,dim = 0;
words = (char**)calloc(dim,dim*sizeof(char*));
while(strcmp(buff,"done"))
{
printf("the new word : ");
scanf("%100s", buff);
if(strcmp(buff,"done"))
{
dim++;
words = (char**)realloc(words,dim*sizeof(char*));
if(words == NULL)
{
printf("Memory allocation failed !\n");
exit(0);
}
words[dim] = (char*)calloc(MAX_DIM,sizeof(char));
strcpy(words[dim],buff);
}
}
printf("%d", dim);
for (i = 0;i < dim;i++)
free(words[i]);
free(words);
return 0;
}