gcc means lots of fun

The gcc is a rich source of “interesting debugging experiences”. I was
informed this morning by a developer doing our port project, that gcc
frivolously rearranged order of some C++ code to assure that a NULL
pointer was dereferenced before being allocated. This, of course, was
after a long session with gdb banging through assembly code. Watcom on
QNX4 had no problem with this same code.

If there is interest, Josh will post details. I’m posting to express
fear and dismay at relying on gcc for C++ on RTP. It can easily drag
down what is otherwise an excellent OS. A reasonably priced alternative
is urgently needed. Perhaps the cfront-like C++ compiler mentioned
elsewhere? (can’t remember the name).

Dean Douthat <ddouthat@faac.com> wrote:
: The gcc is a rich source of “interesting debugging experiences”. I was
: informed this morning by a developer doing our port project, that gcc
: frivolously rearranged order of some C++ code to assure that a NULL
: pointer was dereferenced before being allocated. This, of course, was
: after a long session with gdb banging through assembly code. Watcom on
: QNX4 had no problem with this same code.

: If there is interest, Josh will post details. I’m posting to express

I would be curious to see this.

: fear and dismay at relying on gcc for C++ on RTP. It can easily drag
: down what is otherwise an excellent OS. A reasonably priced alternative
: is urgently needed. Perhaps the cfront-like C++ compiler mentioned
: elsewhere? (can’t remember the name).

Do you mean comeau C++? It is a c+±front that generate C code to be compile
by … gcc.

I don’t think anyone has any big problems with gcc compiling C code, it
is the C++ stuff that is worrysome.

-----Original Message-----
From: Alain Magloire [mailto:alain@qnx.com]
Posted At: Thursday, August 30, 2001 11:25 AM
Posted To: devtools
Conversation: gcc means lots of fun
Subject: Re: gcc means lots of fun


Dean Douthat <ddouthat@faac.com> wrote:
: The gcc is a rich source of “interesting debugging experiences”. I
was
: informed this morning by a developer doing our port project, that gcc
: frivolously rearranged order of some C++ code to assure that a NULL
: pointer was dereferenced before being allocated. This, of course, was
: after a long session with gdb banging through assembly code. Watcom
on
: QNX4 had no problem with this same code.

: If there is interest, Josh will post details. I’m posting to express

I would be curious to see this.

: fear and dismay at relying on gcc for C++ on RTP. It can easily drag
: down what is otherwise an excellent OS. A reasonably priced
alternative
: is urgently needed. Perhaps the cfront-like C++ compiler mentioned
: elsewhere? (can’t remember the name).

Do you mean comeau C++? It is a c+±front that generate C code to be
compile
by … gcc.

Alain Magloire wrote:

Dean Douthat <> ddouthat@faac.com> > wrote:
: The gcc is a rich source of “interesting debugging experiences”. I was
: informed this morning by a developer doing our port project, that gcc
: frivolously rearranged order of some C++ code to assure that a NULL
: pointer was dereferenced before being allocated. This, of course, was
: after a long session with gdb banging through assembly code. Watcom on
: QNX4 had no problem with this same code.

: If there is interest, Josh will post details. I’m posting to express

I would be curious to see this.

: fear and dismay at relying on gcc for C++ on RTP. It can easily drag
: down what is otherwise an excellent OS. A reasonably priced alternative
: is urgently needed. Perhaps the cfront-like C++ compiler mentioned
: elsewhere? (can’t remember the name).

Do you mean comeau C++? It is a c+±front that generate C code to be compile
by … gcc.

Yes, that’s it. My impression is that gcc does fine on C code but has many
problems with C++. If so, the comeau cfront might be attractive.

“Dean Douthat” <ddouthat@faac.com> wrote in message
news:3B8E98AF.5197BEB9@faac.com

Alain Magloire wrote:

Dean Douthat <> ddouthat@faac.com> > wrote:
: The gcc is a rich source of “interesting debugging experiences”. I
was
: informed this morning by a developer doing our port project, that gcc
: frivolously rearranged order of some C++ code to assure that a NULL
: pointer was dereferenced before being allocated. This, of course, was
: after a long session with gdb banging through assembly code. Watcom
on
: QNX4 had no problem with this same code.

: If there is interest, Josh will post details. I’m posting to express

I would be curious to see this.

: fear and dismay at relying on gcc for C++ on RTP. It can easily drag
: down what is otherwise an excellent OS. A reasonably priced
alternative
: is urgently needed. Perhaps the cfront-like C++ compiler mentioned
: elsewhere? (can’t remember the name).

