public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/13121] New: Inserting struct with valarray in map fails
@ 2003-11-19 10:12 Uwe dot Seimet at seimet dot de
  2003-11-19 14:34 ` [Bug libstdc++/13121] " bangerth at dealii dot org
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: Uwe dot Seimet at seimet dot de @ 2003-11-19 10:12 UTC (permalink / raw)
  To: gcc-bugs

Hello!

The sample code below demonstrates the bug. Struct A is not inserted correctly
into the map m. When running the sample problem the output is:

5
0

but it should be

5
5


Best regards,   Uwe

--

#include <map>
#include <valarray>
#include <iostream>

struct A 
{
  std::valarray <int> v;
  A (int i=0)
    : v (i)
  {}
};

int main (int, char**)
{
  std::map <void*, std::pair <A, bool> > m;

  A a (5);
  std::cout << (a.v.size ()) << std::endl;
  m [(void*)0] = std::make_pair (a, false);

  std::cout << (m [(void*)0].first.v.size ()) << std::endl;
  return 0;
}

-- 
           Summary: Inserting struct with valarray in map fails
           Product: gcc
           Version: 3.3.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: Uwe dot Seimet at seimet dot de
                CC: Uwe dot Seimet at seimet dot de,gcc-bugs at gcc dot gnu
                    dot org
 GCC build triplet: i686-pc-linux-gnu
  GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu


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


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

* [Bug libstdc++/13121] Inserting struct with valarray in map fails
  2003-11-19 10:12 [Bug c++/13121] New: Inserting struct with valarray in map fails Uwe dot Seimet at seimet dot de
@ 2003-11-19 14:34 ` bangerth at dealii dot org
  2003-11-19 14:47 ` bangerth at dealii dot org
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: bangerth at dealii dot org @ 2003-11-19 14:34 UTC (permalink / raw)
  To: gcc-bugs



-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|c++                         |libstdc++


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


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

* [Bug libstdc++/13121] Inserting struct with valarray in map fails
  2003-11-19 10:12 [Bug c++/13121] New: Inserting struct with valarray in map fails Uwe dot Seimet at seimet dot de
  2003-11-19 14:34 ` [Bug libstdc++/13121] " bangerth at dealii dot org
@ 2003-11-19 14:47 ` bangerth at dealii dot org
  2003-11-19 15:00 ` Uwe dot Seimet at seimet dot de
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: bangerth at dealii dot org @ 2003-11-19 14:47 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From bangerth at dealii dot org  2003-11-19 14:47 -------
This is because this program
------------------
#include <valarray>
#include <iostream>

int main ()
{
  std::valarray <int> v1(5);
  std::valarray <int> v2;
  v2 = v1;

  std::cout << v1.size() << " " << v2.size() << std::endl;
}
-----------------------
when compiled with any gcc version returns
  g/x> /home/bangerth/bin/gcc-3.4-pre/bin/c++ x.cc ; ./a.out
  5 0

icc, for reference, returns "5 5". Both results are standards conforming,
since 26.3.2.2/1 reads: "...The resulting behavior is undefined if the
length of the argument array is not equal to the length of the *this
array."

What you do with your map is the following: you first create an entry
by using m[(void*)0], and only then do you assign to it, so the result
is fully conforming.

W.


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


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


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

* [Bug libstdc++/13121] Inserting struct with valarray in map fails
  2003-11-19 10:12 [Bug c++/13121] New: Inserting struct with valarray in map fails Uwe dot Seimet at seimet dot de
  2003-11-19 14:34 ` [Bug libstdc++/13121] " bangerth at dealii dot org
  2003-11-19 14:47 ` bangerth at dealii dot org
@ 2003-11-19 15:00 ` Uwe dot Seimet at seimet dot de
  2003-11-19 15:04 ` bangerth at dealii dot org
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Uwe dot Seimet at seimet dot de @ 2003-11-19 15:00 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From Uwe dot Seimet at seimet dot de  2003-11-19 15:00 -------
Subject: Re:  Inserting struct with valarray in map fails

Hello!

> ------- Additional Comments From bangerth at dealii dot org  2003-11-19 14:47 -------
> This is because this program
> ------------------
> #include <valarray>
> #include <iostream>
> 
> int main ()
> {
>   std::valarray <int> v1(5);
>   std::valarray <int> v2;
>   v2 = v1;
> 
>   std::cout << v1.size() << " " << v2.size() << std::endl;
> }
> -----------------------
> when compiled with any gcc version returns
>   g/x> /home/bangerth/bin/gcc-3.4-pre/bin/c++ x.cc ; ./a.out
>   5 0
> 
> icc, for reference, returns "5 5". Both results are standards conforming,
> since 26.3.2.2/1 reads: "...The resulting behavior is undefined if the
> length of the argument array is not equal to the length of the *this
> array."
> 
> What you do with your map is the following: you first create an entry
> by using m[(void*)0], and only then do you assign to it, so the result
> is fully conforming.

