Array of pointers to different types

W

whiskers

Hello, gurus. I spent an hour looking for the answer to this and
couldn't find one that pleased me (I don't want to use a base class,
which seems to be a solution).

So I have a function that takes (void * array) as a parameter. This
function will be called automatically a couple hundred times, I can't
change the declaration.

I need to pass the following data to the function:

vector<CustomStruct1> list // over 1500 entries
CustomStruct2 sixstructmembers
double p1
double p2

What I want to be able to do, is pass these 4 data structures in via
(void * array) without having to make them global (that's my last
resort)

void * array[4];
array[0] = &list;
array[1] = &sixstructmembers;
array[2] = &p1;
array[3] = &p2;


So now "array" should contain 4 pointers, each to a different data
structure.

My problem is dereferencing the pointers. When I get into the function,
I want to do something like
double * p2 = (double*)array[3] but that doesn't work, I'm guessing
because the compiler doesn't know what [3] means in this sense (no size
for void*). How do I work around this?

After thinking too much about it, I'm getting lost in all the
indirection. Is it a valid idea to use int * array[4];
array[0]=(int)&list; and then cast the array in the function as int *
newarray = (int*)array? Somehow that seems a little hackish.
 
I

Ivan Novick

whiskers said:
Hello, gurus. I spent an hour looking for the answer to this and
couldn't find one that pleased me (I don't want to use a base class,
which seems to be a solution).

So I have a function that takes (void * array) as a parameter. This
function will be called automatically a couple hundred times, I can't
change the declaration.

I need to pass the following data to the function:

vector<CustomStruct1> list // over 1500 entries
CustomStruct2 sixstructmembers
double p1
double p2

What I want to be able to do, is pass these 4 data structures in via
(void * array) without having to make them global (that's my last
resort)

void * array[4];
array[0] = &list;
array[1] = &sixstructmembers;
array[2] = &p1;
array[3] = &p2;


So now "array" should contain 4 pointers, each to a different data
structure.

My problem is dereferencing the pointers. When I get into the function,
I want to do something like
double * p2 = (double*)array[3] but that doesn't work, I'm guessing
because the compiler doesn't know what [3] means in this sense (no size
for void*). How do I work around this?

After thinking too much about it, I'm getting lost in all the
indirection. Is it a valid idea to use int * array[4];
array[0]=(int)&list; and then cast the array in the function as int *
newarray = (int*)array? Somehow that seems a little hackish.

Hey Whiskers, how about try something like this, let me know if this
helps
#include <iostream>

void func(void* data)
{
void** array = static_cast<void**>(data);
std::cout << *static_cast<double*>(array[0]) << std::endl;
std::cout << *static_cast<double*>(array[1]) << std::endl;
std::cout << *static_cast<int*>(array[2]) << std::endl;
std::cout << *static_cast<int*>(array[3]) << std::endl;
}

int main(int argc, char** argv)
{
double d1 = 2.0, d2 = 3.0;
int i1 = 4, i2 = 5;

void* array[4];
array[0] = &d1;
array[1] = &d2;
array[2] = &i1;
array[3] = &i2;

func(array);

return 0;
}
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

whiskers said:
So I have a function that takes (void * array) as a parameter. This
function will be called automatically a couple hundred times, I can't
change the declaration.

I need to pass the following data to the function:

vector<CustomStruct1> list // over 1500 entries
CustomStruct2 sixstructmembers
double p1
double p2

What I want to be able to do, is pass these 4 data structures in via
(void * array) without having to make them global (that's my last
resort)

The cleaner way will be something like the following:

struct MyThing
{
vector <CustomStruct1> * list;
CustomStruct2 * sixstructmembers;
double p1;
double p2;
};

void cantchangethisdeclaration (void * array)
{
MyThing * thing (static_cast <MyThing *> (array) );
// Do whatever you want with * thing
}

MyThing my;
my.list= & list;
my.sixstructmembers= & sixstructmembers;
my.p1= p1;
my.p2= p2;
cantchangethisdeclaration (& my);

Sure "array" is not used like an array, but who cares? (And if you care,
think of it as an array of one element).

By the way, the name "list" for a vector is confusing.
 
I

Ivan Novick

Julián Albo said:
The cleaner way will be something like the following:

struct MyThing
{
vector <CustomStruct1> * list;
CustomStruct2 * sixstructmembers;
double p1;
double p2;
};

void cantchangethisdeclaration (void * array)
{
MyThing * thing (static_cast <MyThing *> (array) );
// Do whatever you want with * thing
}

MyThing my;
my.list= & list;
my.sixstructmembers= & sixstructmembers;
my.p1= p1;
my.p2= p2;
cantchangethisdeclaration (& my);
Yes this is a better solution if you are allowed to use it in your
requirements, but the one i posted should work too.
 
S

Salt_Peter

whiskers said:
Hello, gurus. I spent an hour looking for the answer to this and
couldn't find one that pleased me (I don't want to use a base class,
which seems to be a solution).

So I have a function that takes (void * array) as a parameter. This
function will be called automatically a couple hundred times, I can't
change the declaration.

Then you essentially have code in C, not C++.
You should ask your question in a C newsgroup, sorry.
Its bad code, bad design. Not to mention downright dangerous and
time-consuming.

By the way, passing an array of any type, including your own types, is
simple, automatic and easy. It can also be safe - using a reference as
shown below. It also involves a very tiny amount of code.

#include <iostream>
#include <ostream>

template< typename T, const size_t Size >
void array_by_ref(T (&array)[Size])
{
for(size_t i = 0; i < Size; ++i)
{
std::cout << array << std::endl;
}
}

int main()
{
double darray[10] = {0};
array_by_ref(darray);
}

/*
0
0
0
0
0
0
0
0
0
*/

And you can change double in that array to anything copyable.
I need to pass the following data to the function:

vector<CustomStruct1> list // over 1500 entries
CustomStruct2 sixstructmembers
double p1
double p2

What I want to be able to do, is pass these 4 data structures in via
(void * array) without having to make them global (that's my last
resort)

void * array[4];
array[0] = &list;
array[1] = &sixstructmembers;
array[2] = &p1;
array[3] = &p2;


So now "array" should contain 4 pointers, each to a different data
structure.

My problem is dereferencing the pointers. When I get into the function,
I want to do something like
double * p2 = (double*)array[3] but that doesn't work, I'm guessing
because the compiler doesn't know what [3] means in this sense (no size
for void*). How do I work around this?

After thinking too much about it, I'm getting lost in all the
indirection. Is it a valid idea to use int * array[4];
array[0]=(int)&list; and then cast the array in the function as int *
newarray = (int*)array? Somehow that seems a little hackish.
 
W

whiskers

Julián Albo said:
struct MyThing
{
vector <CustomStruct1> * list;
CustomStruct2 * sixstructmembers;
double p1;
double p2;
};

void cantchangethisdeclaration (void * array)
{
MyThing * thing (static_cast <MyThing *> (array) );
// Do whatever you want with * thing
}


The idea of a wrapper struct somehow isn't appealing to me, because it
seems like another thing to clutter the code. In general, however, how
is it looked upon? Is it a preferable thing in cases like this?


Ivan said:
Yes this is a better solution if you are allowed to use it in your
requirements, but the one i posted should work too.
---


I works perfectly, just what I wanted! The idea of an array of pointers
to objects seemed totally logical, but I just wasn't going deep enough
with the dereferencing.
Thanks, guys!
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top