I'm reading my C++ book and I've come to a chapter on structures and unions. The book has tried to explain the difference but I'm not getting it's explanation. Playing with the structure examples in the book reminds me of accessing attributes to a python Class but apart from that, probobly inacurate observation, I don't really get them.

Playing with the structure examples in the book reminds me of accessing attributes to a python Class

That's not a bad analogy. A structure in C++ is just a class with different default access (public instead of private). A union is different in that all members of a union share the same block of memory rather than each member having its own block of memory.

When you say 'member' are you refering to the, what would be an object if the structure were a class or something else?

if the structure were a class

In C++, a struct IS a class. A class IS a struct. The only difference is the default member visibility (and related default inheritance). Everything you know about a class in C++ you also know about struct, because they're the same thing.

When you say 'member' are you refering to the, what would be an object if the structure were a class or something else?

No, he's talking about what comes after the . in foo.bar (foo is the object, bar is a member of that object) or in other terms: members are the things you define inside the class/struct/union body.

The main distinction, which decepticon was trying to get across, is that each member variable in a struct has its own memory within the structure, and the structure size is the aggregate of all the member variables. A union is only as big as the largest member of the union. So if you have a struct like this:

struct foo {
    int x;
    char* y;
    double z;
};

The size of struct foo is sizeof(x)+sizeof(y)+sizeof(z). However if you have this union:

union foo {
    int x;
    char* y;
    double z;
};

The size of union foo is the largest of these elements, which depends upon the system architecture. The size of x would be likely 32-bits, z would likely be 64-bits, and y could be anything from 32-bits to more than 64-bits, unless you are programming an 8 or 16 bit embedded processor, in which case they will be smaller. So, assuming you have a 64-bit system, then the size of union foo would be at least 64 bits, and the size of struct foo would be at least 32+64+64, or 160 bits.

You can make out the different of structure and union by going through the following points.

Structure :
1.The keyword struct is used to define a structure
2. When a variable is associated with a structure, the compiler allocates the memory for each member. The size of structure is greater than or equal to the sum of sizes of its members. The smaller members may end with unused slack bytes.
3. Each member within a structure is assigned unique storage area of location.
4. The address of each member will be in ascending order This indicates that memory for each member will start at different offset values.
5 Altering the value of a member will not affect other members of the structure.
6. Individual member can be accessed at a time
7. Several members of a structure can initialize at once.

Union :
1. The keyword union is used to define a union.
2. When a variable is associated with a union, the compiler allocates the memory by considering the size of the largest memory. So, size of union is equal to the size of largest member.
3. Memory allocated is shared by individual members of union.
4. The address is same for all the members of a union. This indicates that every member begins at the same offset value.
5. Altering the value of any of the member will alter other member values.
6. Only one member can be accessed at a time.
7. Only the first member of a union can be initialized.

Structures encapsulate the data members. Unions are similar to structures, but data members share memory, and unions may access members as different types. The class key used for stucture is 'struct' and in the case of uniion, the class key is 'union'. Both of them uses 'public' as the default access specifier.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.