Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.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+中没有明显原因的SIGSEGV和SIGABRT+;_C++_Sockets_Pointers_Sigabrt_Segmentation Fault - Fatal编程技术网

C++ 在C+中没有明显原因的SIGSEGV和SIGABRT+;

C++ 在C+中没有明显原因的SIGSEGV和SIGABRT+;,c++,sockets,pointers,sigabrt,segmentation-fault,C++,Sockets,Pointers,Sigabrt,Segmentation Fault,我正在尝试从一个套接字读取一条完整消息,该消息可能非常长,使用以下代码: int readWholeMessage(char* &message){ bool ended=false; bool first=true; int mlen=0; int len = BUFFLEN; char* buffer; char* sol=new char[BUFFLEN]; while(!ended) { buffer = n

我正在尝试从一个套接字读取一条完整消息,该消息可能非常长,使用以下代码:

int readWholeMessage(char* &message){
    bool ended=false;
    bool first=true;
    int mlen=0;
    int len = BUFFLEN;
    char* buffer;
    char* sol=new char[BUFFLEN];
    while(!ended) {
        buffer = new char[BUFFLEN];
        n = (int) read(s, buffer, (size_t) BUFFLEN);
        if (n < 0) {
            perror("ERROR reading from socket");
            close(s);
            return 1;
        }
        else{
            //looks for the end of the message: "\r\n"          
            for(int i=0; i<BUFFLEN-1; i++){
                mlen++;
                if(buffer[i]=='\r' && buffer[i+1]=='\n'){
                    ended=true;
                    break;
                }
            }
            mlen++;
            //saves the result in sol and deletes buffer
            if(first){
                strcpy(sol, buffer);
                first=false;
            }
            //creates a new bigger array, saves the whole message,
            //deletes de old array points the pointer sol to the new one
            else {
                len+=BUFFLEN;
                char *bufferAux = new char[len+1];
                strcpy(bufferAux, sol);
                strcat(bufferAux,buffer);
                delete [] sol;
                sol=bufferAux;
            }
            delete [] buffer;
        }
    }
    message = new char[mlen+1];
    strncpy(message, sol, (size_t) mlen);
    message[mlen]= '\0';
    delete[](sol);
    return 0;
}
int readWholeMessage(char*&message){
布尔结束=假;
bool first=true;
int-mlen=0;
int len=BUFFLEN;
字符*缓冲区;
char*sol=新char[BUFFLEN];
当(!结束){
缓冲区=新字符[BUFFLEN];
n=(int)读取(s,缓冲区,(大小)BUFFLEN);
if(n<0){
perror(“从套接字读取错误”);
关闭(s);;
返回1;
}
否则{
//查找消息的结尾:“\r\n”

对于(int i=0;离子)一个分支,您删除了另一个分支上的缓冲区,而您不删除。我宁愿将该问题作为一个整体解决(RAII),并希望SIG是由
strcpy(sol,buffer)引起的;
是错误的。缓冲区不是空终止的。您有
delete[]sol
在两个地方——在
for
循环内部以及循环完成后。只要将
std::vector
用于缓冲区,并且不必发出任何对
delete[]的调用,就可以减少大量代码并使其更安全
或进行手动内存管理以调整大小。可能会运行得更快,因为您不会在循环的每个迭代中调用分配器。最好将整个问题和注释线程迁移到聊天室,因为这里没有人正确地将此网站视为问答库。