About space for variable

W

westbest98

Hi,everyone!
i worder how the computer allocate the space for variable.
the program like this:

printf("sizeof(int)=%d,sizeof(char)=%d",sizeof(int),sizeof(char));
int a=10,b=20;
char c[]="123",d[10]="123";
printf("\na=%x,b=%x,c=%x,d=%x",&a,&b,c,d);

then i get the result:

sizeof(int)=4,sizeof(char)=1
a=22ff6c,b=22ff68,c=22ff64,d=22ff50
i know that (a-b),there is a size of space for int,but why (c-d) there
is 20,not 10,like i do d[10]!

Thanks for your solutions!
 
R

raashid bhatt

Hi,everyone!
i worder how the computer  allocate the space for variable.
 the program like this:

printf("sizeof(int)=%d,sizeof(char)=%d",sizeof(int),sizeof(char));
        int a=10,b=20;
        char c[]="123",d[10]="123";
        printf("\na=%x,b=%x,c=%x,d=%x",&a,&b,c,d);

then i get the result:

     sizeof(int)=4,sizeof(char)=1
     a=22ff6c,b=22ff68,c=22ff64,d=22ff50
i know that (a-b),there is a size of space for int,but why (c-d) there
is 20,not  10,like i do d[10]!

Thanks for your solutions!

be more precise please
 
G

Guest

i worder how the computer  allocate the space for variable.

why? This is a serious question! Now the answer may be simply
curiosity, which is fine. And the answer is "it depends"
different compilers do it in different ways (maybe even the same
compiler does it different ways depending on flag settings).

So if you want to know for your compiler then ask on a news group
devoted to that compiler. If you want a general discussion of
the various options then try a general compiler group.

Note that it is usually a bad idea to write code that relies
on memory layout.

 the program like this:

printf("sizeof(int)=%d,sizeof(char)=%d",sizeof(int),sizeof(char));
        int a=10,b=20;

is your space key broken?
[hi richard!]
        char c[]="123",d[10]="123";
        printf("\na=%x,b=%x,c=%x,d=%x",&a,&b,c,d);

this should be
printf ("\na=%p,b=%p,c=%p,d=%p",
(void*)&a, (void*)&b, (void*)c, (void*)d);

then i get the result:

     sizeof(int)=4,sizeof(char)=1
     a=22ff6c,b=22ff68,c=22ff64,d=22ff50
i know that (a-b),there is a size of space for int,but why (c-d) there
is 20,not  10,like i do d[10]!

no idea, ask your compiler writer
 
K

Kojak

Le Wed, 18 Feb 2009 21:56:57 -0800 (PST),
(e-mail address removed) a écrit :
i worder how the computer allocate the space for variable.
the program like this:

printf("sizeof(int)=%d,sizeof(char)=%d",sizeof(int),sizeof(char));
int a=10,b=20;
char c[]="123",d[10]="123";
printf("\na=%x,b=%x,c=%x,d=%x",&a,&b,c,d);

For address (pointer) it's better to use 'p' than 'a':

printf("\na=%p, b=%p, c=%p, d=%p", &a, &b, c, d);

but no matter.
then i get the result:

sizeof(int)=4,sizeof(char)=1
a=22ff6c,b=22ff68,c=22ff64,d=22ff50
i know that (a-b),there is a size of space for int,but why (c-d) there
is 20,not 10,like i do d[10]!

AFAIK, it's mainly a memory alignement issue, depending on
your cpu architecture.
 
B

Ben Bacarisse

Probably worth pointing out (in addition to the other comments) that %d
is for integers and sizeof yields a size_t. If you have C99, then the
format %zu can be used for printing a size_t value. If not, you are
better off converting explicitly. In this case, since sizeof(char)
guaranteed to be 1 and sizeof(int) is not big, you could convert to
int:

printf("sizeof(int) = %d, sizeof(char) = %d",
(int)sizeof(int), (int)sizeof(char));
 
N

Nate Eldredge

Hi,everyone!
i worder how the computer allocate the space for variable.
the program like this:

printf("sizeof(int)=%d,sizeof(char)=%d",sizeof(int),sizeof(char));
int a=10,b=20;
char c[]="123",d[10]="123";
printf("\na=%x,b=%x,c=%x,d=%x",&a,&b,c,d);

then i get the result:

sizeof(int)=4,sizeof(char)=1
a=22ff6c,b=22ff68,c=22ff64,d=22ff50
i know that (a-b),there is a size of space for int,but why (c-d) there
is 20,not 10,like i do d[10]!

Notice that the starting address of d is aligned on a 16-byte boundary.
Many architectures require or prefer (for performance) that objects
of larger sizes be aligned when accessed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top