Dynamic sized array?

Discussion in 'C++' started by Daniel T., Oct 17, 2006.

  1. Daniel T.

    Daniel T. Guest

    I recently came across something like the following:

    int main() {
    unsigned i;
    cin >> i;
    int arr;
    // use arr
    }

    Is that valid in C++? I thought it was only valid in C99...

    --
    There are two things that simply cannot be doubted, logic and perception.
    Doubt those, and you no longer have anyone to discuss your doubts with,
    nor any ability to discuss them.
     
    Daniel T., Oct 17, 2006
    #1
    1. Advertisements

  2. Daniel T.

    Kai-Uwe Bux Guest

    Daniel T. wrote:

    > I recently came across something like the following:
    >
    > int main() {
    > unsigned i;
    > cin >> i;
    > int arr;
    > // use arr
    > }
    >
    > Is that valid in C++?


    No. However, some compiler accept it as an extension. However, a diagnostic
    is required: if the compiler accepts it, you should still get a warning.


    > I thought it was only valid in C99...


    I don't know about C99.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Oct 17, 2006
    #2
    1. Advertisements

  3. Daniel T.

    Martin Steen Guest

    Daniel T. wrote:
    > I recently came across something like the following:
    >
    > int main() {
    > unsigned i;
    > cin >> i;
    > int arr;
    > // use arr
    > }
    >
    > Is that valid in C++? I thought it was only valid in C99...



    No, it's not valid. Although some compilers (e.g. g++) may
    compile it.

    If you want dynamic arrays, better use vector-templates from the STL:

    #include <vector>

    int main()
    {
    unsigned i;
    cin >> i;
    std::vector<int> arr(i);
    // use arr
    return 0;
    }

    -Martin
     
    Martin Steen, Oct 17, 2006
    #3
  4. Daniel T.

    Sumit Rajan Guest

    Daniel T. wrote:
    > I recently came across something like the following:
    >
    > int main() {
    > unsigned i;
    > cin >> i;
    > int arr;
    > // use arr
    > }
    >
    > Is that valid in C++?


    No/Not yet.

    >I thought it was only valid in C99...


    Yes, it is valid in C99.

    Regards,
    Sumit.
     
    Sumit Rajan, Oct 17, 2006
    #4
  5. Daniel T.

    Sumit Rajan Guest

    Sumit Rajan wrote:

    > Yes, it is valid in C99.


    Of course, "cin" wouldn't be around in such a case.

    Sumit.
     
    Sumit Rajan, Oct 17, 2006
    #5
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. thechaosengine

    Oddly sized sized password textbox

    thechaosengine, Sep 15, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    715
    David Hearn
    Sep 15, 2005
  2. Replies:
    1
    Views:
    420
    Martin Honnen
    May 31, 2005
  3. Allen

    run-time sized array

    Allen, Aug 23, 2003, in forum: C++
    Replies:
    7
    Views:
    687
    J. Campbell
    Aug 23, 2003
  4. Jonathan Halterman

    Dynamic sized structs

    Jonathan Halterman, Oct 14, 2003, in forum: C++
    Replies:
    2
    Views:
    409
    Jonathan Halterman
    Oct 15, 2003
  5. Leo Havmøller

    Sized array w/initializer

    Leo Havmøller, Apr 21, 2010, in forum: C Programming
    Replies:
    4
    Views:
    510
    Gil Johnson
    Apr 22, 2010
  6. MartinBroadhurst

    List as a dynamic array of increasing-sized arrays

    MartinBroadhurst, Oct 21, 2010, in forum: C Programming
    Replies:
    13
    Views:
    1,259
    MartinBroadhurst
    Nov 3, 2010
  7. joe
    Replies:
    1
    Views:
    466
    Goran
    Aug 31, 2011
  8. Eric Peterson

    make dynamic sized iframe work? or alt ideas?

    Eric Peterson, Jan 24, 2006, in forum: Perl Misc
    Replies:
    0
    Views:
    158
    Eric Peterson
    Jan 24, 2006
Loading...