This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
no warning when assigning a string to itself; infinite loop when printing a string
- From: Steve Ward <planet36 at gmail dot com>
- To: gcc-help at gcc dot gnu dot org
- Date: Sun, 22 May 2011 14:10:04 -0400
- Subject: no warning when assigning a string to itself; infinite loop when printing a string
In the attached code, the line "const string str = str;" doesn't give
a warning. Is this right? If I change the data type to an "int", a
warning is given. ("warning: 'str' is used uninitialized in this
function")
Also, when trying to print the string, it seems to execute forever.
Thanks,
Steve
/*
$ g++ --version
g++ (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ g++ -Wall gcc_no_warning.cpp && ./a.out
*/
#include <iostream>
using namespace std;
int main()
{
// no warning
const string str = str;
// infinite loop
cout << str << endl;
return 0;
}