static array with variable amount of elements??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ni@m

    #1

    static array with variable amount of elements??

    I found out smth strange in g++ >= 3.3.5 =>
    I was able to do
    register char buff[outSTDBuffer];//// where
    outSTDBuffer is a variable!!!!

    Is it a bug in gcc or specification of c++ changed?? I've noticed this
    a mistake after successfull compilation of my program!

    Should I change the code. I think `register char buff[outSTDBuffer];`
    is much safer then use pointers in case if outSTDBuffer is small!!!

  • Neelesh Bodas

    #2
    Re: static array with variable amount of elements??

    Ni@m wrote:[color=blue]
    > I found out smth strange in g++ >= 3.3.5 =>
    > I was able to do
    > register char buff[outSTDBuffer];//// where
    > outSTDBuffer is a variable!!!!
    >
    > Is it a bug in gcc or specification of c++ changed?? I've noticed this
    > a mistake after successfull compilation of my program!
    >[/color]

    Not according to standard. Its one of those gnu extensions to C89 (and
    C++)

    [color=blue]
    > Should I change the code.[/color]

    Yes if you want it to be portable, and C++ std compliant.

    Comment

    • Ni@m

      #3
      Re: static array with variable amount of elements??

      Thanks!!! Is hould change that!

      Comment

      • albcamus

        #4
        Re: static array with variable amount of elements??

        Neelesh Bodas дµÀ:[color=blue]
        > Not according to standard. Its one of those gnu extensions to C89 (and
        > C++)
        > http://gcc.gnu.org/onlinedocs/gcc/Va...ariable-Length[/color]
        C99 supports this feature, too. :)

        Comment

        • Jack Klein

          #5
          Re: static array with variable amount of elements??

          On Fri, 06 Jan 2006 09:44:34 +0800, albcamus <albcamus@hit.e du.cn>
          wrote in comp.lang.c++:
          [color=blue]
          > Neelesh Bodas дµÀ:[color=green]
          > > Not according to standard. Its one of those gnu extensions to C89 (and
          > > C++)
          > > http://gcc.gnu.org/onlinedocs/gcc/Va...ariable-Length[/color]
          > C99 supports this feature, too. :)[/color]

          In general, yes, but not in this case. I've put back the OP's code
          snippet:
          [color=blue]
          > register char buff[outSTDBuffer];//// where[/color]

          Defining register variable of array type produces undefined behavior
          in C, because any attempt to use the array in any way, even with a
          subscript, results in the address of its first element being taken.
          Taking the address of a register variable in C is illegal.

          --
          Jack Klein
          Home: http://JK-Technology.Com
          FAQs for
          comp.lang.c http://c-faq.com/
          comp.lang.c++ http://www.parashift.com/c++-faq-lite/
          alt.comp.lang.l earn.c-c++

          Comment

          • Peter T. Breuer

            #6
            Re: static array with variable amount of elements??

            In comp.os.linux.d evelopment.syst em Jack Klein <jackklein@spam cop.net> wrote:[color=blue]
            > On Fri, 06 Jan 2006 09:44:34 +0800, albcamus <albcamus@hit.e du.cn>
            > wrote in comp.lang.c++:[/color]
            [color=blue][color=green]
            >> Neelesh Bodas ____:[color=darkred]
            >> > Not according to standard. Its one of those gnu extensions to C89 (and
            >> > C++)
            >> > http://gcc.gnu.org/onlinedocs/gcc/Va...ariable-Length[/color]
            >> C99 supports this feature, too. :)[/color][/color]
            [color=blue]
            > In general, yes, but not in this case. I've put back the OP's code
            > snippet:[/color]
            [color=blue][color=green]
            >> register char buff[outSTDBuffer];//// where[/color][/color]
            [color=blue]
            > Defining register variable of array type produces undefined behavior
            > in C, because any attempt to use the array in any way, even with a
            > subscript, results in the address of its first element being taken.
            > Taking the address of a register variable in C is illegal.[/color]

            "Register" is only a programmer's request - the compiler is not
            required to obey. One would deduce that it hasn't, since it can't.

            Peter

            Comment

            • Ni@m

              #7
              Re: static array with variable amount of elements??

              So, if it's possible to use `char buff[outSTDBuffer];` what
              compilators supports this?? Now I definetily know that gcc supports
              that and some other beautifull things but if smbd wants to compile this
              code for example with icc or VC 6.0?? Because some parts of my code
              have `throw blocks` and it's really suitable to use stach array!! I
              shouldn't delete[] it in any exception!!

              Comment

              • Neelesh Bodas

                #8
                Re: static array with variable amount of elements??


                Ni@m wrote:[color=blue]
                > So, if it's possible to use `char buff[outSTDBuffer];`[/color]

                it is NOT according to std c++. Period.

                First step is to make sure whether you want to code in c89 or c99 or
                std c++ or some compiler's C++.
                [color=blue]
                > but if smbd wants to compile this code for example with icc or VC 6.0??[/color]

                That is to say you want a _portable_ code. Donot write such a code
                then,

                Why not use std::vector instead?

                Comment

                • Michiel.Salters@tomtom.com

                  #9
                  Re: static array with variable amount of elements??


                  Peter T. Breuer wrote:[color=blue]
                  > In comp.os.linux.d evelopment.syst em Jack Klein <jackklein@spam cop.net> wrote:[color=green]
                  > > On Fri, 06 Jan 2006 09:44:34 +0800, albcamus <albcamus@hit.e du.cn>
                  > > wrote in comp.lang.c++:[/color]
                  >[color=green][color=darkred]
                  > >> Neelesh Bodas ____:
                  > >> register char buff[outSTDBuffer];//// where[/color][/color]
                  >[color=green]
                  > > Defining register variable of array type produces undefined behavior
                  > > in C, because any attempt to use the array in any way, even with a
                  > > subscript, results in the address of its first element being taken.
                  > > Taking the address of a register variable in C is illegal.[/color]
                  >
                  > "Register" is only a programmer's request - the compiler is not
                  > required to obey. One would deduce that it hasn't, since it can't.[/color]

                  That sounds reasonable, but it doesn't work that way. The compiler
                  isn't
                  required to obey anything anymore, as the code causes undefined
                  behavior.

                  The logic is similar to like ' a /= 0; ' // it can't do this, so it
                  hasn't?

                  HTH,
                  Michiel Salters

                  Comment

                  • Peter T. Breuer

                    #10
                    Re: static array with variable amount of elements??

                    In comp.os.linux.d evelopment.syst em Michiel.Salters @tomtom.com wrote:
                    [color=blue]
                    > Peter T. Breuer wrote:[color=green]
                    >> In comp.os.linux.d evelopment.syst em Jack Klein <jackklein@spam cop.net> wrote:[color=darkred]
                    >> > On Fri, 06 Jan 2006 09:44:34 +0800, albcamus <albcamus@hit.e du.cn>
                    >> > wrote in comp.lang.c++:[/color]
                    >>[color=darkred]
                    >> >> Neelesh Bodas ____:
                    >> >> register char buff[outSTDBuffer];//// where[/color]
                    >>[color=darkred]
                    >> > Defining register variable of array type produces undefined behavior
                    >> > in C, because any attempt to use the array in any way, even with a
                    >> > subscript, results in the address of its first element being taken.
                    >> > Taking the address of a register variable in C is illegal.[/color]
                    >>
                    >> "Register" is only a programmer's request - the compiler is not
                    >> required to obey. One would deduce that it hasn't, since it can't.[/color][/color]
                    [color=blue]
                    > That sounds reasonable, but it doesn't work that way. The compiler
                    > isn't required to obey anything anymore, as the code causes undefined
                    > behavior.[/color]

                    You miss the point, I think. Perhaps it's different in C++ but
                    "register" is like "inline" in C, in that the compiler is not required
                    to obey the directive - indeed, it can't, since there are a limited
                    number of registers on the chip, and you can define an arbitrarily
                    large number of register variables.

                    Or are you perhaps saying that qualifying an array with "register"
                    is disallowed? In that case, the compiler ought to flag an error.

                    C90 requires automatic and register variables of aggregate type
                    (struct, array, or union) to have initializers containing only
                    constant expressions. (Many compilers do not adhere to this
                    restriction, however.)

                    (http://david.tribble.com/text/cdiffs.htm)
                    I deduce that register arrays are directly allowed for in the C90
                    standard, if that quote is accurate. They would have to be small
                    arrays, or else the compiler would have to do a no-op there.

                    C99 removes that restriction, allowing non-constant expressions to
                    be used in such initializers.

                    C++ allows non-constant expressions to be used in initializers for
                    automatic and register variables. (It also allows arbitrary
                    non-constant expressions to be used to initialize static and
                    external variables.)
                    [color=blue]
                    > The logic is similar to like ' a /= 0; ' // it can't do this, so it
                    > hasn't?[/color]

                    Well, it would have to be only a similarity, since a/=0 is perfectly
                    doable, at least for floats, and results in a well-defined undefined
                    result.


                    Peter

                    Comment

                    • Bill Marcum

                      #11
                      Re: static array with variable amount of elements??

                      ["Followup-To:" header set to comp.os.linux.d evelopment.syst em.]
                      On 5 Jan 2006 00:11:52 -0800, Ni@m
                      <niam.niam@gmai l.com> wrote:[color=blue]
                      > Thanks!!! Is hould change that!
                      >[/color]



                      --
                      He who lives without folly is less wise than he believes.

                      Comment

                      Working...