public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
From: "vol.litwr at gmail dot com" <gcc-bugzilla@gcc.gnu.org> To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/53990] New: wrong "optimisation": automatic variable doesn't removed at fuction exit Date: Tue, 17 Jul 2012 05:40:00 -0000 [thread overview] Message-ID: <bug-53990-4@http.gcc.gnu.org/bugzilla/> (raw) http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53990 Bug #: 53990 Summary: wrong "optimisation": automatic variable doesn't removed at fuction exit Classification: Unclassified Product: gcc Version: 4.6.2 Status: UNCONFIRMED Severity: major Priority: P3 Component: c++ AssignedTo: unassigned@gcc.gnu.org ReportedBy: vol.litwr@gmail.com I use C++ with the following specs by g++ -v Using built-in specs. Target: x86_64-manbo-linux-gnu Configured with: ../configure --prefix=/usr --libexecdir=/usr/lib --with-slibdir=/lib64 --with-bugurl=https://qa.mandriva.com/ --mandir=/usr/share/man --infodir=/usr/share/info --enable-checking=release --enable-languages=c,c++,ada,fortran,objc,obj-c++,java --build=x86_64-manbo-linux-gnu --host=x86_64-manbo-linux-gnu --with-cpu=generic --with-system-zlib --enable-threads=posix --enable-shared --enable-objc-gc --enable-long-long --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --enable-java-awt=gtk --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-gtk-cairo --disable-libjava-multilib --enable-ssp --disable-libssp --disable-werror --with-ppl --with-cloog --with-python-dir=/lib/python2.6/site-packages Thread model: posix gcc version 4.4.1 (GCC) I've also done tests with gcc version 4.4.5 (Debian 4.4.5-8) and gcc version 4.6.2 (SUSE Linux). See the next code struct A { int d; A operator*(const A&) const; A operator+(const A&) const; } a; A A::operator*(const A& a) const { A c; c.d = d*a.d; return c; } A A::operator+(const A& a) const { cout << *this; return a; } "return a;" causes the call of the copy constructor, but "return c;" doesn't call this constructor---it returns (does not destroy!) automatic (!) variable c. Is this optimization in C++ standard? If no then this optimisation may cause severe errors if we are using pointers. See the next code. #include <iostream> using namespace std; struct Array { unsigned char HSize, VSize; int *p; bool canBeDeleted; Array(unsigned char, unsigned char); Array(unsigned char, unsigned char, int*); Array(const Array&); Array operator[](unsigned char) const; Array& operator=(const Array&); Array& operator=(int); Array operator+(Array) const; friend ostream& operator<<(ostream&, const Array&); operator int&() {return p[0];}; ~Array(); }; Array::Array(unsigned char n, unsigned char m) { VSize = n; HSize = m; p = new int[n*m]; canBeDeleted = true; } Array::Array(unsigned char n, unsigned char m, int* p1) { VSize = n; HSize = m; p = p1; canBeDeleted = true; } Array::Array(const Array& Data) { HSize = Data.HSize; VSize = Data.VSize; p = new int[VSize*HSize]; for (int i = 0; i < Data.VSize; i++) { for (int j = 0; j < Data.HSize; j++) p[i*HSize + j] = Data.p[i*HSize + j]; } canBeDeleted = true; } Array::~Array() { if (canBeDeleted) delete []p; } Array Array::operator[](unsigned char i) const { if (i >= VSize && VSize != 1) throw 1; unsigned char size = HSize; if (VSize == 1) { size = 1; if (i >= HSize) throw 7; } Array A(1, size, p + i*size); A.canBeDeleted = false; return A; } Array& Array::operator=(int i) { if (HSize != 1 && VSize != 1) throw 2; this->p[0] = i; } Array& Array::operator=(const Array& Data) { if (VSize == Data.VSize && HSize == Data.HSize) for (int i = 0; i < VSize; i++) for (int j = 0; j < HSize; j++) p[i*HSize + j] = Data.p[i*HSize + j]; else throw 3; return *this; } Array Array::operator+(Array Data) const { if (VSize == Data.VSize && HSize == Data.HSize) for (int i = 0; i < VSize; i++) for (int j = 0; j < HSize; j++) Data.p[i*HSize + j] += p[i*HSize + j]; else throw 5; return Data; } ostream& operator<<(ostream& s, const Array& Data) { if (Data.VSize == 1 && Data.HSize == 1) { s << Data.p[0]; return s; } s << endl; for (int i = 0; i < Data.VSize; i++) { for (int j = 0; j < Data.HSize; j++) s << '\t' << Data.p[i*Data.HSize + j]; s << endl; } s << endl; return s; } int main() { Array A(3,3), E(3,3); for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) { E[i][j] = i == j; A[i][j] = i + j; } cout << "A" << A << "E" << E; A[1][2] = 7; A[2] = E[1]; //it works. but should it work? A[1] + E[2]; //a problem! cout << "E" << E; } E was changed! :-( Regards Litwr
next reply other threads:[~2012-07-17 5:40 UTC|newest] Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top 2012-07-17 5:40 vol.litwr at gmail dot com [this message] 2012-07-17 6:00 ` [Bug c++/53990] " pinskia at gcc dot gnu.org 2012-07-17 8:03 ` redi at gcc dot gnu.org 2012-07-18 12:19 ` vol.litwr at gmail dot com 2012-07-18 12:40 ` redi at gcc dot gnu.org
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=bug-53990-4@http.gcc.gnu.org/bugzilla/ \ --to=gcc-bugzilla@gcc.gnu.org \ --cc=gcc-bugs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).