void * help

Started by
13 comments, last by snk_kid 18 years, 4 months ago
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?
Advertisement
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.
- Jason Citron- Programmer, Stormfront Studios- www.stormfront.com
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.
What are you trying to do?
store different type of values in a vector

like bool,int,float,D3DXVECTOR3,D3DXCOLOR,string
My first thought is "Why are you trying to do that?" because I have never seen an actual need for that.

My second thought is "Please, please, please use boost::any"

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.
----Erzengel des Lichtes光の大天使Archangel of LightEverything has a use. You must know that use, and when to properly use the effects.♀≈♂?
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]
Quote:Original post by 31337noob
store different type of values in a vector

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


I know. I meant why are you trying to do that.
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
i am tring to make a script system
that holds the data.

This topic is closed to new replies.

Advertisement