I'd like to be aware of any possible optimization you people know in
terms of efficient memory usage by objects of class/Union types, like
somebody mentioned to me There are some rules about two objects having
same or overlapping memory addresses but not with any detail.
Well, you should be aware of variable sizes if memory is a big issue for
you. Compilers will normally add padding bytes to make variables line up
correctly. For example, a 4 byte integer may want to start on every 4th
byte. Some interger types break in fact if they are aligned wrong.
Consider a structure in which we have 2 1 byte chars and 2 4 byte integers.
We can do it a few ways:
struct foo {
char c1;
char c2;
int i1;
int i2;
};
struct bar {
int i1;
char c1;
int i2;
char c2;
};
Doesn't seem like it should make a difference, right? Do a sizeof on both
of these and you shoudl find that the sizeof foo is 12 and the sizeof bar is
16. Huh? Where did those 4 bytes go? This is how it lines up in memory:
foo:
1 byte for c1
1 byte for c2
2 padding bytes to make int line up
4 bytes for int i1
4 bytes for int i2
1 + 2 + 2 + 4 + 4 = 12.
bar:
4 bytes for i1
1 byte for c1
3 padding bytes to make int line up
4 bytes for int i2
1 byte for char c2
3 padding bytes to make whole struct line up
4 + 1 + 3 + 4 + 1 + 3 = 16
Without those last 3 padding bytes the size would of been 13. If you have 2
of these structures in an array bar x[2]; the system has to ensure that
everythign lines up, so that is why the 3 bytes at the end, to make i1
always start on a evenly divisible by 4 boundary.
I don't know all the rules, and they may change from system to system from
compiler to compiler from year to year. But what I generally stick with is:
The system is going to line somethign up based on it's size. A char is size
1, so it doesn't matter. A short is size 2, so every other byte, or every
even bit. 4 btye ints line up on 4 bytes, etc...
Normally you don't need to worry about these rules, but you need to be aware
about them. Soemtimes you may try to read binary data into a structure and
wonder why things aren't lining up right.