# void * help

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

## Recommended Posts

sorry to bug you guys again but i am stuck again. i have a vector<void *>data now that vector holds the following bool,int,float,D3DXCOLOR,D3DXVECTOR3,string. now i am having problems with the data not being the same. example
void *m_data;
m_data = new bool;
fscanf(afile,"%s",buffer);
bool b;
if(strcmp(buffer,"true")==0)
{
b = true;
}
else
{
b = false;
}
memcpy(m_data,&b,sizeof(bool));
data.push_back(m_data);
bool a = (bool)&m_data;
if(a==b)
MessageBox(0,0,0,0);
delete m_data;


it never does the messagebox because the data is not the same. why?

##### Share on other sites
bool a = (bool*)m_data;

is the same as

bool a = (m_data != 0);

In other words, you're casting void* to bool* and then setting a bool to it. Unless that is a null pointer, your value is going to be true. And true is usually 1, which is not what m_data was. What you really want is

bool a = *reinterpret_cast<bool*>(m_data);

Also, don't do this. Use polymorphism to achieve whatever you're trying to do. Void* is almost always evil.

Good luck.

##### Share on other sites
what is a better way to do this then?

cause i still cant get this to work.

show an example of a way to do it.

reinterpret_cast didnt work.

##### Share on other sites
What are you trying to do?

##### Share on other sites
store different type of values in a vector

like bool,int,float,D3DXVECTOR3,D3DXCOLOR,string

##### Share on other sites
My first thought is "Why are you trying to do that?" because I have never seen an actual need for that.

The biggest problem was sortof mentioned, but it looks like different source then quoted. In your source you have:
bool a = (bool)&m_data;

but:
m_data is a void*.
&m_data is a void**
(bool)&m_data is a boolean which will always be true because &m_data points to a stack variable (which points to a heap variable which may be true or false).
a is then always set to true.

What you're supposed to do is:
bool a = *(bool*)m_data;

Or use reinterprit_cast<bool*> instead of (bool*).

If it's not working, you didn't copy it right. It's "dereferance m_data as if it were a pointer to a boolean", *(bool*)m_data. Notice the two asterisks, and NO ampersands.

##### Share on other sites
Why do you want to do that? I mean, how are you going to distinguish what is what later on and what are you trying to achieve? Maybe we can find a cleaner solution or there is a handy boost lib you can use.

edit: Beaten by erzengeldeslichtes [eviltonque]

##### Share on other sites
Quote:
 Original post by 31337noobstore different type of values in a vectorlike bool,int,float,D3DXVECTOR3,D3DXCOLOR,string

I know. I meant why are you trying to do that.

##### Share on other sites
Changing a bit of the program so that it isn't too Windowsy (I'm under linux)... If you really wanted void* elements, then this would be better...

I admit I'm more a C programmer than a C++ one, so you C++ purist, don't look, you'll crisp and probably have a hard attack...

That said, I agree with everybody in saying that void* elements should be used with great care, you better know what you are doing...

#include <stdlib.h>#include <iostream>using namespace std;int main(int argc, char **argv){void *m_data;m_data = new bool;bool b;//Just to easily test  true or false without recompilingb = argc>1;memcpy(m_data,&b,sizeof(bool));//This becomes slightly ugly but once you think about it, it should be logicalbool a = *((bool *) m_data);//This should always (and does) do the if and not the elseif(a==b)       cout << "A==B\n";else      cout << "A!=B\n";//Clear up the new, again, C++ purists will hate me for this onedelete ((bool*) m_data);return 1;}

It sometimes happens (I suppose) that you may need a vector that can hold different types and generally a void* seems the easy solution. But I think you are stretching it a bit... What kind of element in your program could be a bool,int,float,D3DXCOLOR,D3DXVECTOR3,string?

If you rethink your problem, you'll probably get a better solution that will not need a void* vector, seeing as you are having problems with it...

To end this post will be a:

I'm sorry I have posted an example with void*
I'm sorry I actually like void*
I promise not to say it again, lol

Jc

##### Share on other sites
i am tring to make a script system
that holds the data.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 16
• 15
• 29
• 12
• 14
• ### Forum Statistics

• Total Topics
634805
• Total Posts
3019369
×