• <small id='tgtOu'></small><noframes id='tgtOu'>

      1. <legend id='tgtOu'><style id='tgtOu'><dir id='tgtOu'><q id='tgtOu'></q></dir></style></legend>
      2. <i id='tgtOu'><tr id='tgtOu'><dt id='tgtOu'><q id='tgtOu'><span id='tgtOu'><b id='tgtOu'><form id='tgtOu'><ins id='tgtOu'></ins><ul id='tgtOu'></ul><sub id='tgtOu'></sub></form><legend id='tgtOu'></legend><bdo id='tgtOu'><pre id='tgtOu'><center id='tgtOu'></center></pre></bdo></b><th id='tgtOu'></th></span></q></dt></tr></i><div id='tgtOu'><tfoot id='tgtOu'></tfoot><dl id='tgtOu'><fieldset id='tgtOu'></fieldset></dl></div>

          <bdo id='tgtOu'></bdo><ul id='tgtOu'></ul>
        <tfoot id='tgtOu'></tfoot>

        std:ostringstream 和 -std=c++11 的内存错误?

        Memory Error with std:ostringstream and -std=c++11?(std:ostringstream 和 -std=c++11 的内存错误?)

        <small id='iOEYn'></small><noframes id='iOEYn'>

          <bdo id='iOEYn'></bdo><ul id='iOEYn'></ul>
            <i id='iOEYn'><tr id='iOEYn'><dt id='iOEYn'><q id='iOEYn'><span id='iOEYn'><b id='iOEYn'><form id='iOEYn'><ins id='iOEYn'></ins><ul id='iOEYn'></ul><sub id='iOEYn'></sub></form><legend id='iOEYn'></legend><bdo id='iOEYn'><pre id='iOEYn'><center id='iOEYn'></center></pre></bdo></b><th id='iOEYn'></th></span></q></dt></tr></i><div id='iOEYn'><tfoot id='iOEYn'></tfoot><dl id='iOEYn'><fieldset id='iOEYn'></fieldset></dl></div>
                1. <legend id='iOEYn'><style id='iOEYn'><dir id='iOEYn'><q id='iOEYn'></q></dir></style></legend>

                2. <tfoot id='iOEYn'></tfoot>
                    <tbody id='iOEYn'></tbody>
                  本文介绍了std:ostringstream 和 -std=c++11 的内存错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  编辑:感谢所有指出问题并在 Stack Overflow 上讨论过的人.我自己投了最后一票.

                  EDIT: Thanks to everyone who pointed out the problem, and that it was discussed on Stack Overflow. I cast the last close vote myself.

                  一个相关的问题:ostringstreamostringstream::str 状态它是一个临时的.怎么这么多人知道?还是我应该查阅不同的文档?

                  A related question: neither CPP Reference on ostringstream or ostringstream::str state its a temporary. How did so many people know? Or is there different documentation I should have consulted?

                  我在带有 GCC 4.7.2、-std=c++11std::ostringstream 的 Debian 7.3 (x64) 下的内存错误方面遇到了很多麻烦代码>.它导致像 https://stackoverflow.com/questions/21260815/which-r-in-this-create-table-error-message.

                  I'm having a lot of trouble with memory errors under Debian 7.3 (x64) with GCC 4.7.2, -std=c++11 and std::ostringstream. Its leading to bizaare results like https://stackoverflow.com/questions/21260815/which-r-in-this-create-table-error-message.

                  完整的程序使用 Sqlite.从命令行运行简化案例与 Valgrind 打印 2 个不同的错误.整个简化案例程序可在 代码查看器 上找到(我认为发布时间有点长整个样本在这里).它所做的只是初始化 Sqlite、打开数据库、创建表、关闭数据库和单元化数据库.如果发生错误,它会报告错误.没有其他事情发生.

                  The full blown program uses Sqlite. Running a reduced case from the command line vs. Valgrind prints 2 different errors. The entire reduced case program is available at Code Viewer (I thought it was kind of long to post the entire sample here). All it does is initializes Sqlite, opens a database, creates a table, closes the database, and unitializes the database. And it reports errors if they occur. There's nothing else occurring.

                  这是简化案例程序的一部分,它只是尝试创建一个表.如果该表存在,它应该会产生一个错误(确实如此):

                  Here's part of the reduced case program that simply tries to create a table. If the table exists, it should produce an error (which it does):

                  ostringstream qs;
                  qs.str().reserve(96);
                  
                  qs << "CREATE TABLE test";
                  qs << "(";
                  qs << "  userid INTEGER PRIMARY KEY AUTOINCREMENT,";
                  qs << "  username TEXT,";
                  qs << "  salt BLOB,";
                  qs << "  hmac BLOB";
                  qs << ");";
                  
                  const char* stmt = qs.str().c_str();
                  AC_ASSERT(NULL != stmt);
                  
                  rc = sqlite3_exec(db, stmt, NULL, NULL, &err);
                  AC_ASSERT(rc == SQLITE_OK);
                  
                  if(rc != SQLITE_OK)
                  {
                      ostringstream oss;
                      oss.str().reserve(96);
                  
                      oss << "sqlite3_exec failed, error " << rc;
                      LogError(oss.str().c_str());
                  
                      oss.clear(), oss.str("");
                      oss << "Sqlite error: " << err;
                      LogError(oss.str().c_str());
                  
                      // Break, handle error
                  }
                  

                  但是,在命令行下,消息是:

                  However, under command line, the message is:

                  sqlite3_exec failed, error 1
                  Sqlite error: table 
                  

                  在 Valgrind 下,消息是:

                  Under Valgrind, the message is:

                  sqlite3_exec failed, error 1
                  Sqlite error: table test already exists
                  

                  程序大量使用ostringstream,Valgrind围绕它们产生了近13个问题,其中9个包括底层operator delete(void*)>basic_string.例如,如下所示(t.cpp 中的第 76 行是 const char* stmt = qs.str().c_str();):

                  The program makes heavy use of ostringstream, and Valgrind produces nearly 13 issues centered around them, 9 of which include operator delete(void*) on the underlying basic_string. For example, one is shown below (and line 76 from t.cpp is const char* stmt = qs.str().c_str();):

                  ==14318== Invalid read of size 1
                  ==14318==    at 0x45ACC8: sqlite3_exec (sqlite3.c:94542)
                  ==14318==    by 0x405D07: main (t.cpp:79)
                  ==14318==  Address 0x5d89728 is 24 bytes inside a block of size 127 free'd
                  ==14318==    at 0x4C27870: operator delete(void*) (vg_replace_malloc.c:502)
                  ==14318==    by 0x530EB1F: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17)
                  ==14318==    by 0x405CF1: main (t.cpp:76)
                  

                  有没有人对这里发生的事情有任何想法?是 ostringstream 吗?或者也许是 GCC 4.7.2?或者也许是 Debian 的端口?

                  Does anyone have any ideas of what's going on here? Is it ostringstream? Or perhaps GCC 4.7.2? Or maybe Debian's port?

                  (对于开放式问题很抱歉.我已经无事可做).

                  (And sorry for the open ended question. I've run out of things to do).

                  推荐答案

                  const char* stmt = qs.str().c_str();
                  

                  qs 中提取一个临时字符串,获取指向其内容的指针,然后销毁临时字符串,使指针悬空.在此之后使用指针将产生未定义的行为.

                  That extracts a temporary string from qs, takes a pointer to its content, then destroys the temporary leaving the pointer dangling. Using the pointer after this will give undefined behaviour.

                  要修复它,您可以将 str() 的结果分配给一个变量,使其不再是临时的,或者使用此表达式作为 sqlite3_exec 的参数>,以便临时文件在该函数调用之后继续存在.(在第二种情况下,您必须删除第一个断言;但无论如何该断言都毫无意义).

                  To fix it, you could either assign the result of str() to a variable, so that it's no longer temporary, or use this expression as the argument to sqlite3_exec, so that the temporary survives until after that function call. (In the second case, you'd have to remove the first assertion; but that assertion is rather pointless anyway).

                  这篇关于std:ostringstream 和 -std=c++11 的内存错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Constructor initialization Vs assignment(构造函数初始化 Vs 赋值)
                  Is a `=default` move constructor equivalent to a member-wise move constructor?(`=default` 移动构造函数是否等同于成员移动构造函数?)
                  Has the new C++11 member initialization feature at declaration made initialization lists obsolete?(声明时新的 C++11 成员初始化功能是否使初始化列表过时了?)
                  Order of constructor call in virtual inheritance(虚继承中构造函数调用的顺序)
                  How to use sfinae for selecting constructors?(如何使用 sfinae 选择构造函数?)
                  Initializing a union with a non-trivial constructor(使用非平凡的构造函数初始化联合)

                    1. <i id='YMk3q'><tr id='YMk3q'><dt id='YMk3q'><q id='YMk3q'><span id='YMk3q'><b id='YMk3q'><form id='YMk3q'><ins id='YMk3q'></ins><ul id='YMk3q'></ul><sub id='YMk3q'></sub></form><legend id='YMk3q'></legend><bdo id='YMk3q'><pre id='YMk3q'><center id='YMk3q'></center></pre></bdo></b><th id='YMk3q'></th></span></q></dt></tr></i><div id='YMk3q'><tfoot id='YMk3q'></tfoot><dl id='YMk3q'><fieldset id='YMk3q'></fieldset></dl></div>

                      <small id='YMk3q'></small><noframes id='YMk3q'>

                    2. <tfoot id='YMk3q'></tfoot>
                        <bdo id='YMk3q'></bdo><ul id='YMk3q'></ul>

                            <tbody id='YMk3q'></tbody>
                          <legend id='YMk3q'><style id='YMk3q'><dir id='YMk3q'><q id='YMk3q'></q></dir></style></legend>