Jump to content
  • Advertisement
Sign in to follow this  

[C++] Void** conversion

This topic is 3123 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to make an universal function that would be able to take any class pointer and load it into a pointer array of the same type. The code I use is the following:
bool AddPointer(void* pointer, void** pointer_array, int &sizeofarray)
{
	for (int i = 0; i < sizeofarray; i++)
		if(!pointer_array)
		{
			pointer_array = pointer;
			return TRUE;
		}

	if(ReallocateArray(pointer_array, sizeofarray))
		return AddPointer(pointer, pointer_array, sizeofarray);

	return FALSE;
}
foo** ppfoo;
int nfoos;
AddPointer((void*)new foo(), ppfoo, nfoos);

At the call of the AddPointer function, Visual Studio 2008 compiler returns this error: error C2664: 'AddPointer' : cannot convert parameter 2 from 'foo **' to 'void **' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast. I do not know what to do. I searched over internet and found nothing. I am constantly inserting pointers into arrays and I really don't want to have to rewrite this function for every class I have. Does anyone have a solution?

Share this post


Link to post
Share on other sites
Advertisement
Ever heard of std::vector. Or for that matter any of the other standard containers?

Furthermore: A void* is a pointer to an unknown type, a void** is NOT.

Share this post


Link to post
Share on other sites
I am 99% sure that, whatever problem you're trying to solve, there are much better ways to go about it than what you're doing here.

Before going any further though, a couple of questions:

1. What do these arrays hold exactly? Are you trying to implement a heterogeneous container of some sort?

2. Why are you returning TRUE and FALSE rather than true and false?

3. Can you post the function definition for ReallocateArray()?

As for your question, the compiler is telling you exactly what the problem is. It's even suggested a solution (use a cast), but that would probably be a bad idea in this case (IMO).

Share this post


Link to post
Share on other sites
One way would be to change AddPointer() to be a template function. However, you probably should be using a std::vector of some type rather than playing around with pointers directly.

Share this post


Link to post
Share on other sites

#include <vector>

std::vector<int> myList; // an array of integers

myList.push_back( 1 ); // Automatically resizes array (if needed)
myList.push_back( 2 );
myList.push_back( 3 );

// Can use like a regular array:
std::cout << myList[0] << myList[1] << myList[2] << '\n';

// Can resize on command:
myList.resize( 5 );
myList.resize( 2 ); // and shrink

// Provides bounds checking (in debug mode):
// std::cout << myList[2]; // Error!

// Can use algorithms: (include <algorithm>, <cstdlib>, and <iterator>)
std::generate_n( std::back_inserter( myList ), 5, rand ); // Pushes 5 random numbers onto the end of myList
std::copy( myList.begin(), myList.end(), std::ostream_iterator<int>( std::cout, "\n" ) ); // Displays the contents of myList, with newlines after each element



Friends don't let friends muddle with raw arrays in C++. See more at cplusplus.com.

Share this post


Link to post
Share on other sites
To jyk:

1.The container is homogeneous, but there are different types of containers

2.Is there a difference?

3.It does not return an error, the problem was clearly in the void to pointer conversion.

Never heard of std::vector, but I just checked out templates, and it seems that they solved my problem pretty well:


template <class C>
bool AddPointer(C pointer, C* pointer_array, int &sizeofarray)
{
for (int i = 0; i < sizeofarray; i++)
if(!pointer_array)
{
pointer_array = pointer;
return TRUE;
}

if(ReallocateArray(pointer_array, sizeofarray))
return AddPointer(pointer, pointer_array, sizeofarray);

return FALSE;
}
template <class C>
bool ReallocateArray(C* &pointer_array, int &sizeofarray)
{
C* newp = (C*) realloc(pointer_array, (sizeofarray + ARRAY_EXTENSION_AMOUNT) * sizeof(C));
if(newp)
{
pointer_array = newp;
sizeofarray += ARRAY_EXTENSION_AMOUNT;
return TRUE;
}
return FALSE;
}



I am still curious though to how the std::vector could help me solve the problem, since apparently it's a pretty big class and if I'll use one for every pointer I have the memory this program will take will be insane. So, if someone is willing to share thoughts, it would be very helpful since, apparently, I hit a part of OOP that I never saw before.

[Edited by - Ratslayer on October 31, 2009 2:08:42 PM]

Share this post


Link to post
Share on other sites
A typical std::vector implementation takes up three machine words plus the data it holds. Right now you're tracking both a pointer and a size along with the data and pointers to that data, so using a std::vector would only take slightly more room than what you're doing and probably give you a speed increase since it won't need to reallocate the buffer every single time you add to it.

Share this post


Link to post
Share on other sites
Oh, I see it then, I didn't realise this vector was working with the arrays. I guess I might switch to that instead of the old raw array system.

Share this post


Link to post
Share on other sites
Quote:
2.Is there a difference?
You can find some info on the subject here (see the sections on C, C99, and C++).

In short, in C++ you should use the keywords true and false rather than constants or macros such as TRUE, FALSE, etc.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!