Re: Sizeof question

From: Chris \( Val \) (chrisval_at_bigpond.com.au)
Date: 01/10/04


Date: Sun, 11 Jan 2004 03:29:31 +1100


"Nils O. Selåsdal" <noselasd@frisurf.no> wrote in message
news:slrnbvgohf.erf.noselasd@asgaard.fiane.intra...
| In article <bt9akd$4mtsb$1@ID-110726.news.uni-berlin.de>, Chris ( Val ) wrote:
| >
| > "Nils O. Selåsdal" <noselasd@frisurf.no> wrote in message
| > news:slrnbvddf5.1ev.noselasd@asgaard.fiane.intra...
| > | In article <2amJb.19773$nK2.17361@nwrddc01.gnilink.net>, Dan Moos wrote:
| > | > Simple C++ question
| > | >
| > | > int *array[3];
| > | > int x = sizeof(array);
| > | >
| > | >
| > | > what is assigned to "x"? Is it the size of 3 ints in bytes? Is it the size
| > | > of the pointer to the first element in the array?
| > | > Is it the size of just the first element (the size in bytes of an int)?
| > |
| > | sizeof returns the number of chars that array occupies.
| > | Array is a an array holding 3 pointers to int. On my system
| > | a pointer is 4 chars , so sizeof array =3*4 =12
| >
| > <Nitpick>
| > The 'sizeof' operator, actually returns the
| > number(as std::size_t) of *bytes*.
| No it doesn't(atleast not in C, perhaps in C++ ?). sizeof returns the
| multiple of sizeof(char) and sizeof(char) == 1.
| It doesn't say that a char should have 8 bits(1 byte)
| On the most common system, a char is 1 byte though.

Yes, it does.

In my 'C' draft copy, it states:

6.5.3.4 The sizeof operator

Constraints

[#1] The sizeof operator shall not be applied to an
expression that has function type or an incomplete type, to
the parenthesized name of such a type, or to an lvalue that
designates a bit-field object.
Semantics

[#2] The sizeof operator yields the size (in bytes) of its
operand, which may be an expression or the parenthesized
name of a type. The size is determined from the type of the
operand. The result is an integer. If the type of the
operand is a variable length array type, the operand is
evaluated; otherwise, the operand is not evaluated and the
result is an integer constant.

[#3] When applied to an operand that has type char, unsigned
char, or signed char, (or a qualified version thereof) the
result is 1. When applied to an operand that has array
type, the result is the total number of bytes in the
array.73) When applied to an operand that has structure or
union type, the result is the total number of bytes in such
an object, including internal and trailing padding.
[#4] The value of the result is implementation-defined, and
its type (an unsigned integer type) is size_t, defined in
the <stddef.h> header.

Cheers.
Chris Val



Relevant Pages

  • Re: Null terminated strings: bad or good?
    ... The sizeof operator yields the size of its operand, ... If the type of the operand is a variable length array type, ... As far as I can tell, the standard doesn't explicitly forbid naming types larger than SIZE_MAX bytes, or even applying sizeof to such types -- it just describes the semantics of sizeof in a way that is logically impossible for such types. ... What this means, in effect, is that it's actually the size of its operand that is implementation-defined; the fact that the value is implementation-defined does not give the implementation the option of having sizeof yield any value other than "the size of it's operand". ...
    (comp.std.c)
  • Re: A couple of things from H&S
    ... expression of array type decays to a pointer to the array's first ... element *unless* it's in one of three contexts: the operand of sizeof, ... is quite obvious, p is the pointer. ...
    (comp.lang.c)
  • Re: Newbie: learning to use malloc().
    ... of 80 char. ... A pointer is just a value that refers to the location in memory of an object ... sizeof char * is 4 ... When you use a fixed size array with fgets, ...
    (comp.lang.c)
  • Re: Wording glitch: sizeof array vs. sizeof (array)
    ... A parenthesized expression is a primary expression. ... Except when it is the operand of the sizeof operator or the unary ... & operator, or is a string literal used to initialize an array, an ...
    (comp.std.c)
  • Re: sizeof()
    ... I have a question about sizeof() function. ... I have a structure which contains char* as a member. ... Its operand _can_ be surrounded by parentheses regardless of whether or not the operand is a type. ... For anything other than a VLA, the compiler knows at translation-time what the size is for a particular type of object, so in these very common cases, a 'sizeof' expression is an integer constant value, determinable at translation-time. ...
    (comp.lang.c)