Skip to main content

Union and Memory Allocation in C

Memory Allocation

You can allocate memory for unions using two methods:

  • Static memory allocation
  • Dynamic memory allocation

Static memory allocation

  • In this method, the memory for the union is allocated at compile-time.

  • This method is suitable when the size of the union is fixed and known at compile-time.

Syntax

union union_name {
// union members
};

int main() {
union union_name u; // static memory allocation
// use union members
return 0;
}

Example

#include <stdio.h>

// define a union type named "my_union"
union my_union
{
int i;
float f;
char c;
};

int main()
{
union my_union u; // allocate memory for the union statically
u.i = 42; // set the value of the "int" member
printf("The value of the integer member is %d\n", u.i);
u.f = 3.14; // set the value of the "float" member
printf("The value of the float member is %f\n", u.f);
u.c = 'A'; // set the value of the "char" member
printf("The value of the char member is %c\n", u.c);
printf("The value of the integer member is %d\n",
u.i); // accessing the "int" member after setting the "char" member
// overwrites its value
return 0;
}
Output:

Explanation:

  • We define a union type named my_union that has three members: an integer named i, a float named f, and a character named c.
  • In the main function, we declare a variable u of type my_union, which allocates memory for the union statically.
  • We then set the value of the int member to 42 using the dot notation, and print its value.
  • We then set the value of the float member to 3.14 using the dot notation, and print its value.
  • We then set the value of the char member to 'A' using the dot notation, and print its value.
note
  • Note that when we set the value of the char member, it overwrites the value of the int member, since a union can hold only one member at a time.
  • Thus, when we print the value of the int member again, we get the value that was last set in the union.

Dynamic memory allocation

  • In this method, the memory for the union is allocated at run-time.

  • This method is suitable when the size of the union is not fixed and known only at run-time.

Syntax

union union_name {
// union members
};

int main() {
union union_name *u = (union union_name*) malloc(sizeof(union union_name)); // dynamic memory allocation
// use union members
free(u); // free memory when done
return 0;
}

Explanation

  • We use the malloc() function to allocate memory for the union.
  • The sizeof() operator is used to get the size of the union, which is then passed as an argument to malloc().
  • We cast the return value of malloc() to the type of the union to ensure that the pointer returned by malloc() points to a block of memory of the correct size.
  • When we are done using the union, we must free the memory allocated by calling the free() function.

Example

#include <stdio.h>
#include <stdlib.h> // include the standard library header for malloc() and free()

// define a union type named "my_union"
union my_union
{
int i;
float f;
char c;
};

int main()
{
union my_union *u = (union my_union *)malloc(
sizeof(union my_union)); // allocate memory for the union dynamically

u->i = 42; // set the value of the "int" member using the arrow notation
printf("The value of the integer member is %d\n", u->i);

u->f = 3.14; // set the value of the "float" member using the arrow notation
printf("The value of the float member is %f\n", u->f);

u->c = 'A'; // set the value of the "char" member using the arrow notation
printf("The value of the char member is %c\n", u->c);

printf("The value of the integer member is %d\n",
u->i); // accessing the "int" member after setting the "char" member
// overwrites its value

free(u); // free the memory allocated for the union

return 0;
}
Output:

Explanation:

  • We define a union type named my_union that has three members: an integer named i, a float named f, and a character named c.
  • In the main function, we declare a pointer variable u of type my_union, which will point to the block of memory allocated for the union dynamically.
  • We use the malloc() function to allocate memory for the union dynamically.
  • We pass the size of the union, which is obtained using the sizeof() operator, as an argument to malloc().
  • We cast the return value of malloc() to the type of the union to ensure that the pointer returned by malloc() points to a block of memory of the correct size.
  • We then set the value of the int member to 42 using the arrow notation, and print its value.
  • We then set the value of the float member to 3.14 using the arrow notation, and print its value.
  • We then set the value of the char member to 'A' using the arrow notation, and print its value.
  • Finally, we use the free() function to free the memory allocated for the union when we are done using it.
note
  • Note that when we set the value of the char member, it overwrites the value of the int member, since a union can hold only one member at a time.
  • Thus, when we print the value of the "int" member again, we get the value that was last set in the union.

Comparion

Comparison table of static memory allocation and dynamic memory allocation for a union:

PropertyStatic memory allocationDynamic memory allocation
Memory allocationMemory for the union is allocated at compile timeMemory for the union is allocated at run time
Memory sizeThe memory size of the union is fixed and determined by its definitionThe memory size of the union can be determined at run time by the programmer
Lifetime The memory for the union exists throughout the entire program executionThe memory for the union can be allocated and freed during program execution
AccessAccess to union members is done using the dot notation or the arrow notationAccess to union members is done using the pointer notation and the arrow notation
Initialization Union members can be initialized at compile timeUnion members can be initialized at run time using the arrow notation
Flexibility Static memory allocation provides less flexibility as the size of the union is fixedDynamic memory allocation provides more flexibility as the size of the union can be changed during program execution
tip
  • The choice between static memory allocation and dynamic memory allocation for a union depends on the specific requirements of the program.
  • If the size of the union is fixed and known at compile time, then static memory allocation can be a good choice.
  • If the size of the union is not fixed or needs to be determined at run time, then dynamic memory allocation may be a better choice.