-1

This question about how to control or optimize or remove or deallocate unused memory in unions? We know that the union size is maximum data type descaled inside union. Suppose I declared long type then it allocates 64-bytes of memory but I used only 16-bytes in program. How to optimize the remaining 48-bytes of memory?

#include<stdio.h>
int main(){
  union data_type {
    unsigned long A;
    unsigned int B;
    unsigned short C;
    unsigned char D;
  };
  union data_type my_union;
  my_union.C = 0X09;
  printf("UNION size:%d\n", sizeof(my_union));
  return 0;
}
Max Langhof
  • 23,383
  • 5
  • 39
  • 72
  • 1
    how you do it is by not using a union only use what you need. – old_timer Dec 19 '19 at 12:10
  • 1
    The size of a type/variable is determined at compile-time. You can't change the size at run-time. However, just because your program prints 8 doesn't mean that 8 bytes of memory were actually used inside `main()`. Inspect the disassembly and have some faith in the optimizer. Overall, it's unclear why you care about this (or rather why you think you have to care). What problem are you really trying to solve? – Max Langhof Dec 19 '19 at 12:11
  • 1
    I think you mean 64 *bits* of memory, considering that `long` on some platforms could be 64 bits (and on others 32 bits). – Some programmer dude Dec 19 '19 at 12:13
  • Note that [`std::any`](https://en.cppreference.com/w/cpp/utility/any) will use more or less (dynamically allocated) memory depending on the contained type (but an instance of `std::any` will still always have the same size). But the overhead for this probably means that it won't solve whatever problem you have. – Max Langhof Dec 19 '19 at 12:14
  • On another note, you have *undefined behavior* in your program. The correct `printf` format specifier to print a `size_t` (what the `sizeof` operator returns) in decimal representation is `%zu`. Mismatching format specifier and argument type leads to UB. – Some programmer dude Dec 19 '19 at 12:15
  • Lastly, if you're worried about wasted space of unions, then perhaps unions aren't the correct solution to your problem? – Some programmer dude Dec 19 '19 at 12:16

2 Answers2

1

You cannot optimize or remove that memory. It's going to remain a waste of space and there's nothing you can do about it, if you use union like this. And this is one of the reasons why using union to create "variant" data types is considered bad practice: you end up wasting memory.

If you wish to do generic programming in C, there are several better ways: void pointers combined with enum type information, or code adapted to C11 _Generic etc.

Lundin
  • 195,001
  • 40
  • 254
  • 396
0

If you remove the members A and B, then the size of the union will be the size of unsigned short, thereby minimising the amount of used memory. Given that only the member C is used in the program, this should not be a problem.

P.S. %d is wrong format specifier for size_t.

eerorika
  • 232,697
  • 12
  • 197
  • 326