Following is a function I wrote to get a line of definite length from a long string. The line is also word-wrapping to make sure no words are splitted:

char * getLine(char * str, int length, int start, int lenSent){
     int index = 0, //Index number of the current Word 
          i = 0, j = 0,//Local Index
          len = length, //Maximum allowed length of the line
          lineLength = 0; //Length of the current line

     //Character pointers
     /***************************************************************/
     //Line containing data to be returned to the calling program
     char * line;
     try {
          line = new char[length + 5];
     }
     catch (std::bad_alloc){
          cerr << "Error creating blank line for getLine f(x)" << endl;
     }

     //Temperory characater array pointer holding the data from which line is extracted
     char * temp;
     try {
          temp = new char[length + (length * start) + 10];
     }
     catch (std::bad_alloc){
          cerr << "Error creating temp array for getLine f(x)" << endl;
          exit(EXIT_FAILURE);
     }

     /***************************************************************/    
     //Populate the 'temp' array with usefull data
     for(i = lenSent, j = 0; i < length + (length * start) + 10; i++, j++)
          temp[j] = str[i];
     temp[j] = '\0'; //End the string

     /***************************************************************/
     //Loop till the line length is equal to or less than that of the allowed length of the line
     while(lineLength <= length){

          //get a single word, at position 'index' from the temperory string 
          char * word = getWord(temp, len, index); 

          //If empty word is returned i.e. word is either longer than the allowed length,
          //or end of the string was reached, then exit the loop
          if(strlen(word) == 0)
               break; 

          //Append 'word' to the line
          for(i = lineLength, j = 0; i < lineLength + strlen(word); i++, j++)
               line[i] = word[j];

          //Increase length of the line by the length of the word and the follwoing space
          lineLength += strlen(word) + 1;

          //Insert space after the word just added to the line
          line[i] = ' ';

          //Decrease the allowed length, i.e. the number of character that can still fit the line,
          //by the length of the word added and one space.
          len -= strlen(word) + 1;

          //Increase the index of the word
          index++;

          //Delete the character pointer to word.
          delete word;
     }

     //End the line with a null byte
     line[lineLength] = '\0';

     //Delete the temporary pointer 'temp'
     delete temp; //<- This causes a HEAP CORRUPTION

     //exit and return line
     return line;
}

Can someone tell me what I'm missing here thats screwing up the temp variable and corrupting heap?

Thanks

line 71 needs to be delete[] temp;

line 71 needs to be delete[] temp;

Already tried it, still got the Heap Corruption.

Post some sample code that calls that function. Lines 30 and 31 could very well be reading beyond the end of str[].

The string that I used is this:

char page1[ ] =
      "Once there lived a village of creatures along the bottom of a great crystal "
      "river. The current of the river swept silently over them all -- young and old, "
      "rich and poor, good and evil, the current going its own way, knowing only "
      "its own crystal self. Each creature in its own manner clung tightly to the "
      "twigs and rocks of the river bottom, for clinging was their way of life, and "
      "resisting the current what each had learned from birth. "
      "But one creature said at last, 'I am tired of clinging. Though I cannot see "
      "it with my eyes, I trust that the current knows where it is going. I shall "
      "let go, and let it take me where it will. Clinging I shall die of boredom.' "
      "The other creatures laughed and said, 'Fool! Let go and that current you "
      "worship will throw you tumbled and smashed across the rocks, and you will "
      "die quicker than boredom!' But the one heeded them not, and taking a breath "
      "did let go, and at once was tumbled and smashed by the current across the rocks. "
      "Yet in time, as the creature refused to cling again, the current lifted him "
      "free from the bottom, and he was bruised and hurt no more. "
      "And the creatures downstream, to whom he was a stranger cried, 'See a miracle! "
      "A creature like ourselves, yet he flies! See the Messiah come to save us all!' "
      "And the one carried in the current said, 'I am no more Messiah than you. The "
      "river delights to lift us free, if only we dare let go. Our true work is this "
      "voyage, this adventure.' But they cried the more, 'Saviour!' all the while "
      "clinging to the rocks, and then when they looked again he was gone, and they "
      "were left alone making legends of a Saviour. --Richard Bach--";

Following is the loop that utilises this function, this is present in the constructor of the TextField Class:

//lenSent -> is the number of characters that have been sent in lines
//maxLen is the strlen(page) which checks if all the characters have been put into lines.
for(int i = 0; lenSent < maxLen; i++){

          //Get a 'line' and store in the current line array.
          lines[i] = getLine(page, width, i, lenSent);

          //Increase the length of characters sent by the length of the current line.
          lenSent += strlen(lines[i]);

          //Increase the line count by 1
          lineCount++;
     }

The getWord function inside get line simply gets a single word from the line. Using Visual Studio 2008 I was able to confirm that atleast the address of the variable temp is never changed. I am still making changes and trying to make it work. If I am able to do something, I'll post up.

Thanks for the reply Ancient (Sorry dont know your real name to Thank you properly.).

Sid

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.