Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: static array params

Reply
Thread Tools

Re: static array params

 
 
Keith Thompson
Guest
Posts: n/a
 
      02-07-2013
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) http://www.velocityreviews.com/forums/(E-Mail Removed) <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"
 
Reply With Quote
 
 
 
 
Shao Miller
Guest
Posts: n/a
 
      02-07-2013
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
 
Reply With Quote
 
 
 
 
Noob
Guest
Posts: n/a
 
      02-07-2013
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ajax passing params array Monica Saiz Ruby 0 09-24-2008 10:38 PM
static (non-zero) initialization of an array of structs (array of length #define LEN) anon.asdf@gmail.com C Programming 23 08-28-2007 11:54 PM
params v.s. @params in rails? Barry Ruby 9 09-15-2005 03:12 AM
Convert Static Array to Dynamic Array ? raxitsheth@gmail.com C Programming 2 07-14-2005 01:25 AM
Can a static array contain a dynamic array of pointers? Peter B. Steiger C Programming 8 04-26-2004 03:07 AM



Advertisments