array in function parameter declaration

This is a discussion on array in function parameter declaration within the C Programming forums, part of the General Programming Boards category; Hi, this is from the C standard document: If the keyword static also appears within the [ and ] of ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    3

    array in function parameter declaration

    Hi,

    this is from the C standard document:
    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.
    I thought just a normal constant expression would describe this behaviour.....

    In other words, what is the difference between
    Code:
    int foo(int a[static 10]) { ... }
    and
    Code:
    int foo(int a[10]) { ... }
    then?

    Thanks for helping me with this.

    kind regards,

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I haven't actually read the C standard, and this seems like a good reason not to

    I don't think there is a difference (who says there has to be?). That would explain why
    Code:
    int foo(int a[static 10]) {
    is probably never used.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,650
    I think that what the 1999 edition of the C standard means is that given this function:
    Code:
    void print(int numbers[static 10]);
    You would not be allowed to do this:
    Code:
    int my_numbers[5];
    print(my_numbers);
    The actual argument of print() is a pointer to the first element of the my_numbers array, but my_numbers is not "an array with at least as many elements as specified by the size expression", since my_numbers has 5 elements but the size expression specifies 10 elements.

    That said, I was not able to get the MinGW port of gcc 3.4.5 to emit any error or warning despite compiling with the -Wall -pedantic -std=c99 options.

    EDIT:
    Quote Originally Posted by MK27
    I don't think there is a difference (who says there has to be?).
    The 1999 edition of the C Standard, assuming that I interpreted it correctly.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by laserlight View Post
    That said, I was not able to get the MinGW port of gcc 3.4.5 to emit any error or warning despite compiling with the -Wall -pedantic -std=c99 options.
    gcc 4.3.2 with all those switches totally respects this as well.
    Code:
    #include <stdio.h>
    
    void testfunc (int a[static 20]) {
    	int i;
    	for (i=0; i<10; i++) printf("%d\n",a[i]);
    	a[7]=2000;
    }
    
    int main () {
    	int b[10]={11,21,22,23,14,51,66,17,88,91};	
    	testfunc(b);
    	b[5]=0;
    	puts("______________");
    	testfunc(b);
    	return (0);
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,650
    Quote Originally Posted by MK27
    gcc 4.3.2 with all those switches totally respects this as well.
    As in it emits a diagnostic?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by laserlight View Post
    As in it emits a diagnostic?
    Used on the above:
    Code:
    [root~/C]  gcc -Wall -pedantic -std=c99 test2.c
    [root~/C]  ./a.out
    It's the same regardless of the presence/absence of "static". By "respect" I meant the code and not the standard.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,650
    Quote Originally Posted by MK27
    It's the same regardless of the presence/absence of "static"
    Ah, then gcc 4.3.2 is also non-conforming in this respect, assuming that my interpretation is correct. I tried passing a pointer to the second element, but gcc 3.4.5 still accepts the argument without a diagnostic.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Again, I haven't read the C standard, so I don't know if
    shall provide access to the first element of an array with at least as many elements as specified
    would mean that the compiler should check. Then there doesn't seem to be a point to the whole statement, or even that it is slightly misleading because it implies a difference, but unless that difference is spelled out (eg. "in contrast to") it is still only implied.

    And (computer) people are like that on a regular basis, when really they should have a greater sense of responsibility to avoid obfuscation or ambiguity....
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    3
    thank you for your thoughts about this.

    I really think there is a difference in the meaning, even if every existing compiler ignores it. Maybe it's a kind of "dead feature"...

    If there would be no difference then there would be no need for the "static" at all because it has no other function than the quoted one.

    I suspect GCC not to implement this so trial-and-error approach finding the meaning will maybe fail.
    For example the
    Code:
    int foo(int p[*])
    thing isn't implemented by GCC too... but at least in this case we get a warning.
    Last edited by DyingSoul; 02-14-2009 at 03:28 PM.

  10. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    3
    Ok, i finally found something which explains it:
    http://publib.boulder.ibm.com/infoce...rray_index.htm

    Basicly it's a compiler hint for optimization. We tell the compiler that the array passed to the function has at least the defined number of elements. The compiler guarantees defined behaviour only if we abide the restiction we made. So it is in the programmers responsibility not to pass an array to the function with less than the defined number of element.

    To actually cause the undefined behaviour, i think we have to set certain optimization flags of the compiler.
    Last edited by DyingSoul; 02-15-2009 at 07:43 AM.

  11. #11
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    To actually cause the undefined behaviour, i think we have to set certain optimization flags of the compiler.
    This is not quite true. It may work perfectly well with all compilers in the world and still is undefined behaviour. Undefined behaviour doesn't mean the program crashes, it just tells you to not expect it to work. The compiler is free to do anything in this case, ranging from the intended behaviour to destroying the universe.

    Quote Originally Posted by laserlight View Post
    Ah, then gcc 4.3.2 is also non-conforming in this respect
    Also not true. An implementation may actually completely ignore "static" in this context (which I think is what gcc does at the moment) and would still be standard-conformant, i.e. cause undefined behaviour. See my comments on "undefined behaviour" above.


    On a personal note, I reject language features that are supposed to support optimization. In this particular case, the compiler is much better at determining the minimal size of the function argument because he knows of every function invocation. If the compiler can't tell, the programmer doesn't as well. The programmer may even prevent the compiler from performing the best possible optimization. More generally, the programmer can only make guesses about the optimal behaviour in a specific case, while the compiler knows for sure (or at least can be taught).

    Greets,
    Philip

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,650
    Quote Originally Posted by Snafuist
    Also not true. An implementation may actually completely ignore "static" in this context (which I think is what gcc does at the moment) and would still be standard-conformant, i.e. cause undefined behaviour.
    Indeed. I deduced that gcc was nonetheless conforming due to these two statements in section 4 of the 1999 edition of the C standard:
    A strictly conforming program shall use only those features of the language and library specified in this International Standard.
    A conforming hosted implementation shall accept any strictly conforming program.
    However, I was not convinced. Now that you point out that undefined behaviour is involved (and that is directly supported by paragraph 2 of the same section), I am convinced, since no diagnostic is required for undefined behaviour.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21