int x[static 10]

S

Serve Laurijssen

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?
 
L

lawrence.jones

Serve Laurijssen said:
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
 
M

Micah Cowan

Serve Laurijssen said:
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
 
D

Dan Pop

In said:
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
 
L

lawrence.jones

Dan Pop said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top