Limitations on the Use of Strings in Unions

Top  Previous  Next

In Tibbo C, unions cannot include string members or member structures, whose members include strings:

 

Tibbo C:

struct packet_x{
  unsigned char packet_type;
  string dt(20); //here is a member string
};
 
packet_x p_x; //a structure with a string member -- this is OK
 
...
 
union packet{
  packet_x p_x; //this will generate an error (not a POD-type") -- packet_x has a string member
  ...
};
 
struct mega_struct{
  packet_x p_x; //this member is a structure with a string member -- this is OK
  ...
};

 

The reason for this limitation is that string variables include metadata (current length and capacity), which could potentially become invalid as a result of union manipulation (for example, it would be possible to set current length > capacity).

Related compiler error looks like this:

 

Output pane:

(prj)\c.tc(19) : error C1203: invalid union member type: 'struct p_x' is not a POD-type

 

"POD" stands for "plain old data". Strings contain metadata, so they are not "plain old data".