Re: Sizeof question
From: Chris \( Val \) (chrisval_at_bigpond.com.au)
Date: 01/10/04
- Next message: Nils O. Selåsdal: "Re: program that compiles in C but not in C++?"
- Previous message: Chris \( Val \): "Re: [C++] Casting void* pointers within different scope"
- Maybe in reply to: Dan Moos: "Sizeof question"
- Next in thread: Richard Heathfield: "Re: Sizeof question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Nils O. Selåsdal: "Re: program that compiles in C but not in C++?"
- Previous message: Chris \( Val \): "Re: [C++] Casting void* pointers within different scope"
- Maybe in reply to: Dan Moos: "Sizeof question"
- Next in thread: Richard Heathfield: "Re: Sizeof question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|