Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

What is structure padding in regards of microcontrollers?

Status
Not open for further replies.

devendra_devgupta

Member level 3
Joined
Apr 8, 2007
Messages
54
Helped
3
Reputation
6
Reaction score
0
Trophy points
1,286
Activity points
1,632
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
 

Re: Structure padding

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.


Code:
/*--- Relay Structure ---*/

struct RELAYBITS
  {
  unsigned xlr:1;     /* SEL_BAL relay */
  unsigned rca:1;     /* SEL_UNBAL relay */
  unsigned earth:1;   /* EARTHSEL relay */
  unsigned ground:1;  /* GND_RLY relay */
  unsigned unused:3;   /* Alignment bits */
  unsigned mute:1;    /* MUTE relay */
  };

/*--- Relay bits union ---*/

typedef union
  {
  struct RELAYBITS Bits; 
  uint8_t data;
  }RELAY;
  
volatile RELAY RELAYbits;


Relays could be set like,

#define ON 1
#define OFF 0

RELAYbits.Bits.xlr = ON;
RELAYbits.Bits.rca = OFF;

and then the relays serial data can be sent like this.

Code:
 for(ix = 0; ix < 8; ix++)
    {
    CLOCK = 0;
    DATA_OUT = ((RELAYbits.data << ix) & 0x80U) ? ON : OFF; 
    CLOCK = 1;
    }
 

Re: Structure padding

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.
 

Structure padding

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;
} ;

93_1206507857.jpg

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
 

Re: Structure padding

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.
 

Structure padding

SORRY i have shown it wrong here is the right way
struct
{
char a;
int b;
short int c;
char d;
} ;

61_1206589214.jpg

these are differnt locations each 32 bit wide
But my questions are as it is!!!!!!
 

Structure padding

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.
 

Re: Structure padding

It isn't hard to figure out:

Code:
//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.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top