0
struct FF{

   void append( char *str ){

         const int strLength = strlen( str );
    const int totalLength = strLength + size;
    char *tmp;
    if( freeSpace < strLength ){
        //not enough free space available for the str to append
        //allocate the total str length including the current size + free space
        tmp = new char[totalLength + 10];
        freeSpace = 10;

        //cpy old string and new string to tmp
        for( int i = 0; i < size; i++ ){
            tmp[i] = strVal[i];
        }
        for( int i = size; i < totalLength; i++ ){
            tmp[i] = str[i];
        }

        delete[] strVal;
        strVal = new char[totalLength+10];
        size = totalLength;
        strcpy( tmp, strVal );

    }else{
        for( int i = size; i <= totalLength; i++ ){
            strVal[i] = str[i];
        }

        freeSpace -= strLength;
        size += strLength;

    }


   }
   char *strVal;      
  unsigned int size;
  unsigned int freeSpace;

};


int main(){

FF a;
a.strVal = new char[10];
a.freeSpace = 10;
a.size = 0;

a.append( "str" ); // should have 7 bytes left
a.append( "len" ); // should have 4 bytes left

std::cout << a.strVal << std::endl; //prints str instead of strlen

return 0;
}

I want the strVal to have free space so I don't have to allocate space everytime I append something to it. However, the first append works just fine. But when I append it another time, it does not work. So at the end only str will be printed out.

5
  • I didnt read the script much, but if you use char* for strings, and you are using c++, consider using std::string Commented Nov 4, 2014 at 12:06
  • @Creris I know that std::string exists, this is for educational purpose only. Commented Nov 4, 2014 at 12:07
  • why not educate with std::string, it does automatic allocation, and you can just call myString.reserve(myString.size() + 10); Commented Nov 4, 2014 at 12:09
  • @Creris Do I know how that works ? No, so this won't help me understand how to implement a string with free space. Commented Nov 4, 2014 at 12:12
  • strcpy( tmp, strVal );... which is dest and which is source? Commented Nov 4, 2014 at 12:15

2 Answers 2

2

The problem is, that the else clause has to be changed to:

...
}else{
    for( int i = 0; i <= strLength; i++ ){
        strVal[i+size] = str[i];
    }

    freeSpace -= strLength;
    size += strLength;

}
Sign up to request clarification or add additional context in comments.

1 Comment

As long as you're changing that else code (which is far from the only problem), may as well simply use std::copy(str, str+strLength+1, strVal+size)
1

You have a problem in the second for loop:
... for( int i = size; i < totalLength; i++ ){ tmp[i] = str[i]; }

Index i is okay for tmp, but not for str:
... for( int i = size; i < totalLength; i++ ){ tmp[i] = str[i<b> - size</b>]; }

Afterwards, you don't need to allocate a new buffer for strVal and copy from tmp (again): Simply assign tmp to strVal.
That get's rid of the strcpy(), where the order of parameters is wrong as already indicated by @doctorlove.

Finally, you have a memory leak: You allocate a char array for tmp but never release it. If you change the handling as described above, you get rid of that too.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.