Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Re: static array params (http://www.velocityreviews.com/forums/t957359-re-static-array-params.html)

Keith Thompson 02-07-2013 02:30 AM

Re: static array params
 
Russell Shaw <rjshawN_o@s_pam.netspace.net.au> writes:
> On 07/02/13 11:04, Russell Shaw wrote:
>> What is the use of a static array length parameter?
>>
>> void
>> func(int b, char a[static b])
>> {
>> }
>>
>> N1570 6.7.6.3 p7 says:
>>
>> "A declaration of a parameter as ``array of type'' shall be adjusted to
>> ``qualified pointer to type'', where the type qualifiers (if any) are those
>> specified within the [ and ] of the array type derivation. If the keyword static
>> also appears within the [ and ] of the array type derivation, then for each call
>> to the function, the value of the corresponding actual argument shall provide
>> access to the first element of an array with at least as many elements as
>> specified by the size expression."
>>
>> In most cases, an array is decayed to a pointer within the function anyway. Is
>> there a place where the array length makes a difference?

>
> Just thought to look in C99RationaleV5.10.pdf. The answer is on P.84.


Which says:

It would be a significant advantage on some systems for the
translator to initiate, at the beginning of the function,
prefetches or loads of the arrays that will be referenced
through the parameters. There is no way in C89 for the user to
provide information to the translator about how many elements
are guaranteed to be available.

In C99, the use of the static keyword in:

void fadd(double a[static 10], const double b[static 10])
{
[snip]
}

guarantees that both the pointers a and b provide access to the
first element of an array containing at least ten elements. The
static keyword also guarantees that the pointer is not NULL
and points to an object of the appropriate effective type. It
does not, however, guarantee that a and b point to unique,
non-overlapping objects. The restrict keyword is used for that
purpose as in:
[...]

What that doesn't quite make clear, however, is that this "guarantee"
is one made by the programmer, not by the compiler. You *can* call

fadd(NULL, NULL);

or

fadd(foo, bar);

where foo and bar are both 2-element arrays. If you do, the compiler
is not required to complain (and gcc, for example, doesn't) --
but the code that implements fadd() is permitted to *assume* that
a and b point to the first elements of arrays that are at least 10
elements long.

By using static, you're promising the compiler that you'll provide
valid arguments. If you break that promise, your program's behavior
is undefined (it violates a "shall" outside a constraint).

(I'm a little surprised that gcc doesn't warn about this.)

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Shao Miller 02-07-2013 05:09 AM

Re: static array params
 
On 2/6/2013 22:55, Russell Shaw wrote:
>
> It's a bit like the "restrict" qualifier. Without interprocedural alias
> analysis, the programmer promises that two pointer parameters don't
> point to overlapping memory.
>
> With interprocedural alias analysis, errors in using the "static"
> qualifier could be detected.


Are you suggesting that 'static' in a function parameter array
declarator (which declares a pointer, as we know) has something to do
with aliasing? It seems like more of an array-bounds issue, to me.

#include <stdlib.h>
#include <stdio.h>

void foo(int ip1[static 10], int ip2[static 10]) {
int i;

for (i = 0; i < 10; ++i)
ip1[i] = ip2[i] = i;
}

int main(void) {
int * ip1;
size_t i;
unsigned int byte;
int * ip2;

ip1 = malloc(15 * sizeof *ip1);
if (!ip1)
return 0;

printf("ip1 hex bytes:");
for (i = 0; i < sizeof ip1; ++i)
printf(" %X", ((unsigned char *) &ip1)[i]);
printf("\n");

printf("Enter the same hex bytes, one per line: ");
for (i = 0; i < sizeof ip2; ++i) {
if (scanf("%x", &byte) != 1) {
free(ip1);
return 0;
}
((unsigned char *) &ip2)[i] = byte;
}
ip2 += 5;

/*
* Can "interprocedural alias analysis" decide that
* 'ip2' is derived from 'ip1'? Is it relevant?
*/
foo(ip1, ip2);
switch (i = 0) for (; i < 15; ++i) {
printf(", ");
default:
printf("%d", ip1[i]);
}
printf("\n");

free(ip1);
return 0;
}

--
- Shao Miller
--
"Thank you for the kind words; those are the kind of words I like to hear.

Cheerily," -- Richard Harter

Noob 02-07-2013 07:34 AM

Re: static array params
 
Shao Miller wrote:

> Are you suggesting that 'static' in a function parameter array
> declarator has something to do with aliasing?


I think you over-snipped.

He was discussing this passage of the rationale:

In C99, the use of the static keyword in [a function parameter
array declarator]
guarantees that both the pointers a and b provide access to the
first element of an array containing at least ten elements. The
static keyword also guarantees that the pointer is not NULL
and points to an object of the appropriate effective type. It
does not, however, guarantee that a and b point to unique,
non-overlapping objects. The restrict keyword is used for that
purpose [...]

He was giving "restrict" and "static in a function parameter array
declarator" as two examples of promises made by the programmer to
the compiler.

Regards.



All times are GMT. The time now is 07:50 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.