Hi guys. I was wondering if you could help me with this problem. I am trying to pass a dynamic array to a function for finding the length

int length(char ** t, int i) {
    int count = -1;
    do
     count++;
    while (t[i][count] != '\0');
    return count;
}

The function always returns a zero value.
As a result the following code for copying the values of arrays doesnt work either

class KMP : public StM {
public:
	  KMP();
      KMP(char **, char *, int index, int sz);
//      ~KMP();
      virtual int operator ++();

      char ** pattern;
      int * prefix;
	  int index;
};
KMP::KMP(char ** p, char * t, int i, int size)  {

    text = t;
    pattern = new char*[size];
    pattern = p;

   ( pattern[i] = new char[length(p, i)] )= p[i];
   index = i;

    patlen = length(p, i);
    prefix = new int[patlen];

    prefix[0] = 0;
    for(int i=1; i < patlen; i++) {
        int k = prefix[i-1];
		while ((pattern[i] != pattern[k]) && (k != 0)) {k = prefix[k-1]; /*count++;*/}
    if(pattern[i] == pattern[k]) prefix[i] = k+1;  else prefix[i] = 0;
	//count++;

}
}
int KMP::operator ++() {
  int mp = length(text);
  int cp = pos + 1;
  int pp = 0;
  for(; cp < mp; cp++)
  {
	  while( (pp>0) && (pattern[index][pp] != text[cp]) ) {pp = prefix[pp-1]; count++;}
    if(pattern[index][pp] == text[cp])
    {
      pp++;
      if(pp == patlen){position(1+cp-patlen); cout << pos << "\t"; }
    }
	count++;
  }
  position(-1);
  return 0;
}

If there is antything wrong with the code please point it out, or maybe if you know about a function that copies values from a dynamic array to a static one please let me know. thanks

Recommended Answers

All 2 Replies

Please, explain these evidently wrong statements:

pattern = new char*[size];
    pattern = p;

   ( pattern[i] = new char[length(p, i)] )= p[i];

See what happens:
- You create uninitialized array of pointers to char.
- The next statement overwrite a pointer to this array (memory leak) by the parameter p value.
- The 3rd statement is the most interesting because it do the same destructive performance in all-in-one manner: it allocates a memory chunk of undefined size (no check for length(...) <= 0) then save an obtained pointer value to pattern[i] element then (immediately) overwrite this value by p[i] . Thank God, the effect of "only even number of errors gets correct result" principle works: pattern and p pointers points to the same memory (see statements above ;)...

Please, explain these evidently wrong statements:

pattern = new char*[size];
    pattern = p;

   ( pattern[i] = new char[length(p, i)] )= p[i];

See what happens:
- You create uninitialized array of pointers to char.
- The next statement overwrite a pointer to this array (memory leak) by the parameter p value.
- The 3rd statement is the most interesting because it do the same destructive performance in all-in-one manner: it allocates a memory chunk of undefined size (no check for length(...) <= 0) then save an obtained pointer value to pattern[i] element then (immediately) overwrite this value by p[i] . Thank God, the effect of "only even number of errors gets correct result" principle works: pattern and p pointers points to the same memory (see statements above ;)...

Thanks a lot. That solved the problem. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.