473,385 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

Jul 2 '06 #1
6 2267
* 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
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
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
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

<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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: TF | last post by:
I have a fixed array of vectors like: vector<string> a_vStr; Then I grow each vector item and use it later. Everything works fine but I am curious about following potential problem. When we...
14
by: Gianni Mariani | last post by:
Does anyone know if this is supposed to work ? template <unsigned N> int strn( const char str ) { return N; } #include <iostream>
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
6
by: Herrcho | last post by:
in K&R Chapter 6.3 it mentions two methods to calculate NKEYS. and points out the first one which is to terminate the list of initializers with a null pointer, then loop along keytab until the...
6
by: Anjali | last post by:
Hi, I am handling an array with a hexadecimal index for the first time. What actually does the below means. arr = { '@','£','$','@','@','@','@','@','@','@', 10,'@', 13,'@','@','@',...
6
by: sd2004 | last post by:
Hi, I am new to C++. I am reading data from file into array. The problem is , I do not know ahead how many lines the file will be. I am arbitrary assign array size with "int MAX" which is not...
10
by: Christian Christmann | last post by:
Hi, I'm wondering if my small example is not "dangerous": #define SIZE 10 char global; char* globalPtr = global; int main()
3
by: danbraund | last post by:
Hi everyone, I'm a long time C coder, who is coding his final year project in C++ to run under the MIT click routing system. Being fairly new to the OO side of the language, my problem is this: ...
18
by: toton | last post by:
Hi, In C++ when I initialize an array it, also initializes the class that it contains, which calls the default constructor. However, I want to initialize the array only (i.e reserve the space) and...
11
by: C C++ C++ | last post by:
Hi all, got this interview question please respond. How can you quickly find the number of elements stored in a a) static array b) dynamic array ? Rgrds MA
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.