By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,407 Members | 920 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,407 IT Pros & Developers. It's quick & easy.

Array size determined at run time

P: n/a
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

Jul 2 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
* pa**@paullee.com:
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<MyClassmyArray( 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?
Jul 2 '06 #2

P: n/a
pa**@paullee.com 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<MyClassmyArray( 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
----------------------------------------------------------------------------------------

Jul 2 '06 #3

P: n/a
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
Jul 2 '06 #4

P: n/a
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

Jul 2 '06 #5

P: n/a

<pa**@paullee.comwrote in message
news:11**********************@h44g2000cwa.googlegr oups.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
Jul 3 '06 #6

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

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

http://www.parashift.com/c++-faq-lit....html#faq-34.1

Cheers! --M

Jul 3 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.