Here is the code of a simple program which is supposed to read a text file which contains one word per line, dynamically allocate memory needed to store all the words, print them on the screen and deallocate the memory used.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
class Dict {
 public:
 int size;
 char ** words;
 Dict (int, int*);
 ~Dict ();
};
Dict::Dict(int s,int* sizes) {
 int i;
 size=s;
 words = new char* [s];
 for (i=0;i<s;i++)
  words[i] = new char [sizes[i]];
}
Dict::~Dict() {
 int i;
 for (i=0;i<size;i++) {
  delete [] words[i];
  printf("i=%d\n",i); // for debugging
  }
 delete [] words;
}
Dict loadDict (char* filename) {
 FILE* file;
 int n=0,i=0;
 int * sizes;
 char buff [64];
 file=fopen(filename,"r");
 while (!feof(file)) {
  n++;
  fscanf(file,"%*[^\n] \n");
 }
 sizes=new int [n];
 rewind(file);
 while (!feof(file)) {
  if (fscanf(file,"%s\n",buff)>0) {
   sizes[i]=strlen(buff);
   i++;
  }
 }
 rewind(file);
 Dict r(n,sizes);
 i=0;
 while (!feof(file)) {
  fscanf(file,"%s\n",r.words[i]);
  i++;
 }
 delete [] sizes;
 return r;
}
int main() {
 int i;
 Dict d=loadDict("dict.txt");
 for (i=0;i<d.size;i++)
 printf("%s|\n",d.words[i]);
 printf("%d DONE.\n",d.size);
 return 0;
}
The deallocating is done in the destructor of the Dict class. However, used on a sample text file with just a few words, the words are correctly printed, but the call to ~Dict crashes the application after the execution of 3 lines of the form delete [] words[i];. If I use Code::Block's debugger and set a breakpoint on that line and tell it to continue on each breakpoint, the program terminates normally.
Since this is a really simple program, I hope there is some kind of easy answer or fix!
 
     
     
     
     
    