Array size as constant expression

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pvinodhkumar@gmail.com

    Array size as constant expression

    The number of elemets of the array, the array bound must be constant
    expression?Why is this restriction?

    Vinodh

  • Victor Bazarov

    #2
    Re: Array size as constant expression

    pvinodhkumar@gm ail.com wrote:[color=blue]
    > The number of elemets of the array, the array bound must be constant
    > expression?Why is this restriction?[/color]

    Because it's better (easier) that way.

    Comment

    • pvinodhkumar@gmail.com

      #3
      Re: Array size as constant expression

      Easier in case of the compiler implementation?
      Is it that difficult?I really don't know.

      Comment

      • Andrey Tarasevich

        #4
        Re: Array size as constant expression

        pvinodhkumar@gm ail.com wrote:[color=blue]
        > The number of elemets of the array, the array bound must be constant
        > expression?Why is this restriction?[/color]

        Firstly, because the current object model of C++ language requires that
        sizes of objects with static or automatic storage duration is known at
        compile time. Memory layout for static and automatic memory in C++ is
        determined at compile time. In order to do that the compiler needs to
        know the sizes of all objects, including arrays. (This condition was
        weakened in new C - C99, but it is still there in C++).

        Secondly, there's no such requirement for dynamic objects. For this
        reason, array size in 'new[]' expression is not required to be a
        constant expression.

        --
        Best regards,
        Andrey Tarasevich

        Comment

        • Victor Bazarov

          #5
          Re: Array size as constant expression

          pvinodhkumar@gm ail.com wrote:[color=blue]
          > Easier in case of the compiler implementation?
          > Is it that difficult?I really don't know.
          >[/color]

          What would be the size of an object which as a member has an array whose
          size is unknown? How do you work with an object of an unknown size? How
          do you access other members of such object? All of those offsets have to
          be decided at the run-time. Can you imagine the overhead it will cause?

          Comment

          • Andrew Koenig

            #6
            Re: Array size as constant expression

            <pvinodhkumar@g mail.com> wrote in message
            news:1102697365 .343356.206020@ f14g2000cwb.goo glegroups.com.. .
            [color=blue]
            > The number of elemets of the array, the array bound must be constant
            > expression?Why is this restriction?[/color]

            Because the size of an array is part of its type and C++ types are fixed
            during compilation.


            Comment

            • pvinodhkumar@gmail.com

              #7
              Re: Array size as constant expression

              But why there is no such restriction while allocating in heap?

              Andrew Koenig wrote:[color=blue]
              > <pvinodhkumar@g mail.com> wrote in message
              > news:1102697365 .343356.206020@ f14g2000cwb.goo glegroups.com.. .
              >[color=green]
              > > The number of elemets of the array, the array bound must be[/color][/color]
              constant[color=blue][color=green]
              > > expression?Why is this restriction?[/color]
              >
              > Because the size of an array is part of its type and C++ types are[/color]
              fixed[color=blue]
              > during compilation.[/color]

              Comment

              • pvinodhkumar@gmail.com

                #8
                Re: Array size as constant expression

                But why there is no such restriction while allocating in heap?

                Andrew Koenig wrote:[color=blue]
                > <pvinodhkumar@g mail.com> wrote in message
                > news:1102697365 .343356.206020@ f14g2000cwb.goo glegroups.com.. .
                >[color=green]
                > > The number of elemets of the array, the array bound must be[/color][/color]
                constant[color=blue][color=green]
                > > expression?Why is this restriction?[/color]
                >
                > Because the size of an array is part of its type and C++ types are[/color]
                fixed[color=blue]
                > during compilation.[/color]

                Comment

                • Alf P. Steinbach

                  #9
                  Re: Array size as constant expression

                  * pvinodhkumar@gm ail.com:[color=blue]
                  > [top-posting][/color]

                  Don't top-post. See the FAQ. Corrected (also corrected the quoting).

                  * pvinodhkumar@gm ail.com:[color=blue]
                  > * Andrew Koenig:[color=green]
                  > > * pvinodhkumar@gm ail.com:
                  > >[color=darkred]
                  > > > The number of elemets of the array, the array bound must be
                  > > > constant expression?Why is this restriction?[/color]
                  > >
                  > > Because the size of an array is part of its type and C++ types are
                  > > fixed during compilation.[/color]
                  >
                  > But why there is no such restriction while allocating in heap?[/color]

                  A C++ is a low level structure that directly is a contigous area
                  of memory -- in contrast to an array in e.g. Java or C#, which
                  is a fixed size pointer that points to some data structure which
                  might or might not be a contigous area of memory (doesn't matter).

                  In short, in C++ there's minimal abstraction between your array and
                  the raw memory, nothing that intercepts your stated operations.

                  Now consider the two basic ways an array can be allocated:

                  * As part of another object.

                  * Not as part of another object, i.e. free-standing.

                  When created as part of another object that object's memory layout
                  is fixed at compilation. More precisely, the offsets from the start
                  of the object to the start of each member are fixed at compilation.
                  So unless the array is the very last member it must be of fixed
                  length -- a variable length would change the offsets of the following
                  members at run-time, and so play havoc with the code that assumes fixed
                  offsets (again this is due to no intervening abstraction layer).

                  Summing up: part of another object -> fixed length, unless last member
                  of the object (this last possibility for variable length is offered as
                  a language extension by some compilers, but isn't supported by the std.).

                  Then consider the three ways an array can be allocated when it's not
                  defined by you as being part of another object:

                  * Statically (either outside any function or using the word
                  'static' inside a function).

                  * On the stack (local variable).

                  * On the heap.

                  If it's statically allocated then, although you haven't specified that
                  it's part of another object, at the level of memory layout it is: it's
                  squeezed in between the other static objects, which can't have their
                  offset from the start of static storage changed at run-time -- the
                  very same situation as for object members.

                  Static allocation -> fixed length, unless it's globally the last static
                  object (which there's no way to ensure), so the standard simply
                  requires fixed length.

                  With allocation on the stack there is technically the possibility of
                  dynamic length, because the stack is a dynamic data structure, and that
                  is supported in C99, and offered as a C++ language extension by some
                  compilers. Also, some compilers offer a run-time library function that
                  lets you allocate dynamically on the stack (of course in last-in-first-out
                  fashion). But standard C++ doesn't support either feature, at least not yet.

                  I'm not sure why that was decided, but original C was a comparably simple
                  language, and did not offer features that did not have very direct benefit.

                  Finally, with allocation on the heap there's no allocation-related problem,
                  because heap allocation at bottom already is a function that must be ready to
                  deliver any number of bytes of contigous storage, with n specified at runtime.
                  The only problem is that the number of array elements must be dynamically
                  recorded somewhere, in order to support destructor calls for the elements.
                  In C++ that's supported because dynamic heap allocation of arrays was
                  supported in C (that didn't have the destructor call issue), it would be
                  silly if C++ was more limited than C in that regard, and most important it
                  would have made the language nearly unusable -- it's that critical.

                  Hth.,

                  - Alf

                  --
                  A: Because it messes up the order in which people normally read text.
                  Q: Why is it such a bad thing?
                  A: Top-posting.
                  Q: What is the most annoying thing on usenet and in e-mail?

                  Comment

                  • Andrew Koenig

                    #10
                    Re: Array size as constant expression

                    <pvinodhkumar@g mail.com> wrote in message
                    news:1102754996 .729836.178300@ z14g2000cwz.goo glegroups.com.. .
                    [color=blue]
                    > But why there is no such restriction while allocating in heap?[/color]

                    Because what gets returned is a pointer to the initial element of the array,
                    not the array itself, so the size does not participate in the type.


                    Comment

                    Working...