Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any difference between the members of this C union?

The typedef below is for the DIR register from the Atmel SAMD21 ARM MCU include file. Since the bit struct member and the reg member are both 32 bits, is there any difference between the two members in the union?

I'm trying to understand why they did not just use a uint32_t as the type for the DIRSET register. My only thought that they just defined it this way to be consistent with other registers where there are multiple fields within the bit struct.

typedef union {
    struct {
        uint32_t DIRSET:32;
    } bit;
    uint32_t reg;
} PORT_DIRSET_Type;
like image 698
crj11 Avatar asked Feb 27 '19 04:02

crj11


People also ask

What is the union in C?

A union is a special data type available in C that allows to store different data types in the same memory location. You can define a union with many members, but only one member can contain a value at any given time. Unions provide an efficient way of using the same memory location for multiple-purpose.

What are the limitations of union in C?

Here, are cons/drawbacks for using union: You can use only one union member at a time. All the union variables cannot be initialized or used with varying values at a time. Union assigns one common storage space for all its members.

How do you define a Union in C?

struct [structure name] { member definition; member definition; ... member definition; }; A union is a special data type available in C that allows storing different data types in the same memory location. You can define a union with many members, but only one member can contain a value at any given time.

What are the similarities between structures and unions in C?

Similarities between Structure and Union. Both are user-defined data types used to store data of different types as a single unit. Their members can be objects of any type, including other structures and unions or arrays. A member can also consist of a bit field. Both structures and unions support only assignment = and sizeof operators.

What are the drawbacks of C Union?

The drawback of the union is that only the last entered value to the union will only be available. It should be used when members of the union need not be available to be accessed at the same time. This is a guide to C Union.

What is the difference between Union and structure in Java?

They are both container data types, and they are capable of holding any data type. Although there is one very major difference between them. The union has the same memory location for all of its members, while the Structure possesses separate ones for the same purpose. Let us understand more differences between them.


1 Answers

From a general point of view, it's just code bloat - there is no reason why you would ever want to write code like that. However, the ASF coding style is that every register access ends with .reg, so that's the reason here: they want to keep register naming and use consistent.

They could of course just have done that with typedef struct { uint32_t reg; } PORT_DIRSET_Type but this code base is rarely ever rationally written. It could have been automatically generated through some script.

As a rule of thumb, register maps like these are always horribly ugly and non-portable, filled with irrational code. Those shipped as part of ASF are some of the worst I've ever seen all categories.

like image 178
Lundin Avatar answered Sep 28 '22 16:09

Lundin