rahul said:
How is the memory allocated for structures? I need to optimize the
memory usage and bit fields are not doing the trick.
Any details about the memory allocation for the structures would be a
great help.
You'll probably get better help if you ask a more specific question.
Show us what you want to put in the structure.
The members of a struct are stored in the order you specify. Some
members may have types that require a particular alignment, typically
equal to their size. For example, while chars can go anywhere you may
find that if ints are 4 bytes long then they are always placed on
4-byte boundaries. This is common even if the processor allows
arbitrary alignment, because it's usually faster to access
suitably-aligned data.
Suppose shorts are 2 bytes and ints are 4, with corresponding
alignment requirements, and you want to store 2 chars, a short, and an
int in your struct. These could fit in 8 bytes, and will if you order
them correctly, for example
struct foo {
int a;
short b;
char c, d;
};
but if you do
struct foo2 {
char c;
int a;
char d;
short b;
};
it will take 12 bytes, because the 3 bytes after c and the byte after d
are wasted.
If you use bitfields, then you have to consider similar issues at the
bit level. Adjacent bitfields that fit within a "unit" (probably an
int) will be packed together, so don't spread them out amongst other
members of the struct, and try to order them so that they don't go
over too many int boundaries.
-- Richard