Article View: comp.std.c++
Article #2526Re: sizeof() on an array argument
From: Fergus Henderson
Date: Fri, 10 Nov 1995 19:27
Date: Fri, 10 Nov 1995 19:27
52 lines
2320 bytes
2320 bytes
vosse@ruls41.fsw.LeidenUniv.nl (Theo Vosse) writes:
>[...] If I have a function
>
>void Add(DataPoint* dp, char v[N])
>{
> ... sizeof(v) ...
>}
>
>Should sizeof(v) be
>
>a) 4
>b) N
>c) Another value?
`sizeof(v)' should be equal to `sizeof(char *)' (i.e. 4 on typical 32-bit
implementations).
The reason is that the type of `v' is converted from `char [N]' to
`char *' (see 8.3.5/3 in the April draft working paper).
You cannot pass arrays by value in C or C++ (at least not
unless you wrap them up in a struct or class).
>However, both ARM and some ANSI proposal on the net (the September working
>paper?) state:
>
>* When applied to an array, the result is the total number of bytes in the
>array.
That is not relevant in this example, since despite the declaration,
`v' is not an array.
>The ANSI paper also explicitly adds:
>
>* [...] array-to-pointer [...] standard conversions are suppressed on the
>operand of sizeof.
Again, this does not apply - `v' is not an array, so of course the
array-to-pointer standard conversion cannot be applied. The change
of the type of `v' from array to pointer (since the declaration of
`v' is in a function parameter) is not an instance of the
array-to-pointer standard conversion described in 4.2 which applies to
values. It is a special "adjustment" that is specified in 8.3.5/3,
not a conversion in the usual C++ sense of the word.
--
Fergus Henderson WWW: http://www.cs.mu.oz.au/~fjh
fjh@cs.mu.oz.au PGP: finger fjh@128.250.37.3
---
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID:
<9531505.20142@mulga.cs.mu.OZ.AU>Path:
arc1-ca.pugleaf.net!in1.uu.net!bounce-backReferences:
<vosse-1011951040220001@fslq01.fsw.leidenuniv.nl>Show Full Headers
Complete Article Headers
Approved: fjh@cs.mu.oz.au Date: 10 Nov 95 19:27:13 GMT From: fjh@munta.cs.mu.OZ.AU (Fergus Henderson) Message-ID: <9531505.20142@mulga.cs.mu.OZ.AU> Newsgroups: comp.std.c++ Organization: Computer Science, University of Melbourne, Australia Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back References: <vosse-1011951040220001@fslq01.fsw.leidenuniv.nl> Subject: Re: sizeof() on an array argument X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUBMKOoFOEDnX0m9pzZAQGbEwF/WFiHgfu0FRTwHo9PedQ0wEhDrIYjNOe1 IadbAbDC4EIvrvtQ8XwDq/Ln39wUVcIf =LWCl