Optimizing Memory Usage in C Structures
Optimizing Memory Usage in C Structures
Memory Usage
in C Structures
for Embedded
Systems
Table of Contents
Table of Contents
1. Introduction
2. Minimize Padding Using Proper Member
Ordering
3. Use #pragma pack or
__attribute__((packed)) to Remove Padding
4. Use bit-fields for Compact Representation
of Small Values
5. Use Smaller Data Types When Possible
6. Avoid Unnecessary Pointers
7. Use Unions to Share Memory Space
8. Avoid Wasting Space with Boolean Types
9. Use Dynamic Memory Allocation
10.Use const to Store Read-Only Data in
Flash/ROM Instead of RAM
8. Avoid Wasting Space with Boolean Types
Table
9. Use Dynamic of Contents
Memory Allocation
10.Use const to Store Read-Only Data in
Flash/ROM Instead of RAM
11.Best Practices Summary
12.Example of an Optimized Structure
13.Conclusion
1. Introduction
1. Introduction
Memory is one of the most valuable
resources in embedded systems, where RAM
and storage are often limited due to hardware
constraints. Efficient memory management
can significantly improve the performance and
reliability of embedded applications.
One of the most effective ways to conserve
memory is by optimizing the way C
structures are defined and used.
Problem:
The C compiler often inserts padding to align
members for better memory access, which
wastes space.
Solution:
• Group members of similar size together to
reduce padding.
• Place smaller members together or after
larger members.
Example:
Instead of this:
• Place smaller members together or after
2. Minimize Padding Using
largerProper
members.
Member Ordering
Example:
Instead of this:
Why?
• The first example has padding added after
2. Minimize Padding Using
Proper Member Ordering
Why?
• The first example has padding added after
a and c to align int properly.
• The second example reduces padding by
grouping smaller fields together, saving
memory.
3. Use #pragma
pack or
__attribute__((pac
ked)) to Remove
Padding
3. Use #pragma pack or
__attribute__((packed)) to
Remove Padding
Example:
or:
3. Use #pragma pack or
__attribute__((packed)) to
Remove Padding
or:
Trade-off:
• This may lead to misaligned access, which
can reduce performance on some
architectures.
4. Use bit-fields
for Compact
Representation of
Small Values
4. Use bit-fields for Compact
Representation of Small Values
Example:
Why?
• This packs the structure into a single 8-bit
integer instead of needing four separate
integers (potentially 16 or 32 bytes).
Trade-off:
integer instead of needing four separate
4. Use bit-fields for Compact
integers (potentially 16
Representation of or 32 bytes).
Small Values
Trade-off:
• Bit-fields may generate additional masking
and shifting operations, which can affect
performance.
5. Use Smaller
Data Types When
Possible
5. Use Smaller Data Types
When Possible
Example:
Why?
• This reduces the overall structure size.
6. Avoid
Unnecessary
Pointers
6. Avoid Unnecessary
Pointers
Example:
Instead of:
Use:
6. Avoid Unnecessary
Pointers
Use:
Why?
• Reduces the overhead of storing
addresses and dereferencing.
7. Use Unions to
Share Memory
Space
7. Use Unions to Share
Memory Space
Example:
Why?
Only one of the union members is stored at a
time, saving memory.
8. Avoid Wasting
Space with
Boolean Types
8. Avoid Wasting Space with
Boolean Types
Example:
Instead of:
Use:
8. Avoid Wasting Space with
Boolean Types
Use:
Example:
Instead of:
Use:
9. Use Dynamic Memory
Allocation
Use:
Why?
• Reduces the fixed memory footprint.
• You can allocate exactly the required size.
10. Use const to
Store Read-Only
Data in Flash/ROM
Instead of RAM
10. Use const to Store Read-
Only Data in Flash/ROM Instead
of RAM
Example:
Why?
• Saves RAM because the structure is stored
in Flash/ROM instead.
11. Best Practices
Summary
11. Best Practices Summary
12. Example of an
Optimized
Structure
12. Example of an
Optimized Structure