From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 7166 invoked by alias); 3 Oct 2014 08:19:16 -0000 Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org Received: (qmail 7150 invoked by uid 89); 3 Oct 2014 08:19:15 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.5 required=5.0 tests=AWL,BAYES_00,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW,SPF_PASS autolearn=ham version=3.3.2 X-HELO: mail-la0-f51.google.com Received: from mail-la0-f51.google.com (HELO mail-la0-f51.google.com) (209.85.215.51) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Fri, 03 Oct 2014 08:19:14 +0000 Received: by mail-la0-f51.google.com with SMTP id ge10so613418lab.10 for ; Fri, 03 Oct 2014 01:19:10 -0700 (PDT) X-Received: by 10.112.166.35 with SMTP id zd3mr3852798lbb.3.1412324350476; Fri, 03 Oct 2014 01:19:10 -0700 (PDT) Received: from [130.233.179.5] ([130.233.179.5]) by mx.google.com with ESMTPSA id li3sm2433189lab.25.2014.10.03.01.19.09 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 03 Oct 2014 01:19:09 -0700 (PDT) Message-ID: <542E5BF1.2070508@gmail.com> Date: Fri, 03 Oct 2014 08:19:00 -0000 From: =?UTF-8?B?SGVucmlrIE1hbm5lcnN0csO2bQ==?= User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.1.2 MIME-Version: 1.0 To: gcc-help@gcc.gnu.org Subject: std::string clobbers memory when compiling without optimizations Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-IsSubscribed: yes X-SW-Source: 2014-10/txt/msg00012.txt.bz2 Hello, I found a program that produces incorrect behavior when compiling with -Og or without optimization. By running gdb I can pinpoint the problem to the std::string constructor, but I have to do more research to be able to step through that with gdb. My minimal working example is not so minimal, but I have not been able to reproduce the problem with a smaller program. The Eigen library used is from eigen.tuxfamily.org . On the line indicated by "Correct output" the program prints what I expect, two lines later, the contents has changed. With gdb I can see that the memory content is changed by the std::string namestring(name) line in the t2s function. Watching the memory location that is changed gives std::basic_string, std::allocator >::basic_string(char const*, std::allocator const&) as the offending location. Compiling with g++-4.9 -std=c++11 gives the error, but adding -O1 produces correct results. Do you have any suggestions on what to do next? BR, Henrik Mannerström #include #include #include #include template std::string t2s(T tt) { char *name; int status; name = abi::__cxa_demangle(typeid(tt).name(), 0, 0, &status); std::string namestring(name); // Variable Sigma from main is changed at this line. std::free(name); return namestring; } #include #include int main() { typedef Eigen::Matrix cVector; const cVector mean = (cVector() << 1, 2, 3).finished(); const auto Sigma = (cVector() << 1, 1, 1).finished().asDiagonal(); std::cout << "Correct diagonal: " << Sigma.diagonal() << std::endl; // Correct output: 1 1 1 std::cout << t2s(mean) << std::endl; std::cout << "Incorrect diagonal: " << Sigma.diagonal() << std::endl; // Incorrect output: [random double] 1 1 }