Results 1 to 8 of 8

Thread: Delete

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2001
    Location
    Little Rock, Ar
    Posts
    151

    Question

    The function works like I wan't it to, but when I use the delete comand it returns Junk.
    Code:
    typedef char * string;
    
    string mid(string s,unsigned int start, unsigned int length)
    {	
    	char * cBuffer = new char[length + 1];
    	string sBuffer;	
    	
    	for(unsigned int n = 0; n <= (length - 1); n++)
    	{
    		cBuffer[n] = s[n + (start - 1)];		
    	}
    
    	cBuffer[length] = '\0';	
    	
    	sBuffer = cBuffer;
    	
    	//delete [] cBuffer;
    	return (sBuffer);
    }

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    The problem is that you are allocating a buffer, then returning a pointer to it. If you delete if before you return then it'll contain junk because the pointer is invalid. If you don't delete it then the function that calls mid must make sure to delete it when it's finished or you'll get a memory leak.

    Better still, use the STL string class.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2001
    Location
    Little Rock, Ar
    Posts
    151
    How do you convert string to char *.

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    std::string x = "hello";
    
    const char *pcPtr = x.c_str();
    The const is there because you're not allowed to change the data pointed to by the pointer returned from c_str(). However, you can still use it to copy the contents out into a buffer, etc.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2001
    Location
    Little Rock, Ar
    Posts
    151
    ok, How do I convert it back.

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    char *pcStr = "Hi!";
    std::string sStr = pcStr;
    When you assign it the string is copied, so changes to pcStr will not affect sStr.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jan 2001
    Location
    Little Rock, Ar
    Posts
    151

    Thumbs down

    now I remember why I didn't want to use the string class. gets old converting it. I still want to know how to return the char * and delete it with out getting junk.

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You don't need to convert it if you use it properly. You can use it with everything as normal, and anything that needs char* can just have c_str() appended to it. The problem with returning char* is that that's only a pointer -- if you allocate memory then you get only one copy of the pointer to that memory, so after you've called the function you must delete it. This is bad programming practice because you should clean up after yourself...however if you use a mini-wrapper round it to automatically delete the string when the class is destroyed you can use it properly.

    In your case, you would use something like:
    Code:
    char *pcText;
    char *pcOther = "Here is text";
    
    pcText = mid(pcOther, 2, 6);
    cout << pcText << endl;
    
    delete[] pcText;
    ...whereas with the string class you already have a mid equivalent:
    Code:
    string sText = "Hello world!";
    string sMid;
    
    sMid = sText.substr(/*start*/ 4, /*length*/ 6);
    cout << sMid << endl;
    // No cleanup required
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width