Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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++ 从char追加到std::string*_C++_String - Fatal编程技术网

C++ 从char追加到std::string*

C++ 从char追加到std::string*,c++,string,C++,String,我在Windows8上使用cygwin,g++。我编写了连接类的一个成员函数,该函数用于维护连接并提供一些操作。其中之一如下: const char* Connection::recieve_all_data() { using namespace utils; if(socket_ == INVALID_SOCKET) { throw std::invalid_argument("Socket was not initializ

我在Windows8上使用cygwin,g++。我编写了连接类的一个成员函数,该函数用于维护连接并提供一些操作。其中之一如下:

 const char* Connection::recieve_all_data()
 {
      using namespace utils;
      if(socket_ == INVALID_SOCKET)
      {
             throw std::invalid_argument("Socket was not initialized\n");
      }

      std::string result = std::string();
      char *buf = new char[BUFFER_SIZE];
      while(true)
      {
               int iResult = recv(socket_, buf, utils::BUFFER_SIZE, 0);
               if(iResult == BUFFER_SIZE)
               {
                       result.append(buf);
                       std::cout << buf << std::endl << std::endl; //1, LOOK AT THIS LINE
               }
               else if (iResult < BUFFER_SIZE)
               {
                       char *final_buffer = new char[iResult + 1];
                       strncpy(final_buffer, buf, iResult);
                       final_buffer[iResult] = '\0';
                       result.append(final_buffer);
                       break;
               }
               else if(iResult == WSAENOTCONN)
               {
                       throw std::runtime_error("Socket was not connected");
               }
      }
      return result.c_str();
}
但如果我对第//1行进行注释,则收到的响应不完整:

HTTP/1.0 400 Bad Request
Content-Type: text/html; charset=UTF-8
Content-Length: 1419
Date: Wed, 25 Mar 2015 12:12:07 GMT
Server: GFE/2.0

<!DOCTYPE html>
<html lang=en>
  <meta charset=utf-8>
  <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
  <title>Error 400 (Bad Request)!!1</title>
  <style>
    *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/errors/logo_sm_2.png) no-repeat}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/errors/logo_sm_2_hr.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/errors/logo_sm_2_hr.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/errors/logo_sm_2_hr.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:55px;width:150px}
  </style>
  <a href=

怎么了?我真的不知道那种行为。第//1行如何影响结果?这只是一个输出。可能是cygwin问题?

您遇到了一个大问题,返回了一个超出范围的局部变量的.c_str。这意味着您的指针在离开函数后指向垃圾。您是否有可能更改函数以返回std::string?然后您可以返回结果;std::string结果=std::string;可以简化为std::string结果;在哪里删除buf或final_缓冲区?为什么不好?你在C++编程,你在包裹一个C-API。没有理由不将包装器调整为C++风格。如果必须强制返回c-string,则需要使用新运算符在堆上分配一个char*,然后将std::strings内容复制到此c-string中,然后返回此c-string.Ugh。第一个函数中的内存泄漏只比保证读取的数据以null字符终止的假设稍微可怕一些。如前所述,返回值显然不好。
HTTP/1.0 400 Bad Request
Content-Type: text/html; charset=UTF-8
Content-Length: 1419
Date: Wed, 25 Mar 2015 12:12:07 GMT
Server: GFE/2.0

<!DOCTYPE html>
<html lang=en>
  <meta charset=utf-8>
  <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
  <title>Error 400 (Bad Request)!!1</title>
  <style>
    *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/errors/logo_sm_2.png) no-repeat}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/errors/logo_sm_2_hr.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/errors/logo_sm_2_hr.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/errors/logo_sm_2_hr.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:55px;width:150px}
  </style>
  <a href=