int main()
{
char s[5][10];
f(s);
return 0;
}
on MSVC++, this prints:
4
50
Is this valid?
Why is the sizeof of the p parameter 4 and not 50? Can you give
pointer to places in the standard where this is discussed?
Shmulik
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
> Is this valid?
Yes.
> Why is the sizeof of the p parameter 4 and not 50? Can you give
> pointer to places in the standard where this is discussed?
See 8.3.5p3
"After determining the type of each parameter, any parameter of type
"array of T" or "function returning T" is adjusted to be "pointer to T"
or "pointer to function returning T," respectively."
--
Dag Henriksson
> Hi,
> Consider the following code:
> void f(char p[5][10])
> {
> cout << sizeof(p) << endl;
> char a[5][10];
> cout << sizeof(a) << endl;
> }
>
> int main()
> {
> char s[5][10];
> f(s);
>
> return 0;
> }
>
> on MSVC++, this prints:
> 4
> 50
>
> Is this valid?
> Why is the sizeof of the p parameter 4 and not 50? Can you give
> pointer to places in the standard where this is discussed?
>
> Shmulik
Arrays are not first-class objects in C or C++. You can never pass or
return a naked array to or from a function. In almost all
expressions, the name of an array is converted to a pointer to its
first element. This has always been the case in both C and C++.
The first pair of [] after a name in a function parameter is
syntactical sugar, the actual parameter that f() receives is pointer
to (one or more) array(s) of 10 characters.
But in any case, the parameter p in f() is a pointer, and sizeof
applied to a pointer evaluates to the size of the pointer itself, not
the size of the pointed-to type.
As for where this is covered in the C++ standard, see 4.2
"Array-to-pointer conversion".
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Yes.
> Why is the sizeof of the p parameter 4 and not 50?
In C++ array declaration in parameter list always decays to pointer
declaration. When used as parameter declaration, 'char p[5][10]' is
equivalent to 'char p[][10]' and is equivalent to 'char (*p)[10]' - a
pointer to an array of 10 'char's.
On your platform pointer size is 4. That explains the result.
> Can you give
> pointer to places in the standard where this is discussed?
8.3.5/3
--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP
It is alluded to in 8.3.5 of the C++ standard, but a stronger statement
is
made in the C standard (6.5.4.3 of C90, or 6.5.7.3 of c99). Parameters
of T[] are converted to T*. Size of T* on your machine is obviously 4.
Yes.
> Why is the sizeof of the p parameter 4 and not 50? Can you give
> pointer to places in the standard where this is discussed?
Function parameters never have array type; an array passed to a
function is converted into a pointer to its first element.
The signature of f is really:
void f(char (*)[10])
and its parameter is a pointer.
(In very early versions of C, there was no array type, and declaring
an array just declared and initialised a pointer. The implicit
conversion of arrays to pointers remains in C and C++ despite the
introduction long ago of a real (though limited) array type.)
> Hi,
> Consider the following code:
> void f(char p[5][10])
> {
> cout << sizeof(p) << endl;
> char a[5][10];
> cout << sizeof(a) << endl;
> }
>
> int main()
> {
> char s[5][10];
> f(s);
>
> return 0;
> }
>
However this will give you the correct results:
#include <iostream>
void bar (int (&f)[5][10])
{ cout<<sizeof(f)<<std::endl; }
template <class t>
void foo (t &a)
{
std::cout<<sizeof(a)<<std::endl;
}
int main ()
{ int p[5][10];
bar (p);
foo (p); }
Regards,
-Dhruv.
> Consider the following code:
> void f(char p[5][10])
> {
> cout << sizeof(p) << endl;
> char a[5][10];
> cout << sizeof(a) << endl;
> }
> int main()
> {
> char s[5][10];
> f(s);
> return 0;
> }
> on MSVC++, this prints:
> 4
> 50
> Is this valid?
Logically, no, but this is C++, and it is more or less what the standard
requires.
> Why is the sizeof of the p parameter 4 and not 50?
Because the type of p is a pointer, and not an array.
> Can you give pointer to places in the standard where this is
> discussed?
§8.3.5/3: "[...] After determing the type of each parameter, any
parameter of type 'array of T' or 'function returning T' is adjusted to
be 'pointer to T' or 'pointer to function returning T', respectively."
This is a well know design error in C++, inherited from C (which in turn
inherited it from B, where there was a reason for it). In practice, any
attempt to change it would break so much code as to make it impossible.
The solution adopted in C++ is to prefer std::vector whenever possible.
--
James Kanze GABI Software mailto:ka...@gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, Tél. : +33 (0)1 30 23 45 16