Do you mean comeau C++? It is a c+±front that generate C code to be
compile
by … gcc.

Yes, that’s it. My impression is that gcc does fine on C code but has
many
problems with C++. If so, the comeau cfront might be attractive.

Yeah but then how would you debug the C++ code?

Hi. Listed below (and attached) are two C++ files. The one (bad.cpp),
when compiled using the command:

QCC -o bad bad.cpp

causes a memory fault when run; the other, good.cpp, when compiled
similarly, runs fine. On QNX 4 using Watcom, bad.cpp runs fine; on
Linux, using egcs (an older version, 2.91.66), bad.cpp also faults.

Josh Hamacher
FAAC Incorporated



#include <iostream.h>

struct Fred {
void *a;
void *b;
};

int main(int argc, char *argv[])
{
Fred fred;
Fred *fred_p = NULL;

fred_p->a = fred_p->b = fred_p = &fred;

return 0;
}

#include

struct Fred {
void *a;
void *b;
};

int main(int argc, char *argv[])
{
Fred fred;
Fred *fred_p = NULL;

fred_p = &fred;
fred_p->a = fred_p->b = &fred;

return 0;
}

Josh Hamacher <hamacher@faac.com> wrote:

struct Fred {
void *a;
void *b;
};

fred_p->a = fred_p->b = fred_p = &fred;

I don’t have a copy of the C++ standard, but in C, this is bad code.
Since there’s no sequence points between evaluation of the argument of
the assignment operator, it’s unspecified whether “fred_p” in
“fred_p->a” and “fred_p->b” refers to the old value or the new value of
fred_p.

\

Wojtek Lerch QNX Software Systems Ltd.

On Fri, 31 Aug 2001 15:11:47 -0400, Josh Hamacher <hamacher@faac.com>
wrote:

begin bad.cpp
#include <iostream.h

struct Fred {
void *a;
void *b;
};

int main(int argc, char *argv[])
{
Fred fred;
Fred *fred_p = NULL;

fred_p->a = fred_p->b = fred_p = &fred;

return 0;
}
end bad.cpp

Although compilers should conform to the standards, this

style of coding is asking for trouble regardles of what the
standards might say about the meaning of such constructs…

ako

I disagree. The assignment operator ‘=’ is a sequence operator. I.E. in
a = b = c;
c should always be assigned to b first and then b’s new value assigned to a.

Bill Caroselli


“Wojtek Lerch” <wojtek_l@yahoo.ca> wrote in message
news:9mp0li$b1h$1@nntp.qnx.com

Josh Hamacher <> hamacher@faac.com> > wrote:

struct Fred {
void *a;
void *b;
};

fred_p->a = fred_p->b = fred_p = &fred;

I don’t have a copy of the C++ standard, but in C, this is bad code.
Since there’s no sequence points between evaluation of the argument of
the assignment operator, it’s unspecified whether “fred_p” in
“fred_p->a” and “fred_p->b” refers to the old value or the new value of
fred_p.

\

Wojtek Lerch QNX Software Systems Ltd.

Agreed.

Bill Caroselli

“Andrzej Kocon” <ako@box43.gnet.pl> wrote in message
news:3b90dabd.3578146@inn.qnx.com

On Fri, 31 Aug 2001 15:11:47 -0400, Josh Hamacher <> hamacher@faac.com
wrote:

begin bad.cpp
#include <iostream.h

struct Fred {
void *a;
void *b;
};

int main(int argc, char *argv[])
{
Fred fred;
Fred *fred_p = NULL;

fred_p->a = fred_p->b = fred_p = &fred;

return 0;
}
end bad.cpp

Although compilers should conform to the standards, this
style of coding is asking for trouble regardles of what the
standards might say about the meaning of such constructs…

ako

Andrzej Kocon <ako@box43.gnet.pl> wrote:

Although compilers should conform to the standards, this
style of coding is asking for trouble regardles of what the
standards might say about the meaning of such constructs…

I think you have it backwards. If the standard says that your code is
not correct C, it doesn’t matter whether you think it’s coded in a good
style. If your code produces undefined behaviour, it’s bad code even if
the compiler decides to generate something that that does what you
expected…

\

Wojtek Lerch QNX Software Systems Ltd.

“Bill Caroselli (Q-TPS)” <qtps@earthlink.net> wrote:

I disagree. The assignment operator ‘=’ is a sequence operator. I.E. in
a = b = c;
c should always be assigned to b first and then b’s new value assigned to a.

What is a “sequence operator”?

Since the assignment does not introduce a sequence point, the order in
which new value are stored into a and b is unspecified. A compiler is
allowed to calculate the value(s) to be stored in a and b using
registers, and then copy the register(s) into a and b in any order.
(See the quote below.)


“Wojtek Lerch” <> wojtek_l@yahoo.ca> > wrote in message
news:9mp0li$b1h$> 1@nntp.qnx.com> …
Josh Hamacher <> hamacher@faac.com> > wrote:

struct Fred {
void *a;
void *b;
};

fred_p->a = fred_p->b = fred_p = &fred;

I don’t have a copy of the C++ standard, but in C, this is bad code.
Since there’s no sequence points between evaluation of the argument of
the assignment operator, it’s unspecified whether “fred_p” in
“fred_p->a” and “fred_p->b” refers to the old value or the new value of
fred_p.

Actually, it’s not merely unspecified – this code produces undefined
behaviour:

6.5 Expressions

2 Between the previous and next sequence point an object shall have
its stored value modified at most once by the evaluation of an
expression. Furthermore, the prior value shall be read only to
determine the value to be stored.

3 The grouping of operators and operands is indicated by the syntax.
Except as specified later (for the function-call (), &&, ||, ?:, and
comma operators), the order of evaluation of subexpressions and the
order in which side effects take place are both unspecified.


Wojtek Lerch QNX Software Systems Ltd.

On 1 Sep 2001 20:41:37 GMT, Wojtek Lerch <wojtek_l@yahoo.ca> wrote:

Andrzej Kocon <> ako@box43.gnet.pl> > wrote:
Although compilers should conform to the standards, this
style of coding is asking for trouble regardles of what the
standards might say about the meaning of such constructs…

I think you have it backwards. If the standard says that your code is
not correct C, it doesn’t matter whether you think it’s coded in a good
style. If your code produces undefined behaviour, it’s bad code even if
the compiler decides to generate something that that does what you
expected…

Wojtek Lerch QNX Software Systems Ltd.

This is far more than can be concluded from my statement.

You may safely restrain yourself from using certain programming
constructs, on any ground, without even referring to any standard.
Your criteria may be stronger than those implemented by the compiler.
A famous one is “Does the construct simplify program reading, or
understanding, or (pardon) proving it correct?” (and it does not rely
entirely on one’s taste). This is not the job of a programmer to
ponder the niceties of doubtful or tricky language expressions, valid
or not (unless he writes… a compiler).

Perhaps I should apologize for the term “style” which, besides
being too personal, might have encouraged the “backward” conclusion.
I didn’t recommend neglecting the language definition in favour of
common sense…

ako

Just for the record, I didn’t write that code; I’m porting an existing
app, and that code was already in it. I don’t much care for that style,
either, to tell you the truth.

I did, however, think that it was legal C++ (for the same reason as
Bill; I thought the assignment operator was always evaluated
right-to-left). Thanks for enlightening me.

Josh Hamacher
FAAC Incorporated

Andrzej Kocon <ako@box43.gnet.pl> wrote:

On 1 Sep 2001 20:41:37 GMT, Wojtek Lerch <> wojtek_l@yahoo.ca> > wrote:

Andrzej Kocon <> ako@box43.gnet.pl> > wrote:
Although compilers should conform to the standards, this
style of coding is asking for trouble regardles of what the
standards might say about the meaning of such constructs…

I think you have it backwards. If the standard says that your code is
not correct C, it doesn’t matter whether you think it’s coded in a good
style. If your code produces undefined behaviour, it’s bad code even if
the compiler decides to generate something that that does what you
expected…

This is far more than can be concluded from my statement.

Um… I didn’t really say what exactly I concluded from your statement,
did I… :wink:

My interpretation of what you originally said was that as long as your
code meets your criteria of good coding, it doesn’t matter what the
language standard says about it. My response was that if the language
standard says that your code is invalid, it doesn’t matter whether it
meets your criteria. But as long as your criteria include the
requirement that your code must not be invalid, there’s no real conflict
between these two statements, is there…

You may safely restrain yourself from using certain programming
constructs, on any ground, without even referring to any standard.
Your criteria may be stronger than those implemented by the compiler.

Usually, your criteria should be stronger than those implemented by
the compiler. The fact that your code compiles and works in one
environment is not a proof that it doesn’t produce undefined behaviour.
And if it does, you never know when it may stop working (unless you
wrote your compiler and have no intention of using a different one…).

A famous one is “Does the construct simplify program reading, or
understanding, or (pardon) proving it correct?” (and it does not rely
entirely on one’s taste). This is not the job of a programmer to
ponder the niceties of doubtful or tricky language expressions, valid
or not (unless he writes… a compiler).

