Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
01-18-2007, 04:33 AM
|
#1
|
Member
Registered: Oct 2004
Posts: 45
Rep:
|
trretretre
Last edited by shivaligupta; 01-20-2007 at 01:25 AM.
|
|
|
01-18-2007, 07:03 AM
|
#2
|
LQ Newbie
Registered: Mar 2005
Location: FL, USA
Distribution: SuSE
Posts: 28
Rep:
|
Ordinarily you shouldn't care how the compiler organizes variables, because it will be architecture-specific.
My computer: Linux wolverine 2.6.13-15.12-default #1 Thu Aug 24 11:23:58 UTC 2006 i686 athlon i386 GNU/Linux
Your example program reports:
Sizeof cool is 8
Sizeof cool3 is 20
Sizeof cool2 is 0
Sizeof cool4 is 2
address of cool is bfa5e1a0
address of cool3 is bfa5e18c
address of cool2 is bfa5e18c
address of cool4 is bfa5e18a
&heh1 is bfa5e184
|
|
|
01-18-2007, 08:59 AM
|
#3
|
Member
Registered: May 2002
Posts: 964
Rep:
|
I don't get why it matters. Ken's answer is right on. Each implementation is free to park those variables wherever it wants.
What problem are you trying to solve?
Last edited by jim mcnamara; 01-18-2007 at 09:01 AM.
|
|
|
01-18-2007, 10:56 PM
|
#4
|
Member
Registered: Oct 2004
Posts: 45
Original Poster
Rep:
|
Hi all!
I agree with ken and jim that the addresses assigned will be architecture specific. and the addresses shown on ur machine are easily justified as well. but the values which I see on my machine are not.
U can see below that the difference between address of cool3 and cool2 is 0x10 bytes(16 bytes) where as size of cool3 is 20 bytes?
and why is there a gap of 40 bytes in cool and cool3 address? i mean even if we consider memory alignment, padding etc, i couldnt find out the reason for so much gap. i mean this gap wont be used by anything at all. so is it that that memory is being wasted?
Regards,
Shivali
|
|
|
01-18-2007, 11:14 PM
|
#5
|
Member
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207
Rep:
|
What are &heh1 and &heh2?
|
|
|
01-19-2007, 12:02 AM
|
#6
|
Member
Registered: Oct 2004
Posts: 45
Original Poster
Rep:
|
heh1 and heh2 are just two pointer vaiables. &heh1 is address of that pointer variable(heh1). i m pasting their address also below now:
#include <stdio.h>
struct heh
{
long* ptr;
char data[0];
};
void hello()
{
double cool;
char cool3[20];
char cool2[0];
char cool4[2];
struct heh* heh1, heh2;
printf("\nSizeof cool is %d \n", sizeof(cool));
printf("\nSizeof cool3 is %d \n", sizeof(cool3));
printf("\nSizeof cool2 is %d \n", sizeof(cool2));
printf("\nSizeof cool4 is %d \n", sizeof(cool4));
printf("address of cool is %x\n", &cool);
printf("address of cool3 is %x\n", &cool3);
printf("address of cool2 is %x\n", &cool2);
printf("address of cool4 is %x\n", &cool4);
printf("\n&heh1 is %x ", &heh1);
printf("\n&heh2 is %x ", &heh2);
}
int main()
{
hello();
return 0;
}
Output:
=======
Sizeof cool is 8
Sizeof cool3 is 20
Sizeof cool2 is 0
Sizeof cool4 is 2
address of cool is bfffbd28
address of cool3 is bfffbd00
address of cool2 is bfffbcf0
address of cool4 is bfffbcee
&heh1 is bfffbce8
&heh2 is bfffbcd0
|
|
|
01-19-2007, 02:50 AM
|
#7
|
Member
Registered: Jun 2006
Distribution: Ubuntu 7.04 Feisty
Posts: 79
Rep:
|
hi
try to compile your code with cc -S which will give u
assembly code
then open it with vi editor to see assembly code for your machine
try to analyse it
bye
|
|
|
01-19-2007, 08:41 AM
|
#8
|
LQ Newbie
Registered: Mar 2005
Location: FL, USA
Distribution: SuSE
Posts: 28
Rep:
|
Quote:
Originally Posted by shivaligupta
I agree with ken and jim that the addresses assigned will be architecture specific. and the addresses shown on ur machine are easily justified as well. but the values which I see on my machine are not.
U can see below that the difference between address of cool3 and cool2 is 0x10 bytes(16 bytes) where as size of cool3 is 20 bytes?
and why is there a gap of 40 bytes in cool and cool3 address? i mean even if we consider memory alignment, padding etc, i couldnt find out the reason for so much gap. i mean this gap wont be used by anything at all. so is it that that memory is being wasted?
|
On what kind of system are you running your test program?
If "uname" is available, what does "uname -a" tell you ?
What options are you giving the C compiler to compile and build?
|
|
|
01-19-2007, 11:04 PM
|
#9
|
Member
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207
Rep:
|
The same thing happens to me:
Code:
address of cool is 0xbfac2008
address of cool3 is 0xbfac1fe0
address of cool2 is 0xbfac1fd0
address of cool4 is 0xbfac1fce
&heh1 is 0xbfac1fc8
&heh2 is 0xbfac1fb0
or
&cool == X+88
&cool3 == X+48
&cool2 == X+32
&cool4 == X+30
&heh1 == X+24
&heh2 == X
which is what shivaligupta had. That is, the memory layout is:
- heh2 (4 bytes)
- 20 unused bytes
- heh1 (4 bytes)
- 2 unused bytes
- cool4 (2 bytes)
- cool2 (0 bytes)
- 16 unused bytes
- cool3 (20 bytes)
- 20 unused bytes
- cool (8 bytes)
This is with GCC 3.3.5.
|
|
|
01-20-2007, 12:46 AM
|
#10
|
LQ Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Rep: 
|
shivaligupta -
That's definitely a dumb question. Your time would be *much* better spent reading a book on compilers, than counting byte offsets in random variable declarations.
If you insist, however: please:
a) follow varun_shrivastava's advise, and try to interpret the assembler output
b) analyze the organization of the entire (elf?) format executable
c) speculate on why data in the file is organized the way it it
PS:
I think that's the first time I've ever seen variables named after "Beavis and Butthead" expressions. Oh well...
Last edited by paulsm4; 01-20-2007 at 12:47 AM.
|
|
|
01-20-2007, 01:22 AM
|
#11
|
Member
Registered: Oct 2004
Posts: 45
Original Poster
Rep:
|
paulsm4
I hav obviously read books on compilers and thats why I asked this question. Still Cudnt find the answer so thought posting it on this forum may help but forgot that some ppl prefer reading books than trying something. Thanks for your suggetsions.
|
|
|
All times are GMT -5. The time now is 10:25 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|