This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: sizeof(array) with variable-length array parametery


On Wed, Apr 09, 2008 at 06:34:05PM +0100, Andrew Haley wrote:
> 
> But it's *not* always easily accessible.  In this case,
> 
> int bar(int p[n])

  Well, n must be referring a symbol that is in scope, so
it must be bound to a value at run-time. Otherwise, if its undefined,
the sizeof should also be undefined...

> 
> n is somewhere in global scope, maybe in another translation unit,
> and as I point out below you'd have to copy it at the time the array
> was created.

  What's wrong with taking a snapshot of "n" when the function is
entered? After all, that's what p[n] specifies in the signature?

> 
> So, you're saying it *should* create a hidden copy of the size
> parameter for use by sizeof?  While this would work, it's not very
> C.

  Hhm, not strictly following C99 standard, sure, but following common
sense, yes. That's what seems to be happening in this valid C99
snippet:

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

  I do understand that our idle rumblings will not change the C99
standard, but, frankly speaking, its very disappointing having to
write things such as:

void foo(int s,int a[s])
{
	/* work-around the C99 always returning
	 * sizeof(int*) instead of s*sizeof(int)
	 */
	int _a[s];
	s=11;
	for (int i=0; i<sizeof _a/sizeof _a[0]; i++);
}

  This one also generates slightly less optimal code...

Maybe it's a good canditate for a GCC extension?

> 
> Andrew.
> 

Pjotr


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]