I have a templated union type NodeType. The problem is that upon assignement of its variable field are not set properly and when subsequently accessed the contain garbage values. Specifically, the problem occurs with _field1 and _field2 - see the main.
#include <cstdlib>
#include <iostream>
using namespace std;
template <class T>
struct structExampleType
{
T _structField;
typedef structExampleType<T>* pointerStructExample;
};
enum TYPE_tag
{FIELD1_tag, FIELD2_tag} TYPE_tag;
template <class T>//, class P>
union NodeType
{
enum TYPE_tag _nodeType_tag;
char _field1;
typename structExampleType<T>::pointerStructExample _field2;
NodeType(){_field2=0;}
NodeType(enum TYPE_tag nodeType_tag, char _charField)
{
_nodeType_tag=nodeType_tag;
_field1=_charField;
//_field2=0;
}
NodeType(enum TYPE_tag nodeType_tag, typename structExampleType<T>::pointerStructExample pointer)
{
_nodeType_tag=nodeType_tag;
_field2=pointer;
//_field1='-';
}
};
int main(int argc, char *argv[])
{
NodeType<int> node1, node2;
structExampleType<int>* structExamplePointer;
structExamplePointer=new structExampleType<int>();
structExamplePointer->_structField=100;
// structExamplePointer->_field2=structExamplePointer;
node1=NodeType<int>(FIELD1_tag,'-');
node2=NodeType<int>(FIELD2_tag,structExamplePointer);
cout<<endl<<"node1: ";
if (node1._nodeType_tag==FIELD1_tag)
{cout<<node1._field1<<endl;}
else
{cout<<node1._field2<<endl;}
cout<<endl<<"node2: ";
if (node2._nodeType_tag==FIELD2_tag)
{cout<<node2._field1<<endl;}
else
{
cout<<node2._field2<<endl;
cout<<(node2._field2)->_structField<<endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
What can be the issue? Thanks in advance for your time.
In
main
you have these two lines:Remember that local variables are not initialized unless they have a default constructor (which pointers do not have), so in the second you dereference the uninitialized pointer
structExamplePointer
, leading to undefined behavior.You also seem to misunderstand the purpose of
union
, as you try to set multiple fields. In aunion
all fields share the same space, so when writing to one member then all members are changed. Only the last written to member is valid. Trying to use other fields may again lead to undefined behavior.If you need to have a "tag" field and other data, a structure containing the tag and the union data should be your choice: