Hi,
I have little idea of padding.

i read in one of the links that :

all character variables gets stored in the memory location that is divided by 1.

all short variables gets stored in the memory location that is divided by 2.

all int variables gets stored in the memory location that is divided by 4.

so

struct one {
          char ch;
    }O;
  
  printf("%d", sizeof(O));

is 1 byte ( no padding here )

struct two {
          short s;
    }T;
  
  printf("%d", sizeof(T));

is 2byte ( no padding here also )

slly for only one integer 4 bytes.

now

struct Test {
            char ch;
             int i;
    }Te;

printf("sizeof(Te)= %d\n",sizeof(Te));

here i will get out put as 8.

the reason is after storing the character variable next three bytes will be padded and after the i will be stored.

even if we write int i; first and char ch; second

the padding will be after the ch because when we create array of structures it will be fast to access.

my doubt is :

when the memory is allocted for char it can store any where regardless of memoey location because any number can be divided by 1.

say if char stores at location 2000.
next three bytes( 2001, 2002,2003 are padded ) and int will be stored at 2004.
but if char gets location 2001 or 2002 or 2003 then the memory for int should start at 2005,2006,2007 respectively.
then these numbers are not divisible by four.

i tried a program to check for this :

but i always got the first location that is divisible by four.
whether the first element is a char , short , int.
i never got any other location.

is there any reason that
always the first member of the structure stores at location that is divisible by four.
so that the rest will be easy to analyse.

Thanks,
Danian.

Recommended Answers

All 5 Replies

Memory is aligned on word size. This is done for efficiency reasons, You really should continue exploring memory and how its laid out, its an interesting subject - may I suggest you look into packed structures..

Also for an exercise could you create an unsigned integer that isn't aligned on word size?

Hint: See the example program at the end of this article

Hello Gurus,

I understood that pragma pack will avoid unneccessary packing.

#pragma pack(2)
provides the allignment on 2 byte buondary.

#pragma pack(2)
provides the allignment on 2 byte buondary.


but the question what i asked was :

will the first member always gets the location that is divisible by four.

i am sorry if i could not understand the info provided correctly.

Hello Gurus,

I understood that pragma pack will avoid unneccessary packing.

#pragma pack(2)
provides the allignment on 2 byte buondary.

#pragma pack(2)
provides the allignment on 2 byte buondary.


but the question what i asked was :

will the first member always gets the location that is divisible by four.

i am sorry if i could not understand the info provided correctly.

If you leave variable addressing up the compiler then yes, all addresses will be aligned on word size(and I would say your word size is 4 bytes).

If you engage packed structures then the first variable is aligned and the rest are packed.

If you use some custom addresses scheme then addressing is whatever you want it to be.

Note: Variable addressing should be left to the compiler unless you have a specific reason to change it like, hardware programming, systems programming.

Here's an example where you should use a packed structure - to get the base address and limit of the interrupt/global descriptor tables. Here the program 'should' use a packed structure. Note - you could achieve the same thing without packed structures but the code won't be as readable.

#include <stdio.h>
#include <stdlib.h>

struct id
{
	unsigned short limit;
	void *base;
}__attribute__((packed)) myidtr, mygdtr;


int main(int argc, char**argv)
{
	__asm__ __volatile__
	(
		"sidt	myidtr\n\t"
		"sgdt	mygdtr\n\t"
	);
	fprintf(stdout, "myidtr.limit->%u\n", myidtr.limit);
	fprintf(stdout, "myidtr.base->%p\n", myidtr.base);
	fprintf(stdout, "mygdtr.limit->%u\n", mygdtr.limit);
	fprintf(stdout, "mygdtr.base->%p\n", mygdtr.base);
	exit(EXIT_SUCCESS);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.