But it is!

The job of a programmer includes fixing broken code. You can try to do
that by replacing all the code that looks tricky or doubtful to you with
code that doesn’t look tricky or doubtful; the problem is that in some
cases, invalid code may not look tricky or doubtful, and in those cases,
it’s good to be able to recognize that it is invalid. And that’s when
understanding subtleties of the language can be useful.

Even if you choose to restrict yourself to using a subset of the
language that you feel is safer or easier to understand than the full
language, it still is a good idea to make sure that the subset indeed is
a subset. Before you can safely say that you can ignore the real limits
of the language because it’s enough for you to know the limits of your
subset, you have to make sure that your subset does not cross the real
limits. And that might sometimes be difficult if you don’t understand
the full language well enough.

Perhaps I should apologize for the term “style” which, besides
being too personal, might have encouraged the “backward” conclusion.

Then perhaps I should apologize for using such a strong word…

I didn’t recommend neglecting the language definition in favour of
common sense…

I didn’t think you did – it would be against common sense, wouldn’t
it… :slight_smile:


Wojtek Lerch QNX Software Systems Ltd.

In article <3B8E7684.64A2CF@faac.com>, Dean Douthat <ddouthat@faac.com> wrote:

The gcc is a rich source of “interesting debugging experiences”. I was
informed this morning by a developer doing our port project, that gcc
frivolously rearranged order of some C++ code to assure that a NULL
pointer was dereferenced before being allocated. This, of course, was
after a long session with gdb banging through assembly code. Watcom on
QNX4 had no problem with this same code.

If there is interest, Josh will post details. I’m posting to express
fear and dismay at relying on gcc for C++ on RTP. It can easily drag
down what is otherwise an excellent OS. A reasonably priced alternative
is urgently needed. Perhaps the cfront-like C++ compiler mentioned
elsewhere? (can’t remember the name).

If sounds like you’re talking about us (Comeau Computing,
with Comeau C++). We have an eye on a bunch of new platforms,
QNX included. A few things need to play out for that to
occur though. In the meantime, you can certainly check out
the compilation of your C++ code with the Comeau online compiler.

Greg Comeau export ETA: Dec 1 10% “End of Summer” Offer
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware’s Libraries… Have you tried it?

In article <9mm0dl$dn5$1@nntp.qnx.com>, Alain Magloire <alain@qnx.com> wrote:

Dean Douthat <> ddouthat@faac.com> > wrote:
: A reasonably priced alternative
: is urgently needed. Perhaps the cfront-like C++ compiler mentioned
: elsewhere? (can’t remember the name).

Do you mean comeau C++? It is a c+±front that generate C code to be compile
by … gcc.

Just to nitpick: Comeau C++ is not cfront (the AT&T originated
product), although it functions similarly to it, and even has
various dialects to support cfront compatibility.

Greg Comeau export ETA: Dec 1 10% “End of Summer” Offer
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware’s Libraries… Have you tried it?

In article <9mmc6f$2qb$1@inn.qnx.com>,
Mario Charest <mcharest@remzinformatic.com> wrote:

… [Comeau C++] …

Yeah but then how would you debug the C++ code?

You’d como -g, and then use gdb, or does QNX have a different
debugger (most work anyway).

Greg Comeau export ETA: Dec 1 10% “End of Summer” Offer
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware’s Libraries… Have you tried it?

In article <3B8FE173.5060309@faac.com>,
Josh Hamacher <hamacher@faac.com> wrote:

Hi. Listed below (and attached) are two C++ files. The one (bad.cpp),
when compiled using the command:

QCC -o bad bad.cpp

causes a memory fault when run; the other, good.cpp, when compiled
similarly, runs fine.

Seem to me that bad.cpp is bad, whether it “runs fine” or not.

Greg Comeau export ETA: Dec 1 10% “End of Summer” Offer
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware’s Libraries… Have you tried it?

In article <9mr1s4$qt3$1@inn.qnx.com>,
Bill Caroselli (Q-TPS) <qtps@earthlink.net> wrote:

I disagree. The assignment operator ‘=’ is a sequence operator. I.E. in
a = b = c;
c should always be assigned to b first and then b’s new value assigned to a.

I think that you have precedence and order of evaluation confused
with something else. The bottom line is that the code from the other
post has a problem and shouldn’t be depended upon to produce any
meaningful result. That it may have produced meaningful results
just means that you were lucky (sic).

Greg Comeau export ETA: Dec 1 10% “End of Summer” Offer
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware’s Libraries… Have you tried it?