Union Output ?

Hello All,

i am using following code in vs 2008.

template<typename T>
char * my_fun(T t)
{
union my_enum
{
T tx;
char buf[500];
}e;

memset(e.buf,0,sizeof(e.buf));

e.tx = t;
return e.buf;
}

class myc
{
public:
int a;
long b;
};

void main()
{
myc a;
a.a = 55;
a.b = 5545;

char buf[100] = "";

strcpy(buf,my_fun<myc>(a));

//what will buffer contains ?
}

will buffer contain object a in char buffer format.
(just like serialization ? ).
& will i able to get object back from that buffer ?
i am confuse ?
any idea ?
(sorry i don't know whether it is a proper question ?
or i am trying something wrong )
The first bytes of buf would contain the internal representation of a.
Would contain because your example won't work - in my_fun you're returning a pointer to the buf array which is destroyed when the function exits.
Note that copying objects around like this only works with PODs.
hello ,

i improve my program again
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
template<typename src,typename des>
des my_cast(src s)
{
	union
	{
		des d;
		src ls;
	}e;

	e.ls = s;
	return e.d;
}

char buff[100] = "";

template<typename T>
void  my_ser(T ta)
{
	union
	{
		T src;
		char lbuf[100];

	}e;
	memset(e.lbuf,0,100);

	e.src = ta;

	memcpy(buff,e.lbuf,100);
}

class aclass
{
public:
	int x;
	long y;
};


void main()
{
	aclass ia;
	ia.x = 20;
	ia.y = 35;

	my_ser<aclass>(ia);

	cout<<(my_cast<char *,aclass>(buff)).x; //here i expect value of x again but i did'nt
                                                                                 //get it ?
}


well any idea whats wrong ?
I thinks a will have the value of y only and the output will be some different value ( as value in x will be lost).
Good heavens.

You got the address of buff.

Why? Because the <src> template parameter is a pointer-to-char which happens to be buff (or &buff[0]).
You then typecast that pointer to be an <aclass>. aclass' first four bytes happen to be its <x> member
since aclass has no virtual functions, so x is actually the address of buff.

But what on earth are you trying to do? There is hardly a line of code there that isn't breaking the standard
in some way (even void main() breaks the standard).

Thanks Jsmith,

well i am still trying to get value of x & y back (through char array) any idea ?
That is one of the most chaotic programs I've seen to date (excluding all of mine). I'm sure there's an easier and better way to do all this.

-Albatross
Last edited on

hello Albatross thanks for taking interest,

ya it is really a chaotic program , my aim is just to convert an object of class to an array of

char (bytes) & get it back ,

(another way can be using file streams writing object to file & read back . correct/easy way ?)

can some modification in above code help to solve problem ?


The most general answer to your question is to use boost::serialization.

Anything else is going to have limitations that are hard to overcome. For example, what
to do if the class has pointers in it? Or, what to do if the class has virtual functions?

If all you have are simple classes/structs containing POD types and containing no
member functions, no destructor, and no constructor
, then just use memcpy();
this is directly supported by the standard.

The moment you add a member function (virtual or not), a destructor (virtual or not), or
a constructor, the class is no longer a simple aggregate, and while memcpy() may work
in those cases, you are no longer standards compliant, and your code could break in
the future or with a different compiler.

The moment you add virtual methods your problem becomes much, much harder since
now the class has an implicit vtable pointer that, while you can store it to a file,
you cannot read it back later and expect the program to work at all.

Pointer data members are essentially the same problem as the vtable pointer problem,
in that you can't write the pointer to disk, read it back later, and expect those pointers
to point to anything useful.

Thanks Jsmith.

hope boost::serialization will help me to solve my problem.

thanks to all.
vivmen, if you after all need to use an union, perhaps you can improve things a bit by using anonymous union {..};, but that's just a minor improvement...
Topic archived. No new replies allowed.