Converting std::string to char*

K

karthik.naig

Hi,


This was the routine I wrote earlier to convert a C++ string to a
char array.


But I found that the char* array consisted only of junk after returning

from the below function.


int convertStringToChar(string& str, char* data)
{
data = (char*)malloc(str.length() * sizeof(char));
memset((void*)data,0,sizeof(da­ta));
strcpy(data,str.c_str());
return 0;
}


However, I replaced it with the following function and it worked just
fine.

int convertStringToChar(string& str, char** data)
{
*data = (char*)malloc(str.length() * sizeof(char));
memset((void*)*data,0,sizeof(d­ata));
strcpy(*data,str.c_str());
return 0;
}


1. Can you tell me the reason?
2. Is the second routine defect free? Note that I'm not terminating the

char* array with a "\0" anywhere.

FYI, the compiler being used is gcc 2.96.


Thanks and Regards,
Karthik.
 
J

James Aguilar

I don't know about all that, but this solution worked just fine for me:

#include <string>
#include <algorithm>
#include <iostream>
using namespace std;

char *convertStringToChar(const string &str)
{
char *retPtr(new char[str.length() + 1]);

copy(str.begin(), str.end(), retPtr);
retPtr[str.length()] = '\0';

return retPtr;
}

int main() //test main
{
string d("acbs");

char *dPtr(convertStringToChar(d));

cout << d << endl;
cout << dPtr << endl;
}
 
V

Victor Bazarov

James said:
I don't know about all that, but this solution worked just fine for me:

#include <string>
#include <algorithm>
#include <iostream>
using namespace std;

char *convertStringToChar(const string &str)
{
char *retPtr(new char[str.length() + 1]);

copy(str.begin(), str.end(), retPtr);
retPtr[str.length()] = '\0';

I think you could also replace those two lines with

strcpy(retPtr, str.c_str());

return retPtr;
}

int main() //test main
{
string d("acbs");

char *dPtr(convertStringToChar(d));

cout << d << endl;
cout << dPtr << endl;
}

You might want to remember to 'delete[] dPtr' after you're done with it.

V
 
C

CrayzeeWulf

int convertStringToChar(string& str, char* data)
{
data = (char*)malloc(str.length() * sizeof(char));
memset((void*)data,0,sizeof(da­ta));
strcpy(data,str.c_str());
return 0;
}
This will not work because the parameter "data" is being passed by value
and any changes to it in your function will not affect the caller. You
also have a memory leak as the memory allocated by malloc() in this
function cannot be freed. This is the same reason why the following will
always print '10' not '20':

#include <iostream>

void foo(int a)
{
a = 20 ;
return ;
}

int main()
{
int b = 10 ;
foo(b) ;
std::cout << b << std::endl ;
return 0 ;
}

Thanks,
 
S

Stephen Howe

copy(str.begin(), str.end(), retPtr);
retPtr[str.length()] = '\0';

I think you could also replace those two lines with

strcpy(retPtr, str.c_str());

It is not the same. What if str contains multiple embedded '\0' ?
The former will handle that. strcpy() won't. It stops at the first '\0'.

Stephen Howe
 
V

Victor Bazarov

Stephen said:
copy(str.begin(), str.end(), retPtr);
retPtr[str.length()] = '\0';

I think you could also replace those two lines with

strcpy(retPtr, str.c_str());

It is not the same. What if str contains multiple embedded '\0' ?
The former will handle that. strcpy() won't. It stops at the first
'\0'.

And what use would the resulting pointer would be if it contains
multiple embedded null chars? The first embedded null character
will terminate it to any of C string functions, for example.
 
J

James Aguilar

Victor Bazarov said:
And what use would the resulting pointer would be if it contains
multiple embedded null chars? The first embedded null character
will terminate it to any of C string functions, for example.

Also, the OP specifically says the string contains no null characters.

- JFA1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top