There’s a lot of math and script reading involved in memory calculation, but don’t worry — we’ll keep it simple and avoid overwhelming you.
Why Memory Calculation Matters
Memory calculation might seem like something you don’t need at first. But over time, especially as you write more low level code, you’ll realize how important it is.
The more you understand how memory works, the better you will be at writing efficient code. Knowing what’s happening “under the hood” gives you an edge when debugging or optimizing performance.
C
Memory usage in C depends on the system architecture. For example:
- On a 32-bit system, an
inttakes 4 bytes. - On a 64-bit system, an
intmay take 8 bytes.
Some data types, however, use the same memory regardless of the system:
| Data Type | Size (in bytes) |
|---|---|
char |
1 byte |
float |
4 bytes |
double |
8 bytes |
You can use the sizeof() function in C to check the memory size of a variable. When printing with printf, use format specifiers like %zu for sizeof().
Using Structs to Group Variables
Instead of declaring multiple variables like this:
int dude1;
int dude2;
int dude3;
You can use a struct to organize related data together:
typedef struct Dude {
int dudeshii;
} dude_t;
Now, you can create variables of type dude_t and access the dudeshii field:
dude_t myDude;
myDude.dudeshii = 42;
This helps keep your code cleaner and more organized, especially when managing multiple related values.
Padding, Why?
When you create a struct, the compiler may add padding to align the data in memory. This ensures faster access and better performance.
Example 1:
typedef struct Dude {
char job;
int age;
char name;
} dude_t;
Assuming:
char= 1 byteint= 4 bytes
This struct will use 12 bytes instead of the expected 6 bytes because of padding:
char job(1 byte) + 3 bytes paddingint age(4 bytes)char name(1 byte) + 3 bytes padding
Total: 12 bytes
The CPU prefers data to be aligned to specific byte boundaries (e.g., 4 bytes). So it adds padding between fields to ensure proper alignment.
Think of it like this:
Imagine a bookshelf where books need to start on a new shelf every 4 slots. If a book only takes 1 slot, you may have to leave 3 empty slots before placing the next book. Those empty slots are padding.
Optimizing Memory with Reordering
We can reduce the memory used by reordering the struct:
typedef struct Dude {
int age;
char job;
char name;
} dude_t;
Now:
int age(4 bytes)char job(1 byte)char name(1 byte) + 2 bytes padding
Total: 8 bytes (better than 12!)
By placing the larger data types first, we reduce the amount of padding needed.
the compiler adds 3 byte of padding to our var name and var job, to make struct Dude be a properly alligned address, which make our total memory used in struct Dude be 12 bytes. Now to correct this we need to re allign the struct and reduce the calculated memory used in struc dude
typedef struct Dude {
int age;
char job;
char name;
} dude_t;
this is going to result our Dude struct to 8 byte and that is as a result of us calculating re alligning the vars in our struct and not over using them, remember our assumed byte usage on each var then having 4 first with the remaining 2 then we can add the padding 2 also finally to get all thing right.
“this is a good memory save, bruh. 😎”
more to come in let calculate memory II