public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/49122] New: [C++0x] initializer_list is broken
@ 2011-05-23 13:14 sscrisk at gmail dot com
  2011-05-23 14:52 ` [Bug c++/49122] " paolo.carlini at oracle dot com
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: sscrisk at gmail dot com @ 2011-05-23 13:14 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49122

           Summary: [C++0x] initializer_list is broken
           Product: gcc
           Version: 4.6.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: sscrisk@gmail.com


The initializer_list object returned to function is broken.
See the next code:

#include<initializer_list>
#include<iostream>

std::initializer_list<int> f(int a, int b)
{
 return {a, b};
}

int main()
{
 std::initializer_list<int> list = f(0, 1);
 for(int i: list)
  std::cout << i << std::endl;
}


Output result (example) is

2293416
2293636


It's a strange result.
Output should be

0
1


I tried use compiler version 4.6.0 Release and 4.7.0 20110521 (experimental)


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/49122] [C++0x] initializer_list is broken
  2011-05-23 13:14 [Bug c++/49122] New: [C++0x] initializer_list is broken sscrisk at gmail dot com
@ 2011-05-23 14:52 ` paolo.carlini at oracle dot com
  2011-05-23 17:10 ` redi at gcc dot gnu.org
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: paolo.carlini at oracle dot com @ 2011-05-23 14:52 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49122

Paolo Carlini <paolo.carlini at oracle dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID

--- Comment #1 from Paolo Carlini <paolo.carlini at oracle dot com> 2011-05-23 14:19:40 UTC ---
If we *really* have to use the word broken here, it much better applies to your
snippet, not the implementation of std::initializer_list: an initializer_list
is essentially just a pointer (+ a length) into something else (open the header
and you will see it): thus, f is returning a pointer to the {a, b} which by
itself goes away when f returns. Anything can happen. Compare:

std::initializer_list<int> f(int a, int b)
{
  static auto range = {a, b};
  return range;
}

...


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/49122] [C++0x] initializer_list is broken
  2011-05-23 13:14 [Bug c++/49122] New: [C++0x] initializer_list is broken sscrisk at gmail dot com
  2011-05-23 14:52 ` [Bug c++/49122] " paolo.carlini at oracle dot com
@ 2011-05-23 17:10 ` redi at gcc dot gnu.org
  2013-01-08  9:39 ` joy.career at gmail dot com
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: redi at gcc dot gnu.org @ 2011-05-23 17:10 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49122

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> 2011-05-23 16:44:22 UTC ---
There's a note in [support.initlist] which says:
"Copying an initializer list does not copy the underlying elements."

However there was some discussion on the core reflector last month pointing out
that other requirements are inconsistent with that.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/49122] [C++0x] initializer_list is broken
  2011-05-23 13:14 [Bug c++/49122] New: [C++0x] initializer_list is broken sscrisk at gmail dot com
  2011-05-23 14:52 ` [Bug c++/49122] " paolo.carlini at oracle dot com
  2011-05-23 17:10 ` redi at gcc dot gnu.org
@ 2013-01-08  9:39 ` joy.career at gmail dot com
  2013-01-08 10:28 ` redi at gcc dot gnu.org
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: joy.career at gmail dot com @ 2013-01-08  9:39 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49122

Joyabrata Ghosh <joy.career at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |joy.career at gmail dot com

--- Comment #3 from Joyabrata Ghosh <joy.career at gmail dot com> 2013-01-08 09:38:51 UTC ---
Hi All,

Would anyone from GCC team please confirm, when the broken initializer_list
feature will be fixed:

I tried with gcc 4.6.3 and it appears to be broken today.

Results: Behaviour is persistent and data returning from caller constructor
were  garbage values.


$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/i686-linux-gnu/4.6/lto-wrapper
Target: i686-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro
4.6.3-1ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs
--enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr
--program-suffix=-4.6 --enable-shared --enable-linker-build-id
--with-system-zlib --libexecdir=/usr/lib --without-included-gettext
--enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6
--libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu
--enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object
--enable-plugin --enable-objc-gc --enable-targets=all --disable-werror
--with-arch-32=i686 --with-tune=generic --enable-checking=release
--build=i686-linux-gnu --host=i686-linux-gnu --target=i686-linux-gnu
Thread model: posix
gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/49122] [C++0x] initializer_list is broken
  2011-05-23 13:14 [Bug c++/49122] New: [C++0x] initializer_list is broken sscrisk at gmail dot com
                   ` (2 preceding siblings ...)
  2013-01-08  9:39 ` joy.career at gmail dot com
