Array size determined at run time

P

paul

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
 
A

Alf P. Steinbach

* (e-mail address removed):
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<MyClass> myArray( iVal );
 
A

Axter

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<MyClass> myArray( 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:
http://www.experts-exchange.com/Cplusplus
----------------------------------------------------------------------------------------
 
F

Frederick Gotham

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;
}
 
P

paul

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
 
H

Howard

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top