Ive written the following simple code to find the addresses of the varialbles

#include<stdio.h>
int main()
{   int a;
	int b;
	char c;
	float d;
	printf("\nInt : %x\nInt : %x\nChar : %x\nfloat : %x",&a,&b,&c,&d);
	return 0;
}

/* OUTPUT

Int : 12ff4c
Int : 12ff48
Char : 12ff47
float : 12ff40

*/

My Question is why is there an extra gap of 3 in the memory??

Recommended Answers

All 3 Replies

What gap are you talking about?

> My Question is why is there an extra gap of 3 in the memory?
Because things like int or floats are more efficiently accessed (or even only possibly accessible) if they're on say a 4 byte boundary (or even 8 bytes for doubles on some machines).

I suppose it's something about the memory "alignment" of the operating system...

sizeof(int)=4
sizeof(char)=1
sizeof(float)=4

12ff4c 12ff48 12ff47 12ff40
= = = = | = = = = | = | = = = | = = = = |
int int char float

the address of data should be the mulitple of the size of data tpye, it's meant that in your example the float could not be located at 12ff44 because of the last byte already occupied by char, also could not be placed at address 12ff43(12ff47-4=12ff43) for the reason it's not the multiple of 4!!

so operating system search the next right address 12ff40 for data "float" and put data there, that's why there is a 3-byte gap.

commented: That's it exactly +21
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.