Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 中断语句的分段错误?_C++_Ubuntu_Segmentation Fault_Switch Statement - Fatal编程技术网

C++ 中断语句的分段错误?

C++ 中断语句的分段错误?,c++,ubuntu,segmentation-fault,switch-statement,C++,Ubuntu,Segmentation Fault,Switch Statement,我只是在看一个switch语句: case RoutingMessage::FWDMESSAGE: { //Message Format: //@FromNode~12~DestNode.from x to y hops .<the message> int destID; string changableStr, finalMessage;

我只是在看一个switch语句:

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";
            }

我从没见过这个。。。有人能帮忙吗?

看起来您正在
sprintf
使用未分配的缓冲区:

char* buf;
sprintf(buf, "@%d~%d~%d.%s", myID, RoutingMessage::FWDMESSAGE, destID, cstr);

真奇怪-你是对的,我完全是。。。但是为什么在
break
上崩溃,而不是在我的
sprintf()上崩溃呢?在这个节点崩溃之前,我实际上能够将正确的缓冲区发送到我的其他节点被单位化并被覆盖,我猜“未定义的行为”适用-可能性令人难以置信:)@MrDuk on
break
(或者,通常在块结束时)为每个离开作用域的本地对象调用析构函数。您在
buf
指针中有未定义的数据,并覆盖了一些内存-在某处;在您的情况下,此覆盖似乎损坏了malloc的标记。你们真的很幸运,你们的程序这么快就崩溃了,所以你们可以很容易地发现问题并解决它。
char* buf;
sprintf(buf, "@%d~%d~%d.%s", myID, RoutingMessage::FWDMESSAGE, destID, cstr);