Re: tryick use of sizeof again[Explaination Wanted]
Harald van D k <true...@gmail. comwrote:
6.5.3.4p2: "... If the type of the operand is a variable
length array type, the operand is evaluated; otherwise,
the operand is not evaluated and the result is an
integer constant."
Since you don't apply sizeof to an operand of variable
length array type, I don't see how or why f should be
called.
I believe the evaluation clause in 6.5.3.4p2 is more for
cases like...
sizeof (int [nonconstexpr])
Since pointer arithmetic is required to work on VLAs,
such pointers (including those created by using the name
of a VLA) necessarily carry information about the VLAs
size, either directly or indirectly.
Consider...
#include <stdio.h>
#include <stdlib.h>
int foo()
{
return 1 + rand() % 10;
}
size_t bah(char (*vlap)[*])
{
return sizeof *vlap;
}
int main()
{
int vla[foo()];
printf("%zu\n", bah(&vla));
return 0;
}
The function foo() is only called once. A compiler that
calls foo() twice would be broken.
--
Peter
Harald van D k <true...@gmail. comwrote:
Ian Collins wrote:
>
This is the behaviour mandated by the standard and
implemented by the compiler I'm using. If you believe
the standard would have been better if sizeof on VLAs
were specified differently, then I don't necessarily
disagree, but if you believe the standard requires (or
allows) any different output, then could you please
explain why?
Harald van D k wrote:
Should it?
Keith Thompson wrote:
Can anyone think of a case where the evaluation of the
argument of sizeof (when it's a VLA) has a visible
effect on the behavior of the program?
>
#include <stdio.h>
int f(void) {
puts("f is called!");
return 0;
}
int main(void) {
enum { constant_size = 1 };
const int nonconstant_siz e = 1;
>
char nonvla[constant_size];
char vla[nonconstant_siz e];
>
printf("sizeof (&nonvla)[f()] = %zu\n",
sizeof (&nonvla)[f()]);
printf("sizeof (&vla)[f()] = %zu\n",
sizeof (&vla)[f()]);
>
return 0;
}
>
This should print:
>
sizeof (&nonvla)[f()] = 1
f is called!
sizeof (&vla)[f()] = 1
Can anyone think of a case where the evaluation of the
argument of sizeof (when it's a VLA) has a visible
effect on the behavior of the program?
>
#include <stdio.h>
int f(void) {
puts("f is called!");
return 0;
}
int main(void) {
enum { constant_size = 1 };
const int nonconstant_siz e = 1;
>
char nonvla[constant_size];
char vla[nonconstant_siz e];
>
printf("sizeof (&nonvla)[f()] = %zu\n",
sizeof (&nonvla)[f()]);
printf("sizeof (&vla)[f()] = %zu\n",
sizeof (&vla)[f()]);
>
return 0;
}
>
This should print:
>
sizeof (&nonvla)[f()] = 1
f is called!
sizeof (&vla)[f()] = 1
This is the behaviour mandated by the standard and
implemented by the compiler I'm using. If you believe
the standard would have been better if sizeof on VLAs
were specified differently, then I don't necessarily
disagree, but if you believe the standard requires (or
allows) any different output, then could you please
explain why?
length array type, the operand is evaluated; otherwise,
the operand is not evaluated and the result is an
integer constant."
Since you don't apply sizeof to an operand of variable
length array type, I don't see how or why f should be
called.
I believe the evaluation clause in 6.5.3.4p2 is more for
cases like...
sizeof (int [nonconstexpr])
Since pointer arithmetic is required to work on VLAs,
such pointers (including those created by using the name
of a VLA) necessarily carry information about the VLAs
size, either directly or indirectly.
Consider...
#include <stdio.h>
#include <stdlib.h>
int foo()
{
return 1 + rand() % 10;
}
size_t bah(char (*vlap)[*])
{
return sizeof *vlap;
}
int main()
{
int vla[foo()];
printf("%zu\n", bah(&vla));
return 0;
}
The function foo() is only called once. A compiler that
calls foo() twice would be broken.
--
Peter
Comment