我正在通过一个开关声明:
case RoutingMessage::FWDMESSAGE:
{
//Message Format:
//@FromNode~12~DestNode.from x to y hops .<the message>
int destID;
string changableStr, finalMessage;
string buf(iter->second);
string delim = ".";
destID = atoi(buf.substr(0, buf.find(delim)).c_str());
buf.erase(0, buf.find(delim) + delim.length());
changableStr = buf.substr(0, buf.find(delim));
stringstream sstm;
sstm << changableStr << " " << myID-3700;
changableStr = sstm.str();
buf.erase(0, buf.find(delim) + delim.length());
string theMsg = buf.substr(0, buf.find(delim));
stringstream sstm2;
sstm2 << changableStr << "." << theMsg;
finalMessage = sstm2.str();
cout << "FINAL MESSAGE: " << finalMessage << endl;
if(myID == destID)
cout << "\t\t\tMINE\n";
//PrintMessage(iter->)
else
{
neighbor.sin_port = forwardingTable.at(destID).begin()->first;
char *cstr = new char[finalMessage.length() + 1];
strcpy(cstr, finalMessage.c_str());
char* buf;
sprintf(buf, "@%d~%d~%d.%s", myID, RoutingMessage::FWDMESSAGE, destID, cstr);
SendMessage(neighbor, buf);
}
cout << "test\n";
break;
cout << "test2\n";
}
但是,当我点击break;
gdb spits out:
Program received signal SIGSEGV, Segmentation fault.
__GI___libc_free (mem=0x32317e3130373340) at malloc.c:2892
2892 malloc.c: No such file or directory.
(gdb) backtrace
#0 __GI___libc_free (mem=0x32317e3130373340) at malloc.c:2892
#1 0x00007ffff7b43cab in std::ios_base::~ios_base() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#2 0x00007ffff7b6ea13 in std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >::~basic_stringstream() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
我从未见过这个......任何人都可以帮忙吗?
答案 0 :(得分:2)
在这里看起来你是sprintf
未分配的缓冲区:
char* buf;
sprintf(buf, "@%d~%d~%d.%s", myID, RoutingMessage::FWDMESSAGE, destID, cstr);