# array of void pointers (C++)

## Recommended Posts

michael879    100
ok I just spent an hour search google and havnt found anything on this. I have an array of void pointers as a field in a class. class A { private: void* data[5]; } first, is there any way to make it so that that 5 can be any number that is given to the constructor? second, how do you allocate memory for this thing?? I tried data = new void* [5]; which didnt work. I tried putting parenthesis around the void* and got an error...

##### Share on other sites
Bezben    202
class A{A(){data = 0;}A( int size ){data = new void*[ size ];}~A(){delete [] data;}private:void** data;};

##### Share on other sites
d_emmanuel    271
Just to add to the above post you'll want to declare data as
"void** data;" and (more than likely) declare your constructors as public.

##### Share on other sites
michael879    100
ah got it thanks that makes sense.

##### Share on other sites
Conner McCloud    1135
It is extremely, extremely rare that you need to use a void* in a C++ program. I suggest you elaborate on what you want to do, and then we can tell you the right way to do it.

However, you've clearly got issues with dynamic memory arrays. In general, this isn't so bad, so I'll help out there. Just, I'll substitute int for void*.

To create a variable length array of ints, you first declare a pointer to an int.
int* array;

Then, you actually create the array via a call to new[]
array = new int[5]; //5 can be anything, including a variable

Now, you can use array however you want.
cout << array[3] << endl;

When you are done, you get rid of the array. You created it with acall to new[], so you erase it with a call to delete[]
delete[] array;

Now, lets say you want to create an array of pointers to int. Straight forward: your type is simply 'int*' rather than 'int'.
int** array;array = new int*[5];cout << array[3] << endl; //this prints the address in array[3]     //to print the value you use *(array[3]) just like you would any other pointerdelete[] array;

Now, having covered all that, the right way to do this is to use std::vector, but that's a lesson for another day.

CM

##### Share on other sites
Array sizes must be known at compile time, which is the previous two posters show you how to do it with a pointer to an array of pointers.

However, it would be easier and safer if you used something like std::vector or boost::array. Also, you should consider boost::any instead of void pointers as it is much less hassle and is much safer.

##### Share on other sites
MaulingMonkey    1728
Quote:
 Original post by michael879ok I just spent an hour search google and havnt found anything on this. I have an array of void pointers as a field in a class.class A{ private: void* data[5];}first, is there any way to make it so that that 5 can be any number that is given to the constructor? second, how do you allocate memory for this thing?? I tried data = new void* [5];which didnt work. I tried putting parenthesis around the void* and got an error...

#include <vector>class A{private:    std::vector< void * > data;public:    void foo() {        data.resize( 5 );        //data[0] = ...        //data[4] = ...        //data[5] = ERROR - 5 elements = 0, 1, 2, 3, 4.        data.resize( 6 );        //data[5] = OK now    }};

You (probably) don't want to use (void *).

##### Share on other sites
Bezben    202
Quote:
 Original post by d_emmanuel and (more than likely) declare your constructors as public.

Bugger, theres always something I miss out, and theres always some smug bastard who points it out :P :)

##### Share on other sites
Conner McCloud    1135
Quote:
 Original post by BezbenBugger, theres always something I miss out, and theres always some smug bastard who points it out :P :)

This is why I try and use structs for code examples [grin]

CM

##### Share on other sites
MaulingMonkey    1728
Quote:
Original post by Conner McCloud
Quote:
 Original post by BezbenBugger, theres always something I miss out, and theres always some smug bastard who points it out :P :)

This is why I try and use structs for code examples [grin]

It's things like that which make me want a Firefox IDE plugin ;-).