int x[static 10]

Discussion in 'C Programming' started by Serve Laurijssen, Jul 12, 2003.

  1. A question about this C99 feature.

    suppose I have a function

    void f(int x[static 10]);

    and I call it with

    int *x = malloc(20 * sizeof(int));

    f(x);

    Should a compiler emit a warning then or fail?



    What if it's called with int x[20]; ? Is there a difference?
    Serve Laurijssen, Jul 12, 2003
    #1
    1. Advertising

  2. Serve Laurijssen

    Guest

    Serve Laurijssen <> wrote:
    >
    > suppose I have a function
    >
    > void f(int x[static 10]);
    >
    > and I call it with
    >
    > int *x = malloc(20 * sizeof(int));
    >
    > f(x);
    >
    > Should a compiler emit a warning then or fail?


    No. In this context, "static" indicates that the argument will contain
    *at least* 10 elements, not exactly 10 elements.

    > What if it's called with int x[20]; ? Is there a difference?


    No.

    -Larry Jones

    This game lends itself to certain abuses. -- Calvin
    , Jul 12, 2003
    #2
    1. Advertising

  3. Serve Laurijssen

    Micah Cowan Guest

    "Serve Laurijssen" <> writes:

    > A question about this C99 feature.
    >
    > suppose I have a function
    >
    > void f(int x[static 10]);
    >
    > and I call it with
    >
    > int *x = malloc(20 * sizeof(int));
    >
    > f(x);
    >
    > Should a compiler emit a warning then or fail?


    No. Nore is there UB invoked, nor any other problem. the [static 10]
    means that the parameter must have at *least* 10 elements. No problems
    there. However, if you attempted to pass in *fewer* than 10, you would
    be invoking UB. No diagnostic is required. And there is no difference
    in behavior between dynamic allocation using malloc(), and static or
    automatic allocation.

    HTH,
    -Micah
    Micah Cowan, Jul 14, 2003
    #3
  4. Serve Laurijssen

    Dan Pop Guest

    In <beoqnh$hgn$1.nb.home.nl> "Serve Laurijssen" <> writes:

    >A question about this C99 feature.
    >
    >suppose I have a function
    >
    >void f(int x[static 10]);
    >
    >and I call it with
    >
    >int *x = malloc(20 * sizeof(int));
    >
    >f(x);
    >
    >Should a compiler emit a warning then or fail?
    >
    >What if it's called with int x[20]; ? Is there a difference?


    You've already gotten the correct replies. I only want to point out that
    this is probably the most useless feature introduced by C99: even if the
    function prototype is in scope and a function call is in obvious violation
    (e.g. an array of 5 int is passed to your function), no diagnostic is
    required. So, why bother with this feature? A helpful compiler could
    produce the expected diagnostic even in the absence of the static
    keyword...

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, Jul 14, 2003
    #4
  5. Serve Laurijssen

    Guest

    Dan Pop <> wrote:
    >
    > You've already gotten the correct replies. I only want to point out that
    > this is probably the most useless feature introduced by C99: even if the
    > function prototype is in scope and a function call is in obvious violation
    > (e.g. an array of 5 int is passed to your function), no diagnostic is
    > required. So, why bother with this feature?


    Because you've completely missed the point. The point is not to provide
    better diagnostics (although a high-quality implementation can, and
    should, use the information to do so) but to provide better performance
    by allowing the compiler to generate code to pre-fetch some of the
    values without having to worry about handling invalid addresses.

    -Larry Jones

    Aw Mom, you act like I'm not even wearing a bungee cord! -- Calvin
    , Jul 14, 2003
    #5
    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. Schnoffos
    Replies:
    2
    Views:
    1,206
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,625
    Old Wolf
    Jan 20, 2004
  3. arun
    Replies:
    8
    Views:
    449
    Dave Thompson
    Jul 31, 2006
  4. aling
    Replies:
    8
    Views:
    942
    Jim Langston
    Oct 20, 2005
  5. Replies:
    9
    Views:
    428
    James Kanze
    Apr 17, 2007
Loading...

Share This Page