Re: static array params

Discussion in 'C Programming' started by Keith Thompson, Feb 7, 2013.

  1. 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.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"
     
    Keith Thompson, Feb 7, 2013
    #1
    1. Advertising

  2. Keith Thompson

    Shao Miller Guest

    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 = ip2 = 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));
    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) = 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);
    }
    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
     
    Shao Miller, Feb 7, 2013
    #2
    1. Advertising

  3. Keith Thompson

    Noob Guest

    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.
     
    Noob, Feb 7, 2013
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter B. Steiger

    Can a static array contain a dynamic array of pointers?

    Peter B. Steiger, Apr 19, 2004, in forum: C Programming
    Replies:
    8
    Views:
    2,136
    Dave Thompson
    Apr 26, 2004
  2. Replies:
    2
    Views:
    1,611
    Jack Klein
    Jul 14, 2005
  3. Replies:
    23
    Views:
    873
    Chris Thomasson
    Aug 29, 2007
  4. Barry
    Replies:
    9
    Views:
    492
    Ara.T.Howard
    Sep 15, 2005
  5. Monica Saiz

    ajax passing params array

    Monica Saiz, Sep 24, 2008, in forum: Ruby
    Replies:
    0
    Views:
    140
    Monica Saiz
    Sep 24, 2008
Loading...

Share This Page