[comp.std.c] sizeof on a word-oriented machine

diamond@csl.sony.co.jp (Norman Diamond) (11/16/89)

Consider a machine where each 4-byte word has an address.

  char x[37];
  int  q;
  q = sizeof x / sizeof (char);

What is sizeof x?

If sizeof x is 40 (since 40 bytes are reserved for x) then the example
on page 46 lines 12 to 13 (section 3.3.3.4) is violated.

If sizeof x is 37 then a user might do:
  char *two_xs;
  two_xs = malloc (2 * sizeof x);
and get screwed because only 76 bytes will be allocated (2 * 37 rounded
up to a multiple of 4) when 80 are really needed.

I think 40 is the most reasonable value for sizeof x.  Is the standard's
example wrong?  May it be ignored?

-- 
Norman Diamond, Sony Corp. (diamond%ws.sony.junet@uunet.uu.net seems to work)
  Should the preceding opinions be caught or     |  James Bond asked his
  killed, the sender will disavow all knowledge  |  ATT rep for a source
  of their activities or whereabouts.            |  licence to "kill".

gwyn@smoke.BRL.MIL (Doug Gwyn) (11/17/89)

In article <11135@riks.csl.sony.co.jp> diamond@ws.sony.junet (Norman Diamond) writes:
-Consider a machine where each 4-byte word has an address.
-  char x[37];
-What is sizeof x?

37.

-If sizeof x is 37 then a user might do:
-  char *two_xs;
-  two_xs = malloc (2 * sizeof x);
-and get screwed because only 76 bytes will be allocated (2 * 37 rounded
-up to a multiple of 4) when 80 are really needed.

No, only 2*37 bytes are needed.  Arrays are not padded.

-I think 40 is the most reasonable value for sizeof x.  Is the standard's
-example wrong?  May it be ignored?

The example is right.  Feel free to ignore it.

henry@utzoo.uucp (Henry Spencer) (11/18/89)

In article <11135@riks.csl.sony.co.jp> diamond@ws.sony.junet (Norman Diamond) writes:
>I think 40 is the most reasonable value for sizeof x.  Is the standard's
>example wrong?  May it be ignored?

As mentioned in 1.4, the examples are technically not part of the standard
and hence should be ignored in the event of conflict.  The question of the
exact correct value of sizeof here looks a bit more involved.
-- 
A bit of tolerance is worth a  |     Henry Spencer at U of Toronto Zoology
megabyte of flaming.           | uunet!attcan!utzoo!henry henry@zoo.toronto.edu

karl@haddock.ima.isc.com (Karl Heuer) (11/18/89)

In article <11135@riks.csl.sony.co.jp> diamond@ws.sony.junet (Norman Diamond) writes:
>Consider a machine where each 4-byte word has an address.
>  char x[37];
>What is sizeof x?

37.  Always.

>If sizeof x is 37 then a user might do:
>  char *two_xs;
>  two_xs = malloc (2 * sizeof x);
>and get screwed because only 76 bytes will be allocated (2 * 37 rounded
>up to a multiple of 4) when 80 are really needed.

Why do you think that 80 are needed?  The second of the two x-sized objects
pointed to by the return value of malloc() will not be word-aligned, but that
should be no more surprising than the equivalent fact about the second
char-sized object obtained from malloc(2*sizeof(char)).

>Is the standard's example wrong?  May it be ignored?

No.

Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint

walter@hpclwjm.HP.COM (Walter Murray) (11/18/89)

Norman Diamond writes:

> Consider a machine where each 4-byte word has an address.
>   char x[37];
>   int  q;
>   q = sizeof x / sizeof (char);

O.K., but you still have to have a way of generating a pointer to
any particular element of an array.  And you are not allowed
to have &x[1]==&x[0].

> What is sizeof x?

37.

> If sizeof x is 40 (since 40 bytes are reserved for x) then the example
> on page 46 lines 12 to 13 (section 3.3.3.4) is violated.

You're right.

> If sizeof x is 37 then a user might do:
>   char *two_xs;
>   two_xs = malloc (2 * sizeof x);
> and get screwed because only 76 bytes will be allocated (2 * 37 rounded
> up to a multiple of 4) when 80 are really needed.

This could cause a problem only if the implementation isn't conforming
or the user makes assumptions not guaranteed by the dpANS.

> I think 40 is the most reasonable value for sizeof x.  Is the standard's
> example wrong?  May it be ignored?

I think the example is not wrong.  Consider the following.

   1.  The elements of an array must be allocated contiguously.
   2.  The sizeof operator yields the size (in bytes) of its operand.
   3.  sizeof(char)==1
   4.  Applied to an array, sizeof yields the total number of bytes
       in the array.

It seems to me the only conclusion is that the dpANS guarantees that
sizeof(x)==37.  Any unused bytes following the last element of an 
array are not counted in its size.

Walter Murray
--- 

davidsen@crdos1.crd.ge.COM (Wm E Davidsen Jr) (11/18/89)

In article <11135@riks.csl.sony.co.jp> diamond@ws.sony.junet (Norman Diamond) writes:

| If sizeof x is 37 then a user might do:
|   char *two_xs;
|   two_xs = malloc (2 * sizeof x);
| and get screwed because only 76 bytes will be allocated (2 * 37 rounded
| up to a multiple of 4) when 80 are really needed.

  This is the problem: 80 aren't needed, 2*37 are needed. And the sizeof
the first array will always be 37, because that's how long you declared
it to be. Arrays are not ever padded. Struct and unions may be.

  The sizeof an array of N elements will be N times the size of a single
element, no more or less.
-- 
bill davidsen	(davidsen@crdos1.crd.GE.COM -or- uunet!crdgw1!crdos1!davidsen)
"The world is filled with fools. They blindly follow their so-called
'reason' in the face of the church and common sense. Any fool can see
that the world is flat!" - anon

gwyn@smoke.BRL.MIL (Doug Gwyn) (11/19/89)

In article <12570032@hpclwjm.HP.COM> walter@hpclwjm.HP.COM (Walter Murray) writes:
>It seems to me the only conclusion is that the dpANS guarantees that
>sizeof(x)==37.

Yes, and I know for a fact from discussion with several "principal members"
of X3J11 that it was intended that the standard guarantee a macro like the
following to work for all conforming implementations:

	#define	Elements( a )	(sizeof a / sizeof a[0])