LINUX.IE, website of the Irish Linux Users' Group
Tux rules!

   
Home
New Users
Articles
Download
Projects
Community
Vendors

  Print Version
Email to...
 
Archives:


planetILUG

Recent News

News Archive


Join the
ILUG
on FaceBook


Join the
ILUG
on LinkedIn


Join the
ILUG SETI
Group



















 
 :: Mailing Lists

[ILUG] gcc optimisation weirdness?

[ILUG] gcc optimisation weirdness?

Kenn Humborg kenn at bluetree.ie
Wed May 10 17:50:34 IST 2000


> gcc seems to do weird things to the following:
>
> int input, frequency[9];
>
> //frequency[] initialised to 0.
>
> do {
>                 scanf("%d", &input);
>                 if (input > 0 && input < 11) {
> 			//point 1
>                         (frequency[input-1])++;
> 			//point 2
>                         }
>         } while (input);
>
> if i compile with -O2 or -O3 it works as i expect it to, but if i compile
> with "gcc -o test test.c" then:
>
> it works correctly for digits 0 thru 9, however if i enter 10 it goes
> wrong: at point 1 input == 10, at point 2 input == 11!

Very odd...  I'd expect that the statement

   (f[i])++

would not be legal.  Is (var) a legal lvalue?  Aha... info gcc
(under the C Extensions section) says that

   File: gcc.info,  Node: Lvalues,  Next: Conditionals,  Prev: Typeof,  Up:
C Extensions

   Generalized Lvalues
   ===================

      Compound expressions, conditional expressions and casts are allowed
   as lvalues provided their operands are lvalues.  This means that you
   can take their addresses or store values into them.

      Standard C++ allows compound expressions and conditional expressions
   as lvalues, and permits casts to reference type, so use of this
   extension is deprecated for C++ code.

      For example, a compound expression can be assigned, provided the last
   expression in the sequence is an lvalue.  These two expressions are
   equivalent:

        (a, b) += 5
        a, (b += 5)

      Similarly, the address of the compound expression can be taken.
   These two expressions are equivalent:

        &(a, b)
        a, &b

      A conditional expression is a valid lvalue if its type is not void
   and the true and false branches are both valid lvalues.  For example,
   these two expressions are equivalent:

        (a ? b : c) = 5
        (a ? b = 5 : (c = 5))

Hmmm...

> PS: is there anyway to initialise an array at declaration time, rather
> than iterate thru the array and set each element manually? eg something
> like: int array[9]=0;

You mean like:

   int array[9]={0, 1, 2, 3, 4, 3, 2, 1, 0};

That is standard C.  GCC also has some extensions that allow
you to do fancied stuff with initializers.  Take a look at
the C and C++ Extensions sections in the GCC info pages.

Later,
Kenn






More information about the ILUG mailing list
Read this without the formatting.
                                                                                                    

 

Hosted by HEAnet


Maintained by the ILUG website team. The aim of Linux.ie is to support and help commercial and private users of Linux in Ireland. You can display ILUG news in your own webpages, read backend information to find out how. Networking services kindly provided by HEAnet, server kindly donated by Dell. Linux is a trademark of Linus Torvalds, used with permission. No penguins were harmed in the production or maintenance of this highly praised website. Looking for the Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!
RSS Version
Powered by Dell