It doesn't matter what the word size is it matters what the specified alignment for your system is.
A system with a 32 bit word size could still use 1, 2, 4 or other alignments for it's data. You will find this information either in your processor data sheet or your compiler manual (or both).
Reducing memory usage or conforming to a particular memory layout for an interface to another machine or program is often a requirement for C/C++ programs. ..... this can be done by removing the padding between structure fields...
Reducing memory usage or conforming to a particular memory layout for an interface to another machine or program is often a requirement for C/C++ programs. ..... this can be done by removing the padding between structure fields...
Well why didn't you say this in the first place, regardless of the structure alignment that your platform uses you normally get the best use of memory in structures by ordering the elements in the structure by size, (largest first or smallest first I don't think it matters).
Another way to save space if the structure contains a lot of bools is to use bit fields instead, this also works if you have a lot of ints that only take a few values (you can hold values in the range 0-3 in 2 bits).
You should not try to map structures to the interface of another computer or memory mapped piece of hardware, this is asking for trouble. You should read/write the individual chars, shorts and longs from there location in memory and write/read them into the correct firld of your structure.
Well why didn't you say this in the first place, regardless of the structure alignment that your platform uses you normally get the best use of memory in structures by ordering the elements in the structure by size, (largest first or smallest first I don't think it matters).
Another way to save space if the structure contains a lot of bools is to use bit fields instead, this also works if you have a lot of ints that only take a few values (you can hold values in the range 0-3 in 2 bits).
You should not try to map structures to the interface of another computer or memory mapped piece of hardware, this is asking for trouble. You should read/write the individual chars, shorts and longs from there location in memory and write/read them into the correct firld of your structure.
yes even you do that there is a padding between int and char. that padding is system and its compiler specific. the C lib has got a function pragmapack, to use for this purpuse only.......
pragma(n)...... .. have you use it............. .
The question is how much it realy takes when you write a driver in a small chip....
1 byte is a big memory there.......
sizeof function will give you the out put what actually an int or char has consume.....
Thats theory only.... practically its the difference....
yes even you do that there is a padding between int and char. that padding is system and its compiler specific. the C lib has got a function pragmapack, to use for this purpuse only.......
pragma(n)...... .. have you use it............. .
No it doesn't, your implementation of C may provide a #pragma pack preprocessor directive but this is NOT standard C and many compilers do not provide this or any way to change structure alignment, you have to live with what ever it does. If your compiler has a #pragma pack and you use it then you will implicitly introduce an portability issue if the hardware of your project ever changes.
Originally posted by rohitbasu77
The question is how much it realy takes when you write a driver in a small chip....
1 byte is a big memory there.......
sizeof function will give you the out put what actually an int or char has consume.....
Thats theory only.... practically its the difference....
I am fully aware of the memory constraints that occur on smaller platforms (having programmed on a PIC with <4k RAM for the past year or so) the sizeof operator will give you the sizeof the type, if that type is a structure then it will include the padding (otherwise pointer arithmetic would not work properly).
Take this code for instance
[code=c]
struct MyStruct {
int i;
char c;
};
struct MyStruct mystruct;
int sms = sizeof mystruct;
int si = sizeof mystruct.i;
int sc = sizeof mystruct.c;
[/code]Assuming that the system has 4 byte (32 bit ints) and that it uses 4 byte alignment then once this runs
sms = 8;
si = 4;
sc = 1;
You can always find out how much wasted space there is in a structure on a given compiler/platform by comparing the sizeof the struct with the size of it's components, in this case 3 bytes (8 - (4+1)).
However it you use the rules I gave previously then wasted space will be minimised (typically 0-3 bytes per structure) on most platforms.
If you can not afford that wasted space don't use structures, although this will make the code more complex so you need to give the problem some careful thought before going down this route.
Comment