Thank you for your rapid response! So gcc is obviously right, and our code
is wrong. Nevertheless we think that the standard is a bit strange, allowing
undefined behavior for an assignment.

Best regards,   Uwe



-- 


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


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

* [Bug libstdc++/13121] Inserting struct with valarray in map fails
  2003-11-19 10:12 [Bug c++/13121] New: Inserting struct with valarray in map fails Uwe dot Seimet at seimet dot de
                   ` (2 preceding siblings ...)
  2003-11-19 15:00 ` Uwe dot Seimet at seimet dot de
@ 2003-11-19 15:04 ` bangerth at dealii dot org
  2003-11-19 15:12 ` Uwe dot Seimet at seimet dot de
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: bangerth at dealii dot org @ 2003-11-19 15:04 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From bangerth at dealii dot org  2003-11-19 15:04 -------
That might well be that the standard is strange, but that's the way it is
and I believe it is that way for a good reason.

In your case it's easy to fix the problem by having an assignment
operator in your class that first sets the valarray to the right
size before copying contents.

W.

-- 


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


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

* [Bug libstdc++/13121] Inserting struct with valarray in map fails
  2003-11-19 10:12 [Bug c++/13121] New: Inserting struct with valarray in map fails Uwe dot Seimet at seimet dot de
                   ` (3 preceding siblings ...)
  2003-11-19 15:04 ` bangerth at dealii dot org
@ 2003-11-19 15:12 ` Uwe dot Seimet at seimet dot de
  2003-11-19 15:24 ` bangerth at dealii dot org
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Uwe dot Seimet at seimet dot de @ 2003-11-19 15:12 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From Uwe dot Seimet at seimet dot de  2003-11-19 15:12 -------
Subject: Re:  Inserting struct with valarray in map fails

Hello,

> That might well be that the standard is strange, but that's the way it is
> and I believe it is that way for a good reason.
> 
> In your case it's easy to fix the problem by having an assignment
> operator in your class that first sets the valarray to the right
> size before copying contents.

Yes, indeed. Just another question (or suggestion) on this: Would it be
possible for gcc to not simply ignore such an error condition but to throw
an exception, or assert, or whatever? In our actual (much larger) program,
where we encountered this problem, our program crashed with a floating point
exception in another piece of code. It was quite difficult to find out that
the problem was caused by the valarray assignment. If the gcc runtime had
aborted execution during the assignment it would have been much easier to
identify what was wrong.

Best regards,   Uwe



-- 


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


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

* [Bug libstdc++/13121] Inserting struct with valarray in map fails
  2003-11-19 10:12 [Bug c++/13121] New: Inserting struct with valarray in map fails Uwe dot Seimet at seimet dot de
                   ` (4 preceding siblings ...)
  2003-11-19 15:12 ` Uwe dot Seimet at seimet dot de
@ 2003-11-19 15:24 ` bangerth at dealii dot org
  2003-11-19 15:25 ` [Bug libstdc++/13121] Wanted: check in valarray::operator= for length of array bangerth at dealii dot org
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: bangerth at dealii dot org @ 2003-11-19 15:24 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From bangerth at dealii dot org  2003-11-19 15:24 -------
The reason why the standard says this is undefined behavior is that
the creators of valarray wanted to have maximum speed for this class.

A check for the length and possibly throwing an exception might sacrifice
this goal, although it would of course be much cheaper than the actual
memory allocation this "feature" is to prevent. I leave this up to
the libstdc++ maintainers -- maybe their new debug mode lib has this
fixed already.

W.

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


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


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

* [Bug libstdc++/13121] Wanted: check in valarray::operator= for length of array
  2003-11-19 10:12 [Bug c++/13121] New: Inserting struct with valarray in map fails Uwe dot Seimet at seimet dot de
                   ` (5 preceding siblings ...)
  2003-11-19 15:24 ` bangerth at dealii dot org
@ 2003-11-19 15:25 ` bangerth at dealii dot org
  2003-11-20  3:19 ` gdr at integrable-solutions dot net
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: bangerth at dealii dot org @ 2003-11-19 15:25 UTC (permalink / raw)
  To: gcc-bugs



