1

Why am I not getting any warnings in my third function call? Like one in the second function call).

Is NULL != NULL?

#include <stdio.h>
#include <stdlib.h>

void my_function(int arr[static 1]){
    printf("first element: %d\n", *arr);
}

int main(void){

    int arr[]={1,2,3};

    // FISRST:
    //works as expected
    my_function(arr);

    //SECOND:
    //compiler warns as expected
    //and a segmentation fault while running
    my_function(NULL);

    //THIRD:
    //no warnings
    //and segmentation fault
    int* p=NULL;
    my_function(p);

    return EXIT_SUCCESS;
}

or another example:

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

void my_function(int arr[static 5]){
    printf("first element: %d\n", *arr);
}

int main(void){

    int arr[]={1,2,3};

    //FIRST 
    //a compiler warning
    //(array argument is too small)
    my_function(arr);

    //SECOND
    //no warnings
    int* p=arr;
    my_function(p);

    return EXIT_SUCCESS;
}

The second function call has a pointer to the same location as the first function call. It seems that this feature doesn't work with pointers.

4
  • 1
    The C compiler is not required to detect all cases where the programmer has messed up. This is one of those cases which it doesn't detect. Commented Mar 17, 2020 at 14:11
  • C compilers don't bound check arrays so the compiler does not keep track of how many elements are in an array. Arrays in c are pointers so to the compiler int a[] is the same as int* a - in both cases the compiler has a pointer to 1 or more int's (the compiler does not care how many). Your function call parameters is int arr[] so the same as int* arr. For the first and third calls the parameters are the same to the compiler so no warnings. At runtime, for the third call, the runtime deferences p (looks at where P is pointing) and finds it is pointing no-where and hence the segfault. Commented Mar 17, 2020 at 14:41
  • @myk: If C compilers do not check array bounds, why does compiling int foo(void) { int a[3]; return a[4]; } with Apple Clang 11.0.0 report “ warning: array index 4 is past the end of the array (which contains 3 elements)”? Commented Mar 17, 2020 at 17:12
  • 2
    @myk: Arrays in C are not pointers. They are converted to pointers in many expressions. But they are not pointers. Commented Mar 17, 2020 at 17:12

1 Answer 1

4

The C standard does not require a C implementation to diagnose this error, and the writers of your compiler have not put in support for recognizing that the value of p can be shown to violate the static requirement.

C 2018 6.7.6.3 7 says:

… 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.

Compilers, or C implementations generally, are required to diagnose violations of certain rules but not of violations of “shall” requirements generally, per 5.1.1.3 1:

A conforming implementation shall produce at least one diagnostic message (identified in an imple- mentation-defined manner) if a preprocessing translation unit or translation unit contains a violation of any syntax rule or constraint, even if the behavior is also explicitly specified as undefined or implementation-defined. Diagnostic messages need not be produced in other circumstances.

Sign up to request clarification or add additional context in comments.

2 Comments

Is there a difference between the second and the third function call? Both arguments == NULL. But there are two different reactions from compiler.
@dandy_andy: In the second call, the compiler immediately sees a null pointer is passed; it does not need to do any further analysis. In the third call, the compiler cannot know a null pointer is passed except by analyzing the initialization of the object and the control flow. I know GCC has some features for that, but it is not automatically built into everything the compiler does; humans have to attend to all the details and write code for various situations. Presumably nobody has done so for this. I doubt it would be a high priority; this static feature is little used and little benefit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.