Reputation: 289
I'm doing a distributed program(I'm don't know if this is the word, I learned in spanish), where I need to send and receive messages between some processes, pretty simple at first sight.
It gets complicated when I want to send two ints in the same message. With send() you can only send char* so I used this:
string s = to_string(valla) + " " + to_string(tiempo);
const char* buffNum = s.c_str();
All good until I have to receive and 'decode' the message. So if I receive one single number I can use atoi(), now the question is, how do I do this when I receive two?
Thanks in advance.
Upvotes: 0
Views: 74
Reputation: 1
const char* buffNum = s.c_str();
yields undefined behaviour dereferencing buffNum
, as soon s
is changed or goes out of scope.
To decode if you're sure your string is fine, use std::istringstream
:
std::istringstream iss(buffNum);
int num1, num2;
iss >> num1 >> num2;
Upvotes: 6