Help!

Size 8 bit, 16 bit, 32 bit and 64 bit systems.

 
  

Post new topic   General Reply to Topic (not reply to a specific post)    Forums Home -> System Development RSS
Next:  RFC3971  
Author Message
adar
External


Since: Aug 01, 2007
Posts: 2



PostPosted: Wed Aug 01, 2007 3:34 pm    Post subject: Size 8 bit, 16 bit, 32 bit and 64 bit systems.
Archived from groups: comp>os>linux>development>system (more info?)

I need to find out what is the size of following data structures in 8
bit, 16 bit, 32 bit, and 64 bit systems.

struct findsize{

unsigned char chA;
unsigned long lg;
unsigned short sh;
unsigned char chB;
};


sizeof() runs at compile time, that means one can know in advance what
size of these. I don't have 8 bit or 16 bit systems. I do have 32 bit
and 64 bit system.

I believe, in 32 bit system, this would be 1+4+2+1 = 8 bytes.

Can anyone throw some light here on how to find size?
Back to top
Måns_Rullgård
External


Since: Jul 29, 2004
Posts: 952



PostPosted: Thu Aug 02, 2007 12:46 am    Post subject: Re: Size 8 bit, 16 bit, 32 bit and 64 bit systems. [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

adar <adar_ji RemoveThis @yahoo.com> writes:

> I need to find out what is the size of following data structures in 8
> bit, 16 bit, 32 bit, and 64 bit systems.
>
> struct findsize{
>
> unsigned char chA;
> unsigned long lg;
> unsigned short sh;
> unsigned char chB;
> };
>
> sizeof() runs at compile time, that means one can know in advance what
> size of these. I don't have 8 bit or 16 bit systems. I do have 32 bit
> and 64 bit system.
>
> I believe, in 32 bit system, this would be 1+4+2+1 = 8 bytes.

You believe wrongly. On a typical 32-bit system, that struct will be
12 bytes because of padding after chA and chB needed to make lg
properly aligned.

If the exact size of a struct is so important, your program is
probably badly designed. Think again why you need to know the size,
and see if you can't write the code in a way that doesn't depend on
the exact values.

--
Måns Rullgård
mans RemoveThis @mansr.com
Back to top
Floyd L. Davidson
External


Since: Aug 24, 2006
Posts: 393



PostPosted: Thu Aug 02, 2007 12:46 am    Post subject: Re: Size 8 bit, 16 bit, 32 bit and 64 bit systems. [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Måns Rullgård <mans.TakeThisOut@mansr.com> wrote:
>adar <adar_ji.TakeThisOut@yahoo.com> writes:
>
>> I need to find out what is the size of following data structures in 8
>> bit, 16 bit, 32 bit, and 64 bit systems.
>>
>> struct findsize{
>>
>> unsigned char chA;
>> unsigned long lg;
>> unsigned short sh;
>> unsigned char chB;
>> };
>>
>> sizeof() runs at compile time, that means one can know in advance what
>> size of these. I don't have 8 bit or 16 bit systems. I do have 32 bit
>> and 64 bit system.
>>
>> I believe, in 32 bit system, this would be 1+4+2+1 = 8 bytes.
>
>You believe wrongly. On a typical 32-bit system, that struct will be
>12 bytes because of padding after chA and chB needed to make lg
>properly aligned.
>
>If the exact size of a struct is so important, your program is
>probably badly designed. Think again why you need to know the size,
>and see if you can't write the code in a way that doesn't depend on
>the exact values.

It should be noted that the structure size is as much *compiler*
dependant as it is platform dependant. Different compilers on the
same platform or even the same compiler with different options might
well pad the structure differently.

Here is a demo program the OP might enjoy,

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

int main(void)
{
struct findsize {
unsigned char chA;
unsigned long lg;
unsigned short sh;
unsigned char chB;
};

size_t begin, end;

printf("sizeof struct: %2ld bytes\n", (long) sizeof (struct findsize));

begin = offsetof(struct findsize, chA);
end = offsetof(struct findsize, lg);
printf("Member "chA": offset -- %2ld bytes; size -- %2ld\n",
(long) begin, (long) end - begin);

begin = offsetof(struct findsize, lg);
end = offsetof(struct findsize, sh);
printf("Member "lg": offset -- %2ld bytes; size -- %2ld\n",
(long) begin, (long) end - begin);

begin = offsetof(struct findsize, sh);
end = offsetof(struct findsize, chB);
printf("Member "sh": offset -- %2ld bytes; size -- %2ld\n",
(long) begin, (long) end - begin);

begin = offsetof(struct findsize, chB);
end = sizeof (struct findsize);

printf("Member "chB": offset -- %2ld bytes; size -- %2ld\n",
(long) begin, (long) end - begin);

return EXIT_SUCCESS;
}



--
Floyd L. Davidson <http://www.apaflo.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska) floyd.TakeThisOut@apaflo.com
Back to top
Floyd L. Davidson
External


Since: Aug 24, 2006
Posts: 393



PostPosted: Thu Aug 02, 2007 1:47 am    Post subject: Re: Size 8 bit, 16 bit, 32 bit and 64 bit systems. [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Måns Rullgård <mans.RemoveThis@mansr.com> wrote:
>Josef Moellers <josef.moellers.RemoveThis@fujitsu-siemens.com> writes:
>
>> <nitpicking>
>> With all due respect, it's not the size you're printing, the size of a
>> char is usually 1 and can be obtained simply by calling
>> sizeof(char). It's the space allocated for that item you're printing
>
>If we're nitpicking, I'd like to point out that sizeof(char) is
>*always* 1 by definition. The CHAR_BITS macro gives the number of
>bits in a char, and POSIX mandates this be 8.

Absolutely true. (It's always good for a laugh when
someone has "sizeof(char)" in their code...)

What I was printing out was most definitely *not* the
size of any particular C type, and no such claim was
made either. What was printed out was the size of the
memory allocated in that particular struct for each
particular member. Note that there were two different
members that were of type char, and they were allocated
*different* sized chunks of memory in the struct.

--
Floyd L. Davidson <http://www.apaflo.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska) floyd.RemoveThis@apaflo.com
Back to top
Josef Moellers
External


Since: Jul 07, 2005
Posts: 389



PostPosted: Thu Aug 02, 2007 9:15 am    Post subject: Re: Size 8 bit, 16 bit, 32 bit and 64 bit systems. [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Floyd L. Davidson wrote:
> Måns Rullgård <mans.RemoveThis@mansr.com> wrote:
>
>>adar <adar_ji.RemoveThis@yahoo.com> writes:
>>
>>
>>>I need to find out what is the size of following data structures in 8
>>>bit, 16 bit, 32 bit, and 64 bit systems.
>>>
>>>struct findsize{
>>>
>>> unsigned char chA;
>>> unsigned long lg;
>>> unsigned short sh;
>>> unsigned char chB;
>>>};
>>>
>>>sizeof() runs at compile time, that means one can know in advance what
>>>size of these. I don't have 8 bit or 16 bit systems. I do have 32 bit
>>>and 64 bit system.
>>>
>>>I believe, in 32 bit system, this would be 1+4+2+1 = 8 bytes.
>>
>>You believe wrongly. On a typical 32-bit system, that struct will be
>>12 bytes because of padding after chA and chB needed to make lg
>>properly aligned.
>>
>>If the exact size of a struct is so important, your program is
>>probably badly designed. Think again why you need to know the size,
>>and see if you can't write the code in a way that doesn't depend on
>>the exact values.
>
>
> It should be noted that the structure size is as much *compiler*
> dependant as it is platform dependant. Different compilers on the
> same platform or even the same compiler with different options might
> well pad the structure differently.
>
> Here is a demo program the OP might enjoy,
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <stddef.h>
>
> int main(void)
> {
> struct findsize {
> unsigned char chA;
> unsigned long lg;
> unsigned short sh;
> unsigned char chB;
> };
>
> size_t begin, end;
>
> printf("sizeof struct: %2ld bytes\n", (long) sizeof (struct findsize));
>
> begin = offsetof(struct findsize, chA);
> end = offsetof(struct findsize, lg);
> printf("Member "chA": offset -- %2ld bytes; size -- %2ld\n",
> (long) begin, (long) end - begin);

<nitpicking>
With all due respect, it's not the size you're printing, the size of a
char is usually 1 and can be obtained simply by calling sizeof(char).
It's the space allocated for that item you're printing which consists of
the space occupied by the item itself plus any padding required to place
the next member at an appropriate offset.
</nitpicking>

--
These are my personal views and not those of Fujitsu Siemens Computers!
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://www.fujitsu-siemens.com/imprint.html
Back to top
Måns_Rullgård
External


Since: Jul 29, 2004
Posts: 952



PostPosted: Thu Aug 02, 2007 9:15 am    Post subject: Re: Size 8 bit, 16 bit, 32 bit and 64 bit systems. [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Josef Moellers <josef.moellers.TakeThisOut@fujitsu-siemens.com> writes:

> <nitpicking>
> With all due respect, it's not the size you're printing, the size of a
> char is usually 1 and can be obtained simply by calling
> sizeof(char). It's the space allocated for that item you're printing

If we're nitpicking, I'd like to point out that sizeof(char) is
*always* 1 by definition. The CHAR_BITS macro gives the number of
bits in a char, and POSIX mandates this be 8.

--
Måns Rullgård
mans.TakeThisOut@mansr.com
Back to top
Display posts from previous:   
Post new topic   General Reply to Topic (not reply to a specific post)    Forums Home -> System Development All times are: Eastern Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum