Hello friends,
I have gone through various sites and understood what is structure padding, But no body tells it in the context of a microcontroller. My questions are:
1) Why only structures are padded
2) what is the difference between padding of same structure in 8 bit, 16 bit and 32 bit microcontrollers
Structure padding is normaly associated with bit fields and unions for allignment.
For example, if you had a serial to parallel 8 bit relay driver, with 3 unused outputs. You could use the following data structure.
Another form of structure padding is aligning data to an even boundary in memory. On Motorola 68000 processors the quickest way to crash a program is to access a two or four byte variable that starts at an odd address. So compilers will automatically align the data for you.
Thankyou ver much my dear friends
In my view, for example in a 32 bit controller following structure will be padded like this
struct
{
char a;
int b;
short int c;
char d;
} ;
My question are
1)What happens if this structure is defined on 8 bit and 16 bit controllers
2)How padding helps compiler for efficient programming even if it takes more space.
3)Finally why only structures are padded. Why not normal variables
I dont think the example you give would be stored as you suggest.
If it was a union, yes, then all variables occupy the same space and require the same number of bytes, in this case 4.
I believe most compilers would pack this structure to 8 bytes, unless you requested with a pragma that the structure is alligned. To allign every member of a structure to the largest variable would be a waste of valuable memory space.
Hey friends please reply. If you don't know the answer try to find out asking your friends/ forums. This not only improves our knowledge but concepts too.
//Using DevC++:
#include <iostream.h>
#include <conio.h>
int main(int argc, char *argv[])
{
struct
{
char a;
int b;
short int c;
char d;
} foo;
printf("%p %p %p %p \n " , &foo.a, &foo.b, &foo.c, &foo.d);
getch();
return 0;
}
The output is:
0022FF60 0022FF64 0022FF68 0022FF6A
But there are still lots of unanswered questions. First, the guideline that drives C
programming is, make it as quick and as efficient and as small a footprint as possible. That is C does not write babysitter code, it will not check array bounds or
set memory to 0. This takes away from speed, efficiency and code size. Now, why wouldn't an optimizing compiling take a bunch of chars declared in a struct and put them together to decrease the size of a struct, even if they are not declared in that order? I would imagine 8 bit micros would do that as a matter of routine, even if a compiler for a big fat pc running at 3GHz with 2 Gig of memory might not feel compelled to. Also, does a compiler has to declare data from low memory to high? I imagine they could declare the data going from higher in memory downwards. It all depends on the processor you are writing for. I wrote some 16 bit integer math code for a pic, all done in extended 8 bit math of course. I put the low order byte in the higher memory space. I reversed the usual order, but the code ran fine, and it was more efficient because of the way the INDF register works. If you assumed you could peek at the low order byte by looking at the low order byte of the 16 bit variable you would be wrong. Does NULL (for an unassigned pointer) have to be zero? No, it's in capital letters because it is a macro, it can be any value a compiler wants NULL to be. On one 8 bit micro, NULL pointed to the exception vector that handled invalid meory accesses, that is, it was not zero. So it's all how you want to write the compiler in C. Always be aware of that.