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. Advertising

  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. Advertising

  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. Advertising

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:
    569
    David Hearn
    Sep 15, 2005
  2. Replies:
    1
    Views:
    354
    Martin Honnen
    May 31, 2005
  3. Jonathan Halterman

    Dynamic sized structs

    Jonathan Halterman, Oct 14, 2003, in forum: C++
    Replies:
    2
    Views:
    319
    Jonathan Halterman
    Oct 15, 2003
  4. MartinBroadhurst

    List as a dynamic array of increasing-sized arrays

    MartinBroadhurst, Oct 21, 2010, in forum: C Programming
    Replies:
    13
    Views:
    1,050
    MartinBroadhurst
    Nov 3, 2010
  5. joe
    Replies:
    1
    Views:
    350
    Goran
    Aug 31, 2011
Loading...

Share This Page