-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement
             Status|UNCONFIRMED                 |NEW
     Ever Confirmed|                            |1
   Last reconfirmed|0000-00-00 00:00:00         |2003-11-19 15:25:29
               date|                            |
            Summary|Inserting struct with       |Wanted: check in
                   |valarray in map fails       |valarray::operator= for
                   |                            |length of array


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


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

* [Bug libstdc++/13121] Wanted: check in valarray::operator= for length of array
  2003-11-19 10:12 [Bug c++/13121] New: Inserting struct with valarray in map fails Uwe dot Seimet at seimet dot de
                   ` (6 preceding siblings ...)
  2003-11-19 15:25 ` [Bug libstdc++/13121] Wanted: check in valarray::operator= for length of array bangerth at dealii dot org
@ 2003-11-20  3:19 ` gdr at integrable-solutions dot net
  2003-11-20  3:22 ` gdr at integrable-solutions dot net
  2004-06-18  4:45 ` pinskia at gcc dot gnu dot org
  9 siblings, 0 replies; 11+ messages in thread
From: gdr at integrable-solutions dot net @ 2003-11-20  3:19 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From gdr at integrable-solutions dot net  2003-11-20 03:19 -------
Subject: Re:  Inserting struct with valarray in map fails

"Uwe dot Seimet at seimet dot de" <gcc-bugzilla@gcc.gnu.org> writes:

| Thank you for your rapid response! So gcc is obviously right, and our code
| is wrong. Nevertheless we think that the standard is a bit strange, allowing
| undefined behavior for an assignment.

Another view is that valarray is not designed for that kind of
manipulation.  If you want an auto-resizing valarray, maybe you would
like to wrap the standard one with something that takes care of
resizing. 

-- Gaby


-- 


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


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

* [Bug libstdc++/13121] Wanted: check in valarray::operator= for length of array
  2003-11-19 10:12 [Bug c++/13121] New: Inserting struct with valarray in map fails Uwe dot Seimet at seimet dot de
                   ` (7 preceding siblings ...)
  2003-11-20  3:19 ` gdr at integrable-solutions dot net
@ 2003-11-20  3:22 ` gdr at integrable-solutions dot net
  2004-06-18  4:45 ` pinskia at gcc dot gnu dot org
  9 siblings, 0 replies; 11+ messages in thread
From: gdr at integrable-solutions dot net @ 2003-11-20  3:22 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From gdr at integrable-solutions dot net  2003-11-20 03:22 -------
Subject: Re:  Inserting struct with valarray in map fails

"bangerth at dealii dot org" <gcc-bugzilla@gcc.gnu.org> writes:

| The reason why the standard says this is undefined behavior is that
| the creators of valarray wanted to have maximum speed for this class.

Valarray was designed to be a low level building block for superscalar
computers.  In particular, jumps are "conceptually" minimized.

| A check for the length and possibly throwing an exception might sacrifice
| this goal, although it would of course be much cheaper than the actual
| memory allocation this "feature" is to prevent. I leave this up to
| the libstdc++ maintainers -- maybe their new debug mode lib has this
| fixed already.

I have no plan to go there.

-- Gaby


-- 


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


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

* [Bug libstdc++/13121] Wanted: check in valarray::operator= for length of array
  2003-11-19 10:12 [Bug c++/13121] New: Inserting struct with valarray in map fails Uwe dot Seimet at seimet dot de
                   ` (8 preceding siblings ...)
  2003-11-20  3:22 ` gdr at integrable-solutions dot net
@ 2004-06-18  4:45 ` pinskia at gcc dot gnu dot org
  9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2004-06-18  4:45 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pinskia at gcc dot gnu dot org  2004-06-18 04:45 -------
Not going to happen, this is undefined so staying at that.

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |WONTFIX


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


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

end of thread, other threads:[~2004-06-18  4:45 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-11-19 10:12 [Bug c++/13121] New: Inserting struct with valarray in map fails Uwe dot Seimet at seimet dot de
2003-11-19 14:34 ` [Bug libstdc++/13121] " bangerth at dealii dot org
2003-11-19 14:47 ` bangerth at dealii dot org
2003-11-19 15:00 ` Uwe dot Seimet at seimet dot de
2003-11-19 15:04 ` bangerth at dealii dot org
2003-11-19 15:12 ` Uwe dot Seimet at seimet dot de
2003-11-19 15:24 ` bangerth at dealii dot org
2003-11-19 15:25 ` [Bug libstdc++/13121] Wanted: check in valarray::operator= for length of array bangerth at dealii dot org
2003-11-20  3:19 ` gdr at integrable-solutions dot net
2003-11-20  3:22 ` gdr at integrable-solutions dot net
2004-06-18  4:45 ` pinskia at gcc dot gnu dot org

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