Array size determined at run time

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

    Array size determined at run time

    Hi all,
    I'm trying to use an array in my C++ programme, but the size of the
    array is not known until run time; I've tried using linked lists, which
    work quite well, but traversing the list is quite slow. Is there
    another way to declare an array during run-time? I thought about
    something like:

    iVal = some_function( blob );

    MyClass *my_array;

    my_array = new MyClass[ iVal ];

    - but this doesn't want to work on my compiler.

    TIA

    Paul

  • Alf P. Steinbach

    #2
    Re: Array size determined at run time

    * paul@paullee.co m:
    Hi all,
    I'm trying to use an array in my C++ programme, but the size of the
    array is not known until run time; I've tried using linked lists, which
    work quite well, but traversing the list is quite slow. Is there
    another way to declare an array during run-time? I thought about
    something like:
    >
    iVal = some_function( blob );
    >
    MyClass *my_array;
    >
    my_array = new MyClass[ iVal ];
    >
    - but this doesn't want to work on my compiler.
    std::vector<MyC lassmyArray( iVal );


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

    • Axter

      #3
      Re: Array size determined at run time

      paul@paullee.co m wrote:
      Hi all,
      I'm trying to use an array in my C++ programme, but the size of the
      array is not known until run time; I've tried using linked lists, which
      work quite well, but traversing the list is quite slow. Is there
      another way to declare an array during run-time? I thought about
      something like:
      >
      iVal = some_function( blob );
      >
      MyClass *my_array;
      >
      my_array = new MyClass[ iVal ];
      >
      - but this doesn't want to work on my compiler.
      IAW C++ standards, the std::vector should be your default container.
      For most container requirements, std::vector is a better choice.
      If you're not deleting or adding from/to the center of your container,
      you don't need std::list, and should be using either std::vector or
      std::deque.

      As "Alf P. Steinbach" correctly stated, use std::vector.
      std::vector<MyC lassmyArray( iVal );

      However, if your originally posted code doesn't compile, then I suspect
      the vector code will not compile.
      Your code will not compile if your class doesn't have a default
      constructor.
      If that's not the problem, please post specifics as to why it doesn't
      compile, by posting your compile error.
      -------------------------------------------------------------------
      David Maisonave
      Policy based smart pointers (http://axter.com/smartptr)
      C++ Expert Exchange Member:
      Get answers to your C++ questions from our top industry experts by using Experts Exchange's platform to find the best C++ help & solutions. Click to learn more!

      ----------------------------------------------------------------------------------------

      Comment

      • Frederick Gotham

        #4
        Re: Array size determined at run time

        Paul posted:

        iVal = some_function( blob );
        >
        MyClass *my_array;
        >
        my_array = new MyClass[ iVal ];
        >
        - but this doesn't want to work on my compiler.

        Here's a code snippet for you to try to compile with your compiler. If
        the code gives any errors, then post them here (along with line numbers)
        and we'll try to help. (However there's nothing wrong with the code and
        it should compile just fine.)

        #include <cstddef>

        class Arb {
        public:

        int i;

        };

        std::size_t GetLength()
        {
        return 57;
        }

        int main()
        {
        Arb *p = new Arb[ GetLength() ];

        delete [] p;
        }


        --

        Frederick Gotham

        Comment

        • paul@paullee.com

          #5
          Re: Array size determined at run time

          Hi,
          Its a bit embarrasing as I'm on vacation at present! The code was the
          last thing I was tackling on Friday and its been niggling me ever
          since. All I can remember is that it was the line with the "new"
          operator in it, and it said something about an error due to a non-const
          value, which I think is due to the fact that the array number is not
          defined precisely at run time.

          TIA

          Paul

          Comment

          • Howard

            #6
            Re: Array size determined at run time


            <paul@paullee.c omwrote in message
            news:1151870540 .578492.157400@ h44g2000cwa.goo glegroups.com.. .
            Hi,
            Its a bit embarrasing as I'm on vacation at present! The code was the
            last thing I was tackling on Friday and its been niggling me ever
            since. All I can remember is that it was the line with the "new"
            operator in it, and it said something about an error due to a non-const
            value, which I think is due to the fact that the array number is not
            defined precisely at run time.
            >
            Please quote what you're referring to, so we don't have to go looking at
            other posts to see what you're talking about.

            That said, if you got en error saying something about a non-constant value
            being used for an array, then chances are you weren't using new to
            dynamically allocate the array. If you're declaring an array statically (as
            in "char buffer[100];") then you need to use a compile-time constant for the
            array size. But you're free to use any sort of integer expression to
            allocate an array dynamically (with "new"). So, let us know when you're
            back at the computer, if you've still got an error, and give us the _real_
            line(s) of code, ok?

            -Howard


            Comment

            • mlimber

              #7
              Re: Array size determined at run time

              Axter wrote:
              IAW C++ standards, the std::vector should be your default container.
              [snip]

              For more on why this is so, see this FAQ:



              Cheers! --M

              Comment

              Working...