C++ - string to const char*

I'm writing a winsock send function that sends data that is passed with a string. I need to use the string because I'm converting it into binary (our server protocol, don't ask).

Here's my code:
1
2
3
4
5
6
void Connection::Send(string buffer){
	const char* cbuff = buffer.c_str();
	send(Connection::sock,cbuff,strlen(cbuff),0);
	cout << "Data Sent: " << buffer << endl;
}


I placed a breakpoint on const char* cbuff and it said:
cbuff -- 0xcccccccc <Bad ptr> 


I guess it fails when I convert the string into a cstring. I don't know.
Nothing is wrong with cbuff. You were just breaking too early (cbuff won't be a valid pointer until after that line).

Is send a blocking call? If not it's possible the pointer is going bad before the data is actually sent.
Yeah, I was breaking too early. Thanks.
Additional info : 0xCCCCCCCC is a special memory value for uninitialized stack memory in Microsoft's debugger.
Other codes in pointer or memory can tell you the source of a problem. See http://en.wikipedia.org/wiki/Magic_number_%28programming%29
Topic archived. No new replies allowed.