Matthew Hannigan wrote:

On Fri, Sep 30, 2005 at 10:09:40AM +1000, O Plameras wrote:
.... and as C is closely bound
to hardware architecture you must have said something about these data

Actually, C is not necessarily that closely bound to hardware architecture.

The the following C program illustrates the relationship of C and hardware architecture on 16-bit and 32-bit (Two different architectures). We cannot use 32-bit and 64-bit (AMD) to illustrate because the int size are the same in both. This is an aberration for AMD CPUs, I
think.

Many documentations says that a 64-bit should really have int size=8 instead of 4

(see http://www.osdata.com/topic/language/asm/datarep.htm
-DEC VAX *16* *bit* [2 *byte*] *word*; 32 bit [4 *byte*] longword; 64 bit [8 *byte*]quadword;
132 bit [16 *byte*] octaword; data may be unaligned at a speed penalty *...*
)

There are other documentations similar to the above assertions on the net.

But check the C-codes.

#include <stdio.h>
struct verify {
  char initials[2];
  int birthdate;
};
int main(void)
{
  struct verify holes;
  printf ("%d\n", sizeof(holes.initials[0]));
  printf ("%d\n", sizeof(holes.initials));
  printf ("%d\n", sizeof(holes.birthdate));
  printf ("%d\n", sizeof(holes));
  return 0;
}
Given that the word-byte
In 16-bit computer = 2 bytes word the output is,
1
2
2
4
in  32-bit computer = 4 bytes word the output is,
1
2
4
8

Two things are different due to computer architecture.
1. The same struct have different memory sizes allocated.
2. The struct has no hole in 16-bit and has 2-byte hole in 32-bit because C-compiler always
   align int data types beginning at the next word byte.

The following quote is from wikipedia
(http://en.wikipedia.org/wiki/C_variable_types_and_declarations#Size)

        "There is some confusion in novice C programmers as to how
        big these types are. The standard is specifically vague
        in this area:

            * A short int must not be larger than an int.
            * An int must not be larger than a long int.
            * A short int must be at least 16 bits long.
            * A long int must be at least 32 bits long.

        The standard does not require that any of these sizes are
        necessarily different. It is perfectly valid, for example,
        that all 3 types be 64 bits long."


--
Matt


--
O Plameras
http://www.acay.com.au/~oscarp/tutor

--
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html

Reply via email to