Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > int x[static 10]

Reply
Thread Tools

int x[static 10]

 
 
Serve Laurijssen
Guest
Posts: n/a
 
      07-12-2003
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?


 
Reply With Quote
 
 
 
 
lawrence.jones@eds.com
Guest
Posts: n/a
 
      07-12-2003
Serve Laurijssen <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Micah Cowan
Guest
Posts: n/a
 
      07-14-2003
"Serve Laurijssen" <(E-Mail Removed)> 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

 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      07-14-2003
In <beoqnh$hgn$(E-Mail Removed)1.nb.home.nl> "Serve Laurijssen" <(E-Mail Removed)> 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: http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
lawrence.jones@eds.com
Guest
Posts: n/a
 
      07-14-2003
Dan Pop <(E-Mail Removed)> 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
 
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
why is int a[0] not allowed, but int* a = new int[0] is? haijin.biz@gmail.com C++ 9 04-17-2007 09:01 AM
Difference between int i, j; and int i; int j; arun C Programming 8 07-31-2006 05:11 AM
int a[10]; int* p=(int*)((&a)+1); But why p isn't equal to ((&a)+1)? aling C++ 8 10-20-2005 02:42 PM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments