static array

L

lazy

Hi,
Im trying to define a hashtable thats static(meaning the table is
initialised at compile time itself)
It takes a char and has 2 other ints as values
For eg:
arr[1]={1,2}
arr[20]={0xaabbff,0xabcdef}

so its a 2D array with n rows and 2 columns. I know n, at compile time
itself. but I cant do int arr[n][2] bcos the first index(or rownumber)
is char and not int.Typecasting to a higher type should be ok, but
another problem is the rows are not sequential, for eg in above
example, there are 2 rows, but I would like to use arr[20].

I want to declare a static array for this. Not sure how to do it.
 
T

Triple-DES

Hi,
Im trying to define a hashtable thats static(meaning the table is
initialised at compile time itself)
It takes a char and has 2 other ints as values
For eg:
arr[1]={1,2}
arr[20]={0xaabbff,0xabcdef}

so its a 2D array with n rows and 2 columns. I know n, at compile time
itself. but I cant do int arr[n][2] bcos the first index(or rownumber)
is char and not int.Typecasting to a higher type should be ok, but
another problem is the rows are not sequential, for eg in above
example, there are 2 rows, but I would like to use arr[20].

I want to declare a static array for this. Not sure how to do it.

This could be solved using std::pair.
#include <utility>

typedef std::pair<int,int> IntPair; // for convenience
static IntPair arr[255];

// to add elements:
arr[0] = IntPair(1,2);

Hope this helps
DP
 
L

lazy

Hi,
Im trying to define a hashtable thats static(meaning the table is
initialised at compile time itself)
It takes a char and has 2 other ints as values
For eg:
arr[1]={1,2}
arr[20]={0xaabbff,0xabcdef}
so its a 2D array with n rows and 2 columns. I know n, at compile time
itself. but I cant do int arr[n][2] bcos the first index(or rownumber)
is char and not int.Typecasting to a higher type should be ok, but
another problem is the rows are not sequential, for eg in above
example, there are 2 rows, but I would like to use arr[20].
I want to declare a static array for this. Not sure how to do it.

This could be solved using std::pair.
#include <utility>

typedef std::pair<int,int> IntPair; // for convenience
static IntPair arr[255];

// to add elements:
arr[0] = IntPair(1,2);

Hope this helps
DP

Hi DP,
Seems to me that this gets initialsed only at runtime?
For eg, I would like to achieve something like
arr[0]=(1,2)
arr[20]=(2,30)

Somewhere down the line. I would like to say declare something like
char test_arr[arr[20][1]]; (get the value at index 20, and then use
second value of the pair)
I mean char test_arr[30] which will be allocated at compile time
Thanks.
 
T

Triple-DES

Hi,
Im trying to define a hashtable thats static(meaning the table is
initialised at compile time itself)
It takes a char and has 2 other ints as values
For eg:
arr[1]={1,2}
arr[20]={0xaabbff,0xabcdef}
so its a 2D array with n rows and 2 columns. I know n, at compile time
itself. but I cant do int arr[n][2] bcos the first index(or rownumber)
is char and not int.Typecasting to a higher type should be ok, but
another problem is the rows are not sequential, for eg in above
example, there are 2 rows, but I would like to use arr[20].
I want to declare a static array for this. Not sure how to do it.
This could be solved using std::pair.
#include <utility>
typedef std::pair<int,int> IntPair; // for convenience
static IntPair arr[255];
// to add elements:
arr[0] = IntPair(1,2);
Hope this helps
DP

Hi DP,
Seems to me that this gets initialsed only at runtime?
For eg, I would like to achieve something like
arr[0]=(1,2)
arr[20]=(2,30)

Somewhere down the line. I would like to say declare something like
char test_arr[arr[20][1]]; (get the value at index 20, and then use
second value of the pair)
I mean char test_arr[30] which will be allocated at compile time
Thanks.

Ok, I understand now what you are trying to do. To solve this you need
a something like a compile-time constant lookup table. This can
unfortunately not be solved just by using arrays, because the elements
of an array (even if declared const) are not treated as compile-time
constants.

There are solutions, but are you sure it is necessary for all elements
to be compile-time constants?
 
L

lazy

Hi,
Im trying to define a hashtable thats static(meaning the table is
initialised at compile time itself)
It takes a char and has 2 other ints as values
For eg:
arr[1]={1,2}
arr[20]={0xaabbff,0xabcdef}
so its a 2D array with n rows and 2 columns. I know n, at compile time
itself. but I cant do int arr[n][2] bcos the first index(or rownumber)
is char and not int.Typecasting to a higher type should be ok, but
another problem is the rows are not sequential, for eg in above
example, there are 2 rows, but I would like to use arr[20].
I want to declare a static array for this. Not sure how to do it.
This could be solved using std::pair.
#include <utility>
typedef std::pair<int,int> IntPair; // for convenience
static IntPair arr[255];
// to add elements:
arr[0] = IntPair(1,2);
Hope this helps
DP
Hi DP,
Seems to me that this gets initialsed only at runtime?
For eg, I would like to achieve something like
arr[0]=(1,2)
arr[20]=(2,30)
Somewhere down the line. I would like to say declare something like
char test_arr[arr[20][1]]; (get the value at index 20, and then use
second value of the pair)
I mean char test_arr[30] which will be allocated at compile time
Thanks.

Ok, I understand now what you are trying to do. To solve this you need
a something like a compile-time constant lookup table. This can
Yeah, exactly
unfortunately not be solved just by using arrays, because the elements
of an array (even if declared const) are not treated as compile-time
constants.
Now, I have a question. I thought if you declare something like say
static char arr[]={'a','b','c'};
Isnt arr allocated at sompile time(so that the data is in data segment
of the binary rather than memory). Is my understanding incorrect?
There are solutions, but are you sure it is necessary for all elements
to be compile-time constants?
Yeah I know the size of the lookup table and all the values.
 
T

Triple-DES

Now, I have a question. I thought if you declare something like say
static char arr[]={'a','b','c'};
Isnt arr allocated at sompile time(so that the data is in data segment
of the binary rather than memory). Is my understanding incorrect?
I think you are confusing static and const. After all, a static
variable may change during runtime and is therefore not a compile-time
constant. But even if you write

const char arr[] = ...
or
static const char arr[] = ...
the elements of an array can not be used where a compile-time constant
is needed.

In your second example you said you wanted to use a number from the
lookup table as array bounds, like this:

char test_arr[arr[20][1]];

Again, this cannot be made to work, because the array bounds must be a
compile-time integral constant. I'd suggest using a dynamically
allocated array (or even better, vector) instead.

Finally, you could resort to template metaprogramming to achieve your
goal. In my example I use a macro for convenience:

template<unsigned char index>
struct Arr;
#define ADD_ARR_ELEM(index, first, second) \
template<> struct Arr<index> { enum { First = first, Second =
second }; };

// to add elements:
ADD_ARR_ELEM(0,1,2);
ADD_ARR_ELEM(1,22,33);
// ...
ADD_ARR_ELEM(20,25,30);

// Now you can write:
char test_arr[ Arr<20>::Second ]; // sizeof(test_arr) == 30

Hope this helps
DP
 
F

Fei Liu

lazy said:
Ok, I understand now what you are trying to do. To solve this you need
a something like a compile-time constant lookup table. This can
Yeah, exactly
unfortunately not be solved just by using arrays, because the elements
of an array (even if declared const) are not treated as compile-time
constants.
Now, I have a question. I thought if you declare something like say
static char arr[]={'a','b','c'};
Isnt arr allocated at sompile time(so that the data is in data segment
of the binary rather than memory). Is my understanding incorrect?
There are solutions, but are you sure it is necessary for all elements
to be compile-time constants?
Yeah I know the size of the lookup table and all the values.

One technique would be to read this table into your memory at the very
beginning of your application. This will effectively create a static
entity with minimal work.

Fei
 
L

lazy

static char arr[]={'a','b','c'};
Isnt arr allocated at sompile time(so that the data is in data segment
of the binary rather than memory). Is my understanding incorrect?

I think you are confusing static and const. After all, a static
variable may change during runtime and is therefore not a compile-time
constant. But even if you write

const char arr[] = ...
or
static const char arr[] = ...
the elements of an array can not be used where a compile-time constant
is needed.

In your second example you said you wanted to use a number from the
lookup table as array bounds, like this:

char test_arr[arr[20][1]];

Again, this cannot be made to work, because the array bounds must be a
compile-time integral constant. I'd suggest using a dynamically
allocated array (or even better, vector) instead.

Finally, you could resort to template metaprogramming to achieve your
goal. In my example I use a macro for convenience:

template<unsigned char index>
struct Arr;
#define ADD_ARR_ELEM(index, first, second) \
template<> struct Arr<index> { enum { First = first, Second =
second }; };

// to add elements:
ADD_ARR_ELEM(0,1,2);
ADD_ARR_ELEM(1,22,33);
// ...
ADD_ARR_ELEM(20,25,30);

// Now you can write:
char test_arr[ Arr<20>::Second ]; // sizeof(test_arr) == 30

Hope this helps
DP

Thanks for the example of template metaprogramming (I'll look more
into it) DP. It clears my doubt.
 

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
474,123
Messages
2,570,720
Members
47,284
Latest member
LeroyOlver

Latest Threads

Top