Thread: String duplicate function - small problem

  1. #1
    Registered User larry's Avatar
    Join Date
    Sep 2001
    Posts
    96

    String duplicate function - small problem

    I need to write function which duplicates a string. I didn't try to compile this one, but I think it works well:
    Code:
    char* clonestring(char* psource) {
      int length = strlen(psource) + 1;  //+ 1 for NULL, that's right, isn't it?
      char *pclone = new char[length]; 
      for (int i = 0; i < length; i++) pclone[i] = psource[i];  //I don't like this line :(
      return pclone;
    }
    
    //example of usage:
    int main() {
      char short[] = "a";
      char long[] =  "aaaaaaaaaaa";
      char *copyofshort = clonestring(short);  //new memory (2 bytes) allocated, same value as short
      char *copyoflong =  clonestring(long);   //new memory (12 bytes) allocated, same value as short
      return 0;
    }
    Maybe you say "why this way". My goal is - not to allocate more memory than needed. If psource is single character, so function'd allocate two bytes (character and terminator, each one byte long, that's right, isn't it?), not more.

    Can I replace the line with for statement with something shorter? Assignment or something??

    Or maybe this whole function could be written any other, more optimal way???
    Please excuse my poor english...

  2. #2
    Registered User larry's Avatar
    Join Date
    Sep 2001
    Posts
    96
    thanx
    Please excuse my poor english...

  3. #3
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    ..or you could use strcpy(), or use the C++ string class if you want to use the assignment operator.

    Also, you should delete any memory allocated with new, and short and long are reserved keywords in C++.
    zen

  4. #4
    Registered User larry's Avatar
    Join Date
    Sep 2001
    Posts
    96
    > and short and long are reserved keywords in C++.
    Don't mind this, in my code there are Czech identifiers, I just translated it to make it clear.

    > or you could use strcpy()
    You're right, I found this in help.

    > Also, you should delete any memory allocated with new
    I know I should free up the memory used, but how? I need characters in the memory allocated with "new" even after the clonestring function finishes, so where should the delete [] statement be and what variable should it delete?

    I can't understand too what to do delete if the function is called e.g. hundred times (hundred of "new" callings and memory allocatings).
    Please excuse my poor english...

  5. #5
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    I know I should free up the memory used, but how? I need characters in the memory allocated with "new" even after the clonestring function finishes, so where should the delete [] statement be and what variable should it delete?
    You need to call delete on a pointer that pointers to the allocated memory. So in your code you could call delete before the return in main on the copyof pointers -

    delete [] copyofshort;
    delete [] copyoflong;

    You must delete these pointers if you are going to use them again (unless you assign the addresses they point to, to another pointer). If you use dynamic memory you have to make sure that you don't lose this memory address before it has been deleted.
    zen

  6. #6
    Registered User larry's Avatar
    Join Date
    Sep 2001
    Posts
    96
    Really? I can't just define new string variables, send them as a parameter to clonestring function and assing the return values to pointers again? I need to delete them first? It's like calling a destructor of a class, isn't it?
    Please excuse my poor english...

  7. #7
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    I can't just define new string variables, send them as a parameter to clonestring function and assing the return values to pointers again? I need to delete them first?
    Unless you record the initial addresses somewhere else before re-assigning addresses to these pointers you'll get a memory leak. These addreses will be marked as in use, but if you no longer have a record of them somewhere you've no way of telling the system that they are free to be used again. If this was a repetitive bit of code, your program would slowly use all your system resources.
    zen

  8. #8
    Registered User larry's Avatar
    Join Date
    Sep 2001
    Posts
    96
    thanx
    Please excuse my poor english...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM