public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* RE: Is this a bug?
@ 2003-08-21  9:23 Lev Assinovsky
  0 siblings, 0 replies; 14+ messages in thread
From: Lev Assinovsky @ 2003-08-21  9:23 UTC (permalink / raw)
  To: Alexandre Oliva; +Cc: Gcc-Bugs (E-mail), Gcc-Help (E-mail)

The test case passed under gcc 3.3.1.
The problem was in lack of SOM weak support in gcc prior 3.3

----
Lev Assinovsky
Aelita Software Corporation
O&S Core Division, Programmer
ICQ# 165072909


> -----Original Message-----
> From: Alexandre Oliva [mailto:aoliva@redhat.com]
> Sent: Tuesday, August 12, 2003 10:57 PM
> To: Lev Assinovsky
> Cc: Gcc-Bugs (E-mail); Gcc-Help (E-mail)
> Subject: Re: Is this a bug?
> 
> 
> On Aug 12, 2003, "Lev Assinovsky" 
> <LAssinovsky@algorithm.aelita.com> wrote:
> 
> > gcc 3.2.3, HPUX-11.00:
> 
> > t.cpp: In static member function `static const item* A::f()':
> > t.cpp:6: warning: sorry: semantics of inline function 
> static data `const item 
> >    arr[1]' are wrong (you'll wind up with multiple copies)
> > t.cpp:6: warning:   you can work around this by removing 
> the initializer
> 
> It's not as much of a bug, but rather a limitation AFAIK imposed by
> the object file of the platform you've chosen.  GCC requires weak
> symbols in order to implement this feature correctly, and 32-bit HP-UX
> object files don't support them (completely?).
> 
> -- 
> Alexandre Oliva   Enjoy Guarana', see http://www.ic.unicamp.br/~oliva/
> Red Hat GCC Developer                 aoliva@{redhat.com, gcc.gnu.org}
> CS PhD student at IC-Unicamp        oliva@{lsd.ic.unicamp.br, gnu.org}
> Free Software Evangelist                Professional serial bug killer
> 

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

* Re: Is this a bug?
@ 2009-07-17  4:23 Bill McEnaney
  0 siblings, 0 replies; 14+ messages in thread
From: Bill McEnaney @ 2009-07-17  4:23 UTC (permalink / raw)
  To: Bob Plantz, Ian Lance Taylor, Honggang Xu, Andrew Haley,
	David Daney, gcc-help

Bob told us his preference.  So I'll admit my preferences: Haskell,
Ocaml, Prolog, Python, and Lisp, probably in that order.

Bill

> On Thu, 2009-07-16 at 17:31 -0700, Ian Lance Taylor wrote:
> > For an integer type there is no performance difference between ++x and
> > x++.
> > 
> > For a C++ class which defines operator++ and a copy constructor, there
> > can be performance differences between ++x and x++.  The latter often
> > requires the compiler to invoke the copy constructor to hold onto the
> > old value before invoking operator++.  So if you are writing C++, it's
> > often a good idea to write ++x when x is a class type, such as an STL
> > iterator.  The compiler can sometimes eliminate the copy constructor,
> > but not always.
> > 
> > Ian
> 
> Thank you, Ian. Being mostly a C programmer, I often forget about the
> things that C++ does behind the scenes.
> 
> My real preference is assembly language. Then it's just me and the
> machine. ;)
> 
> Bob
> 
> 
> 

________________________________________________________________
Please visit a saintly hero:
http://www.jakemoore.org

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

* Re: Is this a bug?
  2009-07-17  0:31           ` Ian Lance Taylor
@ 2009-07-17  2:48             ` Bob Plantz
  0 siblings, 0 replies; 14+ messages in thread
From: Bob Plantz @ 2009-07-17  2:48 UTC (permalink / raw)
  To: Ian Lance Taylor; +Cc: Honggang Xu, Andrew Haley, David Daney, gcc-help

On Thu, 2009-07-16 at 17:31 -0700, Ian Lance Taylor wrote:
> For an integer type there is no performance difference between ++x and
> x++.
> 
> For a C++ class which defines operator++ and a copy constructor, there
> can be performance differences between ++x and x++.  The latter often
> requires the compiler to invoke the copy constructor to hold onto the
> old value before invoking operator++.  So if you are writing C++, it's
> often a good idea to write ++x when x is a class type, such as an STL
> iterator.  The compiler can sometimes eliminate the copy constructor,
> but not always.
> 
> Ian

Thank you, Ian. Being mostly a C programmer, I often forget about the
things that C++ does behind the scenes.

My real preference is assembly language. Then it's just me and the
machine. ;)

Bob


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

* Re: Is this a bug?
  2009-07-16 19:54         ` Bob Plantz
@ 2009-07-17  0:31           ` Ian Lance Taylor
  2009-07-17  2:48             ` Bob Plantz
  0 siblings, 1 reply; 14+ messages in thread
From: Ian Lance Taylor @ 2009-07-17  0:31 UTC (permalink / raw)
  To: Bob Plantz; +Cc: Honggang Xu, Andrew Haley, David Daney, gcc-help

Bob Plantz <plantz@cds1.net> writes:

> I have often heard students say they were told that ++x is more
> efficient than x++. From looking at gcc-generated assembly language, I
> knew this is not always true. It's been pointed out here that the
> situation is more "interesting" than I realized.

For an integer type there is no performance difference between ++x and
x++.

For a C++ class which defines operator++ and a copy constructor, there
can be performance differences between ++x and x++.  The latter often
requires the compiler to invoke the copy constructor to hold onto the
old value before invoking operator++.  So if you are writing C++, it's
often a good idea to write ++x when x is a class type, such as an STL
iterator.  The compiler can sometimes eliminate the copy constructor,
but not always.

Ian

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

* RE: Is this a bug?
  2009-07-16 18:28       ` Honggang Xu
@ 2009-07-16 19:54         ` Bob Plantz
  2009-07-17  0:31           ` Ian Lance Taylor
  0 siblings, 1 reply; 14+ messages in thread
From: Bob Plantz @ 2009-07-16 19:54 UTC (permalink / raw)
  To: Honggang Xu; +Cc: Andrew Haley, David Daney, gcc-help

This was also a learning experience for me.

I have often heard students say they were told that ++x is more
efficient than x++. From looking at gcc-generated assembly language, I
knew this is not always true. It's been pointed out here that the
situation is more "interesting" than I realized.

By the way, I don't like complicated expressions because I find them
more difficult to read and change. I would've written this example as

#include <stdio.h>
int main()
{
    volatile int  x = 20,y = 35;
    x = y + x;
    x += 2;
    y += 2;
    y = y + x;
    printf("x=%d  y=%d\n" ,x,y);
    return 0;
}

Now the volatile type qualifier works independently from the algorithm,
the way god intended. (Of course, "volatile" implies we should expect
the unexpected.)



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

* RE: Is this a bug?
  2009-07-16 18:22     ` Andrew Haley
@ 2009-07-16 18:28       ` Honggang Xu
  2009-07-16 19:54         ` Bob Plantz
  0 siblings, 1 reply; 14+ messages in thread
From: Honggang Xu @ 2009-07-16 18:28 UTC (permalink / raw)
  To: Andrew Haley, David Daney; +Cc: gcc-help

Thank both of you, I didn't think the problem in right way -- the
behavior is undefined, I have been thinking it was caused by volatile
key word, your explanation makes me released. -Honggang 

-----Original Message-----
From: Andrew Haley [mailto:aph@redhat.com] 
Sent: Thursday, July 16, 2009 11:22 AM
To: David Daney
Cc: Honggang Xu; gcc-help@gcc.gnu.org
Subject: Re: Is this a bug?

David Daney wrote:
> Honggang Xu wrote:
>> Or my understand of keyword "volatile" is wrong, following code 
>> outputs compiled by gcc 4.1.1:  x=22 ,y=59
>>  
>> main()
>> {
>> volatile int  x=20,y=35;
>> x=y++ + x++;
>> y= ++y + ++x;
>> printf("x=%d  y=%d\n" ,x,y);
>>  
>> }
>>
> 
> Your program has undefined behavior, the volatile may change the 
> output, but it doesn't change the fact that its behavior is undefined.
> 
> The problem is that the affect of the increment operator can take 
> place either before or after the affect of the assignment.  The 
> compiler can order the affects any way that it desires between
sequence points.

Actually, that's not quite true: as this expression exhibits undefined
behaviour, the compiler can return absolutely anything: it isn't limited
to just where it does the increment.

Andrew.

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

* Re: Is this a bug?
  2009-07-16 18:19   ` David Daney
@ 2009-07-16 18:22     ` Andrew Haley
  2009-07-16 18:28       ` Honggang Xu
  0 siblings, 1 reply; 14+ messages in thread
From: Andrew Haley @ 2009-07-16 18:22 UTC (permalink / raw)
  To: David Daney; +Cc: Honggang Xu, gcc-help

David Daney wrote:
> Honggang Xu wrote:
>> Or my understand of keyword "volatile" is wrong, following code outputs
>> compiled by gcc 4.1.1:  x=22 ,y=59
>>  
>> main()
>> {
>> volatile int  x=20,y=35;
>> x=y++ + x++;
>> y= ++y + ++x;
>> printf("x=%d  y=%d\n" ,x,y);
>>  
>> }
>>
> 
> Your program has undefined behavior, the volatile may change the output,
> but it doesn't change the fact that its behavior is undefined.
> 
> The problem is that the affect of the increment operator can take place
> either before or after the affect of the assignment.  The compiler can
> order the affects any way that it desires between sequence points.

Actually, that's not quite true: as this expression exhibits undefined
behaviour, the compiler can return absolutely anything: it isn't limited
to just where it does the increment.

Andrew.

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

* Re: Is this a bug?
  2009-07-16 18:07 ` Honggang Xu
  2009-07-16 18:17   ` Andrew Haley
@ 2009-07-16 18:19   ` David Daney
  2009-07-16 18:22     ` Andrew Haley
  1 sibling, 1 reply; 14+ messages in thread
From: David Daney @ 2009-07-16 18:19 UTC (permalink / raw)
  To: Honggang Xu; +Cc: gcc-help

Honggang Xu wrote:
> Or my understand of keyword "volatile" is wrong, following code outputs
> compiled by gcc 4.1.1:  x=22 ,y=59
>  
> main()
> {
> volatile int  x=20,y=35;
> x=y++ + x++;
> y= ++y + ++x;
> printf("x=%d  y=%d\n" ,x,y);
>  
> }
> 

Your program has undefined behavior, the volatile may change the output, 
but it doesn't change the fact that its behavior is undefined.

The problem is that the affect of the increment operator can take place 
either before or after the affect of the assignment.  The compiler can 
order the affects any way that it desires between sequence points.

You might want to read up on sequence points: 
http://en.wikipedia.org/wiki/Sequence_point

David Daney

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

* Re: Is this a bug?
  2009-07-16 18:07 ` Honggang Xu
@ 2009-07-16 18:17   ` Andrew Haley
  2009-07-16 18:19   ` David Daney
  1 sibling, 0 replies; 14+ messages in thread
From: Andrew Haley @ 2009-07-16 18:17 UTC (permalink / raw)
  To: Honggang Xu; +Cc: gcc-help

Honggang Xu wrote:
> Or my understand of keyword "volatile" is wrong, following code outputs
> compiled by gcc 4.1.1:  x=22 ,y=59
>  
> main()
> {
> volatile int  x=20,y=35;
> x=y++ + x++;
> y= ++y + ++x;
> printf("x=%d  y=%d\n" ,x,y);
>  
> }

No, it's not a gcc bug: your program exhibits undefined behaviour.

http://c-faq.com/expr/ieqiplusplus.html

Andrew.

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

* RE: Is this a bug?
       [not found] <DDFD17CC94A9BD49A82147DDF7D545C50176D5AE@exchange.ZeugmaSystems.local>
@ 2009-07-16 18:07 ` Honggang Xu
  2009-07-16 18:17   ` Andrew Haley
  2009-07-16 18:19   ` David Daney
  0 siblings, 2 replies; 14+ messages in thread
From: Honggang Xu @ 2009-07-16 18:07 UTC (permalink / raw)
  To: gcc-help

Or my understand of keyword "volatile" is wrong, following code outputs
compiled by gcc 4.1.1:  x=22 ,y=59
 
main()
{
volatile int  x=20,y=35;
x=y++ + x++;
y= ++y + ++x;
printf("x=%d  y=%d\n" ,x,y);
 
}

thanks
Honggang
 

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

* Re: Is this a bug?
  2003-08-14  9:47 Lev Assinovsky
@ 2003-08-15  5:04 ` Alexandre Oliva
  0 siblings, 0 replies; 14+ messages in thread
From: Alexandre Oliva @ 2003-08-15  5:04 UTC (permalink / raw)
  To: Lev Assinovsky; +Cc: Gcc-Bugs (E-mail), Gcc-Help (E-mail)

On Aug 14, 2003, "Lev Assinovsky" <LAssinovsky@algorithm.aelita.com> wrote:

> But the program works after that warning.

Not if you relied on the uniqueness of the static variable across
translation units.  What the warning is telling you is that they won't
be unified, which is required by the standard, but can't be
implemented in this object file format (AFAIK).

-- 
Alexandre Oliva   Enjoy Guarana', see http://www.ic.unicamp.br/~oliva/
Red Hat GCC Developer                 aoliva@{redhat.com, gcc.gnu.org}
CS PhD student at IC-Unicamp        oliva@{lsd.ic.unicamp.br, gnu.org}
Free Software Evangelist                Professional serial bug killer

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

* RE: Is this a bug?
@ 2003-08-14  9:47 Lev Assinovsky
  2003-08-15  5:04 ` Alexandre Oliva
  0 siblings, 1 reply; 14+ messages in thread
From: Lev Assinovsky @ 2003-08-14  9:47 UTC (permalink / raw)
  To: Alexandre Oliva; +Cc: Gcc-Bugs (E-mail), Gcc-Help (E-mail)

But the program works after that warning.

----
Lev Assinovsky
Aelita Software Corporation
O&S Core Division, Programmer
ICQ# 165072909


> -----Original Message-----
> From: Alexandre Oliva [mailto:aoliva@redhat.com]
> Sent: Tuesday, August 12, 2003 10:57 PM
> To: Lev Assinovsky
> Cc: Gcc-Bugs (E-mail); Gcc-Help (E-mail)
> Subject: Re: Is this a bug?
> 
> 
> On Aug 12, 2003, "Lev Assinovsky" 
> <LAssinovsky@algorithm.aelita.com> wrote:
> 
> > gcc 3.2.3, HPUX-11.00:
> 
> > t.cpp: In static member function `static const item* A::f()':
> > t.cpp:6: warning: sorry: semantics of inline function 
> static data `const item 
> >    arr[1]' are wrong (you'll wind up with multiple copies)
> > t.cpp:6: warning:   you can work around this by removing 
> the initializer
> 
> It's not as much of a bug, but rather a limitation AFAIK imposed by
> the object file of the platform you've chosen.  GCC requires weak
> symbols in order to implement this feature correctly, and 32-bit HP-UX
> object files don't support them (completely?).
> 
> -- 
> Alexandre Oliva   Enjoy Guarana', see http://www.ic.unicamp.br/~oliva/
> Red Hat GCC Developer                 aoliva@{redhat.com, gcc.gnu.org}
> CS PhD student at IC-Unicamp        oliva@{lsd.ic.unicamp.br, gnu.org}
> Free Software Evangelist                Professional serial bug killer
> 

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

* Re: Is this a bug?
  2003-08-12 16:27 Lev Assinovsky
@ 2003-08-12 18:57 ` Alexandre Oliva
  0 siblings, 0 replies; 14+ messages in thread
From: Alexandre Oliva @ 2003-08-12 18:57 UTC (permalink / raw)
  To: Lev Assinovsky; +Cc: Gcc-Bugs (E-mail), Gcc-Help (E-mail)

On Aug 12, 2003, "Lev Assinovsky" <LAssinovsky@algorithm.aelita.com> wrote:

> gcc 3.2.3, HPUX-11.00:

> t.cpp: In static member function `static const item* A::f()':
> t.cpp:6: warning: sorry: semantics of inline function static data `const item 
>    arr[1]' are wrong (you'll wind up with multiple copies)
> t.cpp:6: warning:   you can work around this by removing the initializer

It's not as much of a bug, but rather a limitation AFAIK imposed by
the object file of the platform you've chosen.  GCC requires weak
symbols in order to implement this feature correctly, and 32-bit HP-UX
object files don't support them (completely?).

-- 
Alexandre Oliva   Enjoy Guarana', see http://www.ic.unicamp.br/~oliva/
Red Hat GCC Developer                 aoliva@{redhat.com, gcc.gnu.org}
CS PhD student at IC-Unicamp        oliva@{lsd.ic.unicamp.br, gnu.org}
Free Software Evangelist                Professional serial bug killer

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

* Is this a bug?
@ 2003-08-12 16:27 Lev Assinovsky
  2003-08-12 18:57 ` Alexandre Oliva
  0 siblings, 1 reply; 14+ messages in thread
From: Lev Assinovsky @ 2003-08-12 16:27 UTC (permalink / raw)
  To: Gcc-Bugs (E-mail), Gcc-Help (E-mail)

gcc 3.2.3, HPUX-11.00:

typedef struct  { int a, b; } item; 
class A {
public:
    static const item *  f()
        {
            static const item  arr[] = {{0,0}}; 
            return arr;
        }
};

The result:

t.cpp: In static member function `static const item* A::f()':
t.cpp:6: warning: sorry: semantics of inline function static data `const item 
   arr[1]' are wrong (you'll wind up with multiple copies)
t.cpp:6: warning:   you can work around this by removing the initializer

----
Lev Assinovsky
Aelita Software Corporation
O&S Core Division, Programmer
ICQ# 165072909

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

end of thread, other threads:[~2009-07-17  4:23 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-08-21  9:23 Is this a bug? Lev Assinovsky
  -- strict thread matches above, loose matches on Subject: below --
2009-07-17  4:23 Bill McEnaney
     [not found] <DDFD17CC94A9BD49A82147DDF7D545C50176D5AE@exchange.ZeugmaSystems.local>
2009-07-16 18:07 ` Honggang Xu
2009-07-16 18:17   ` Andrew Haley
2009-07-16 18:19   ` David Daney
2009-07-16 18:22     ` Andrew Haley
2009-07-16 18:28       ` Honggang Xu
2009-07-16 19:54         ` Bob Plantz
2009-07-17  0:31           ` Ian Lance Taylor
2009-07-17  2:48             ` Bob Plantz
2003-08-14  9:47 Lev Assinovsky
2003-08-15  5:04 ` Alexandre Oliva
2003-08-12 16:27 Lev Assinovsky
2003-08-12 18:57 ` Alexandre Oliva

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