954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Why address Gap in Char?

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??

basukinjal
Newbie Poster
7 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

What gap are you talking about?

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

> 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).

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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.

bradd
Newbie Poster
1 post since Aug 2008
Reputation Points: 31
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You