|
-
Mar 26th, 2001, 03:39 AM
#1
Thread Starter
Addicted Member
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);
}
-
Mar 26th, 2001, 12:40 PM
#2
Monday Morning Lunatic
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
-
Mar 26th, 2001, 03:05 PM
#3
Thread Starter
Addicted Member
How do you convert string to char *.
-
Mar 26th, 2001, 03:21 PM
#4
Monday Morning Lunatic
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
-
Mar 26th, 2001, 09:01 PM
#5
Thread Starter
Addicted Member
ok, How do I convert it back.
-
Mar 27th, 2001, 06:12 AM
#6
Monday Morning Lunatic
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
-
Mar 27th, 2001, 03:52 PM
#7
Thread Starter
Addicted Member
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.
-
Mar 27th, 2001, 04:10 PM
#8
Monday Morning Lunatic
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|