serialization - Sending the contents of a union in C over a network connection -


I am trying to understand and use union in the Linux environment. Suppose I have the following union

  union test {int one; Two long; } T1;   

If I want to write t1.one in a network file descriptor (socket FD), then the additional zero will be written in the FD as the Union chooses the most The big element, which is two; And it can be worse with the union of structures. Can anyone show me how to overcome it?

No, if you write test.one in network FD, So you will write exactly the ' sizeof (int) bytes; I test.one is an int item; The fact is that it is a member of a union, unless you completely use the union.

If you write the whole union, then surely you will write the full size of the union, which will be at least the size of your largest member.

Do not do this.

Which member of the union is current (i.e., the member who has given you a price recently), and just use that member

Again, given the minimum information you provided, it is not clear that it is understandable to use an association.

Comments