This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

uninitialized string


The following was asked on comp.lang.c++;
It compiles and crashes with no error/warning on gcc 8.2.1:

#include <iostream>
#include <string>

int main()
{
  std::string s1 { s1 };
  std::string s2(s2);
  std::string s3 = s3;

  std::cout << s1 << s2 << s3;
}

[tmp]$ g++ -Wall selfref.cc && ./a.out
Segmentation fault (core dumped)
[tmp]$

On the other hand:

#include <iostream>

int main()
{
  int n { n };

  std::cout  << n << std::endl;
}

[tmp]$ g++ -Wall selfint.cc && ./a.out
selfint.cc: In function ‘int main()’:
selfint.cc:5:7: warning: ‘n’ is used uninitialized in this function [-Wuninitialized]
   int n { n };
       ^
0
[tmp]$

I wonder why int triggers the warning, but std::string does not.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]