uninitialized string

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux