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]

Re: uninitialized string




On 1/21/2019 6:18 PM, Jonathan Wakely wrote:
On Mon, 21 Jan 2019 at 17:13, Manfred <mx2927@gmail.com> wrote:

[...]

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

Well for starters, std::string is not the same as int, not even close.

I know, they obviously are very different objects, but that's not the point.


It's pretty obvious when you think about it. Constructing a
std::string calls a constructor, in this case, it calls the
basic_string(const basic_string&) constructor, and passes the *this as
the argument, which makes a copy of the string. The problem is that
the string was never constructed, so it's a copy of garbage.

That's it, the object is used before it is initialized.
Apparently gcc is able to catch this with int (probably other types too), but not with string.


Failing to warn about it is a longstanding and well-known defect in
GCC, you can find lots of discussion in bugzilla.

The following was reported:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71228

Since this complains about int, but apparently int works today, I was wondering if anything is going on about string a well.


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