Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

array parameter sizeof

3 views
Skip to first unread message

Shmulik Flint

unread,
Jun 25, 2003, 1:17:27 PM6/25/03
to
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

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Dag Henriksson

unread,
Jun 25, 2003, 7:32:31 PM6/25/03
to
"Shmulik Flint" <shmuli...@bmc.com> wrote in message
news:8eaa7326.0306...@posting.google.com...

> 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

Jack Klein

unread,
Jun 25, 2003, 7:32:59 PM6/25/03
to
On 25 Jun 2003 13:17:27 -0400, shmuli...@bmc.com (Shmulik Flint)
wrote in comp.lang.c++.moderated:

> 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

Andrey Tarasevich

unread,
Jun 25, 2003, 7:33:21 PM6/25/03
to
Shmulik Flint wrote:
> ...

> 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?

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

Ron Natalie

unread,
Jun 25, 2003, 7:34:04 PM6/25/03
to

"Shmulik Flint" <shmuli...@bmc.com> wrote in message
news:8eaa7326.0306...@posting.google.com...
\

>
> 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?
>
Yes. This is yet another unfortunate relic of the fact that arrays are
goofy types. Note that the array argument in f is not passed by value.
An array declaration as a function parameter isn't really an array
declaration
at all, but a pointer declaration.

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.

Ben Hutchings

unread,
Jun 25, 2003, 7:35:09 PM6/25/03
to
In article <8eaa7326.0306...@posting.google.com>,

Shmulik Flint wrote:
> Hi,
> Consider the following code:
> void f(char p[5][10])
> {
> cout << sizeof(p) << endl;
> char a[5][10];
> cout << sizeof(a) << endl;
> }
<snip>

> on MSVC++, this prints:
> 4
> 50
>
> 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?

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.)

Dhruv

unread,
Jun 26, 2003, 10:08:44 AM6/26/03
to
On Wed, 25 Jun 2003 13:17:27 -0400, Shmulik Flint wrote:

> 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.

ka...@gabi-soft.fr

unread,
Jun 26, 2003, 6:28:08 PM6/26/03
to
shmuli...@bmc.com (Shmulik Flint) wrote in message
news:<8eaa7326.0306...@posting.google.com>...

> 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

0 new messages