 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Dhruv Guest
|
Posted: Thu Jun 26, 2003 2:08 pm Post subject: Re: array parameter sizeof |
|
|
On Wed, 25 Jun 2003 13:17:27 -0400, Shmulik Flint wrote:
| Quote: | 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
void bar (int (&f)[5][10])
{ cout<
template
void foo (t &a)
{
std::cout<
}
int main ()
{ int p[5][10];
bar (p);
foo (p); }
Regards,
-Dhruv.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Thu Jun 26, 2003 10:28 pm Post subject: Re: array parameter sizeof |
|
|
[email]shmulik_flint (AT) bmc (DOT) com[/email] (Shmulik Flint) wrote in message
news:<8eaa7326.0306240028.3c0202d (AT) posting (DOT) google.com>...
| Quote: | 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.
| Quote: | Why is the sizeof of the p parameter 4 and not 50?
|
Because the type of p is a pointer, and not an array.
| Quote: | 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:kanze (AT) gabi-soft (DOT) 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
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|