sizeof(array) with variable-length array parameter

peter.kourzanov@xs4all.nl peter.kourzanov@xs4all.nl
Wed Apr 9 17:23:00 GMT 2008


On Wed, Apr 09, 2008 at 04:51:14PM +0100, Andrew Haley wrote:
> 
> Well, OK, you may either do
> 
> int foo(int n, int p[])
> 
> or
> 
> int foo(int n, int p[])

int bar(int n, int p[n]) I presume...

  I am perfectly OK with sizeof returning sizeof(int*) for your
foo() function, but it just feels so wrong to do it when the size
is so easily accessible...

> 
> but sizeof doesn't distinguish these two cases.  In any case, how would
> you do it?  Would an assignment to n change the result of sizeof, or not?
> If not, you'd have to create a "shadow" variable.


  Sure not, it is not changed when you do (on a 32-bit platform):

int foo()
{ int s=10,a[s];
  s=11;
  assert(sizeof s==40);
}

  Try it... Why should the behaviour be so much different in the
parameter passing case?

> 
> But that would break compliant code.  Why would you want to do that?

  OK, that's the only argument I buy. 

Pjotr



More information about the Gcc-help mailing list