Declaring an array's dimension at run time: too good to be true...

G

Google Poster

Coming from the C world, I was trilled when I realized that it is
possible to create an array with dimensions, at run time:

string MyTable[ver_dim][hor_dim];

But alas, it seems it was too good to be true. It is clear that C
requires the dimensions to be known at compile time for a reason.

I cannot pass the dynamically created array to functions. I have tried
to code without classes and then with classes:

ExpressionTable::ExpressionTable(int nofRows, int nofCols)
{// initializer

string table[nofRows][nofCols];

}

The above array is created, but I cannot access it. The unknown (at
compile time) dimensions are a big block.

Your expert suggestions are most appreciated...

-Ramon
 
A

Anand Hariharan

Coming from the C world, I was trilled when I realized that it is
possible to create an array with dimensions, at run time:

string MyTable[ver_dim][hor_dim];

But alas, it seems it was too good to be true. It is clear that C
requires the dimensions to be known at compile time for a reason.

I cannot pass the dynamically created array to functions. I have tried
to code without classes and then with classes:

ExpressionTable::ExpressionTable(int nofRows, int nofCols)
{// initializer

    string table[nofRows][nofCols];

}

The above array is created, but I cannot access it. The unknown (at
compile time) dimensions are a big block.

Your expert suggestions are most appreciated...

-Ramon

Since you are using C++ (and std::string), you should prefer vectors
over arrays.

Suggest you spend some time reading the FAQ:
http://www.parashift.com/c++-faq-lite/containers.html#faq-34.1
http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.18
http://www.parashift.com/c++-faq-lite/

- Anand
 
G

Google Poster

Coming from the C world, I was trilled when I realized that it is
possible to create an array with dimensions, at run time:
string MyTable[ver_dim][hor_dim];
But alas, it seems it was too good to be true. It is clear that C
requires the dimensions to be known at compile time for a reason.
I cannot pass the dynamically created array to functions. I have tried
to code without classes and then with classes:
ExpressionTable::ExpressionTable(int nofRows, int nofCols)
{// initializer
    string table[nofRows][nofCols];

The above array is created, but I cannot access it. The unknown (at
compile time) dimensions are a big block.
Your expert suggestions are most appreciated...

Since you are using C++ (and std::string), you should prefer vectors
over arrays.

Suggest you spend some time reading the FAQ:
 http://www.parashift.com/c++-faq-lite/containers.html#faq-34.1
 http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.18
 http://www.parashift.com/c++-faq-lite/

- Anand

Thanks Anand!

I use list and vector on a daily basis, but I assumed they have only 1-
dimension?

-Ramon
 
D

Dombo

Op 20-Dec-10 19:38, Google Poster schreef:
On Dec 20, 12:00 pm, Anand Hariharan

Thanks Anand!

I use list and vector on a daily basis, but I assumed they have only 1-
dimension?

You can put a vector in a vector. Alternatively you might consider using
boost::multi_array from the Boost library.
 
V

Virchanza

ExpressionTable::ExpressionTable(int nofRows, int nofCols)
{// initializer

    string table[nofRows][nofCols];

}

Are you against using dynamic memory allocation?

Here's one possible solution that used dynamic memory allocation:

#include <cstddef>

template<class T>
class Array_Helper_2D {

public:

typedef std::size_t size_t;

protected:

size_t const second_dimension;

T *const p;

public:

Array_Helper_2D(size_t const arg_2nd_dimension, T *const arg_p)
: second_dimension(arg_2nd_dimension), p(arg_p) {}

T &GetElement(size_t const i, size_t const j)
{
return p[i*second_dimension + j];
}
};


#include <string>
using std::string;

class ExpressionTable {
public:
ExpressionTable(int nofRows, int nofCols);
};

ExpressionTable::ExpressionTable(int nofRows, int nofCols)
{
string *const p = new string[nofRows * nofCols];

Array_Helper_2D<string> arr(nofCols,p);

arr.GetElement(0,0) = "Hello";
arr.GetElement(4,5) = "Goodbye";

delete [] p;
}

int main()
{
ExpressionTable exptab(5,6);
}
 
J

Juha Nieminen

In comp.lang.c++ Anand Hariharan said:
Since you are using C++ (and std::string), you should prefer vectors
over arrays.

That's certainly not true in all cases. If a static array (in other
words its size doesn't need to change and its size is determined at
compile time) is sufficient for some task, there are at least two
situations where using a raw array is more efficient than using
std::vector: When the array is used locally inside a function, and when
the array is the member of a class. There's little advantage in using
std::vector in these cases. std::vector will only be more inefficient.

In other words, the two cases are:

void foo()
{
SomeType anArray[someSize];
...
}

and:

class SomeClass
{
SomeType anArray[someSize];
...
};
 
J

James Kanze

That's certainly not true in all cases. If a static array (in other
words its size doesn't need to change and its size is determined at
compile time) is sufficient for some task, there are at least two
situations where using a raw array is more efficient than using
std::vector:

There's also a case where using a raw array is the only correct
solution: when it's a variable with static lifetime, can be
initialized with constants, and might be used from static
constructors. This leads me to use arrays of char const*, for
example, or of struct's with only basic types in them.
 
A

Anand Hariharan

  That's certainly not true in all cases.

Guess you missed my saying "prefer" above?

If a static array (in other
words its size doesn't need to change and its size is determined at
compile time) is sufficient for some task, there are at least two
situations where using a raw array is more efficient than using
std::vector: When the array is used locally inside a function, and when
the array is the member of a class. There's little advantage in using
std::vector in these cases. std::vector will only be more inefficient.

Try (re)reading the original post. The OP -
- does not know the dimensions at compile time
- needs to "pass the dynamically created array to functions."


Notwithstanding that arrays have automatic storage and vectors are
allocated in free store, choosing one of vector or array over another
for reasons entirely based on efficiency, is not what I'd advocate. I
remember Andrew Koening's post from many years ago that showed a
vector of vectors outperformed the equivalent array of arrays.

One advantage that arrays have over vectors is that their size cannot
be changed. Consider -

int a[3] = {0};
vector<int> v(3);

func1(a); // a's elements could have changed, but its number of
elements couldn't have.

void func2(vector<int> &);
func2(v); // v could have changed. Period.
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top