How do I make the following C structure more memory efficient?

Luxdragon

I am trying to solve a rather challenging programming task. We are given the following 64-bit structure, which allows us to store a point in time accurate to the minute:

typedef struct
{
  unsigned char day;  //1 Byte
  unsigned char month; //1 Byte
  unsigned int year;  //4 Bytes
  unsigned char hours; //1 Byte
  unsigned char minutes; //1 Byte
} Time; //4 Bytes are probably used for padding

This structure has the size of 12 Bytes (I checked this and the struct really does use so much space). The task is to reduce the size to 8 Bytes and we are not allowed to use unions. We are supposed to use a lot of these structures, hence why we want to reduce the memory size.

The only thing I can think of is to change the unsigned int to unsigned short, but how can we get rid of the other two Bytes?

Kind regards

Lundin

The main problem here is that char have no alignment requirement, but int has a requirement of 4 byte alignment (on a 32 bit system). Meaning it must start at an address divisible by 4. Structs are guaranteed to start at an aligned address, so what you get is likely this:

unsigned char day;  //1 Byte
unsigned char month; //1 Byte
// 2 byte padding!
unsigned int year;  //4 Bytes
unsigned char hours; //1 Byte
unsigned char minutes; //1 Byte
// 2 byte padding!

The first two padding bytes are there to ensure that the int is aligned, the last two are there to ensure that the next struct in an array of structs start at an aligned address.

The fix is simple, just move year to the top of the struct:

unsigned int year;  //4 Bytes
unsigned char day;  //1 Byte
unsigned char month; //1 Byte
unsigned char hours; //1 Byte
unsigned char minutes; //1 Byte

And now the struct should be 8 bytes large with zero padding.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I turn the following if statements into a more efficient nested for loops

How do I make this VBA code more efficient so it does not crash due to lack of memory?

How do I make this matching list code more efficient?

How do I make the selection function more efficient?

How do I make my pygame levels more efficient?

How can I make this C# code more efficient?

How can I make this more efficient? (Merging arrays in C)

How would you make the following doubling function more efficient and cleaner?

How would I make this script more efficient?

How can I make this loop more efficient?

How can I make this more efficient in Android?

How would I make this more efficient? (python)

How to do the following matrix multiplication more efficient in Matlab?

How can I write the following code in a more efficient and pythonic way?

C++ how to make if statements more efficient

How do I create a "spacer" in a C++ class memory structure?

How do I make this function for concatenating Excel sheets from a single file more efficient?

How do I make a more efficient color changer? VB.net

How do I make my VBA code more efficient using For each loops for three different ranges?

How do I make this more efficient- Consider the fraction, n/d, where n and d are positive integers

How can I make this PyTorch tensor (B, C, H, W) tiling & blending code simpler and more efficient?

How do I make a class with the following requirements?

How do I make efficient queries in Django?

How do i make efficient slots in an inventory?

How can I make a code more efficient and shorter?

How can I make this Python code more efficient

How can I make my pandas code more efficient?

How can I make my trie more efficient?

How can I make this PyTorch heatmap function faster and more efficient?