@ 2013-01-08 10:28 ` redi at gcc dot gnu.org
  2013-01-08 10:48 ` joy.career at gmail dot com
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: redi at gcc dot gnu.org @ 2013-01-08 10:28 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49122

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> 2013-01-08 10:27:55 UTC ---
This is not a bug in GCC, there's nothing to fix.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/49122] [C++0x] initializer_list is broken
  2011-05-23 13:14 [Bug c++/49122] New: [C++0x] initializer_list is broken sscrisk at gmail dot com
                   ` (3 preceding siblings ...)
  2013-01-08 10:28 ` redi at gcc dot gnu.org
@ 2013-01-08 10:48 ` joy.career at gmail dot com
  2013-01-08 12:39 ` redi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: joy.career at gmail dot com @ 2013-01-08 10:48 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49122

--- Comment #5 from Joyabrata Ghosh <joy.career at gmail dot com> 2013-01-08 10:48:36 UTC ---
Hi Jonathan Wakely,

Just wanted to confirm the doubt:

Do you wanted to mean that, the initializer_list<T> behaviour is exactly like
this(work for local stack members) and there nothing work around possible to
avoid this observation ?

Thanks in advance,
Joyabrata Ghosh


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/49122] [C++0x] initializer_list is broken
  2011-05-23 13:14 [Bug c++/49122] New: [C++0x] initializer_list is broken sscrisk at gmail dot com
                   ` (4 preceding siblings ...)
  2013-01-08 10:48 ` joy.career at gmail dot com
@ 2013-01-08 12:39 ` redi at gcc dot gnu.org
  2013-03-08  6:56 ` potswa at mac dot com
  2013-03-08  7:57 ` potswa at mac dot com
  7 siblings, 0 replies; 9+ messages in thread
From: redi at gcc dot gnu.org @ 2013-01-08 12:39 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49122

--- Comment #6 from Jonathan Wakely <redi at gcc dot gnu.org> 2013-01-08 12:39:26 UTC ---
(In reply to comment #5)
> Hi Jonathan Wakely,
> 
> Just wanted to confirm the doubt:

That's not a "doubt" it's a question.

> Do you wanted to mean that, the initializer_list<T> behaviour is exactly like
> this(work for local stack members) and there nothing work around possible to
> avoid this observation ?

Yes. If you want to pass data out of a function use a container or a tuple or
something that *copies* the data. An std::initializer_list is not a container,
it does not own or copy its elements.

The workaround is don't misuse initializer_list for something it isn't designed
to do.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/49122] [C++0x] initializer_list is broken
  2011-05-23 13:14 [Bug c++/49122] New: [C++0x] initializer_list is broken sscrisk at gmail dot com
                   ` (5 preceding siblings ...)
  2013-01-08 12:39 ` redi at gcc dot gnu.org
@ 2013-03-08  6:56 ` potswa at mac dot com
  2013-03-08  7:57 ` potswa at mac dot com
  7 siblings, 0 replies; 9+ messages in thread
From: potswa at mac dot com @ 2013-03-08  6:56 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49122

David Krauss <potswa at mac dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |potswa at mac dot com

--- Comment #7 from David Krauss <potswa at mac dot com> 2013-03-08 06:56:00 UTC ---
I think this should be reopened. The return value object, which exists in the
calling scope, is initialized from the braced-init-list. No copying occurs
beyond what is demonstrated in the example in [dcl.init.list]/6.

For more detail, see http://stackoverflow.com/q/15286450/153285 .


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c++/49122] [C++0x] initializer_list is broken
  2011-05-23 13:14 [Bug c++/49122] New: [C++0x] initializer_list is broken sscrisk at gmail dot com
                   ` (6 preceding siblings ...)
  2013-03-08  6:56 ` potswa at mac dot com
@ 2013-03-08  7:57 ` potswa at mac dot com
  7 siblings, 0 replies; 9+ messages in thread
From: potswa at mac dot com @ 2013-03-08  7:57 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49122

--- Comment #8 from David Krauss <potswa at mac dot com> 2013-03-08 07:57:03 UTC ---
Oops, I didn't read the original code closely enough. To be OK by the above
interpretation, the local initializer_list variable would need to be eliminated
from main() and the loop would be 

 for(int i: f())

Should I open a new bug?


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2013-03-08  7:57 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-05-23 13:14 [Bug c++/49122] New: [C++0x] initializer_list is broken sscrisk at gmail dot com
2011-05-23 14:52 ` [Bug c++/49122] " paolo.carlini at oracle dot com
2011-05-23 17:10 ` redi at gcc dot gnu.org
2013-01-08  9:39 ` joy.career at gmail dot com
2013-01-08 10:28 ` redi at gcc dot gnu.org
2013-01-08 10:48 ` joy.career at gmail dot com
2013-01-08 12:39 ` redi at gcc dot gnu.org
2013-03-08  6:56 ` potswa at mac dot com
2013-03-08  7:57 ` potswa at mac dot com

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).