Static before program number c

So, I read a blog about optimizing sorting int blocks, and the implementation was in c. I came to this line which I do not understand:

void nibble_sort_bucket(uint64_t buf[static 1024]) { 

A buffer is basically data that needs to be sorted, and each int in it receives its own 4-bit blocks, so it is mainly intended for benchmarking. When I looked at using static code in c, I discovered two things.

  • Keep function definitions private to the file.
  • Saving the value of a variable between function calls.

None of them make sense here. Can someone explain why you ever put statics in front of a number and what it does?

+8
c static
source share
2 answers

This is the third meaning of the static introduced in C99, but it is not a well-known function. Its purpose is to tell the compiler that you are passing an array with at least 1024 elements.

From C99 (N1256) ยง6.7.5.3 / p7 Declaration of functions (including prototypes) (emphasis):

If the static also appears inside [ and ] the type array, then for each function call the value of the corresponding actual argument should provide access to the first element of the array with at least as many elements as specified in size .

There is some difference between the actual implementations. For example, clang issues a warning when the passed array does not satisfy the above subclause. For example:

 #include <stdio.h> void foo(int a[static 10]) {} int main() { int array[8] = {0}; foo(array); } 

gives:

warning: array argument is too small; contains 8 elements, called requires at least 10 [-Warray-bounds]

while the gcc implementation does nothing (see GCC error 50584 for more details).

+9
source share

This buf[static 1024] statement tells the compiler that buf is at least 1024 characters long. It is used for optimization, in other words, it wants to say that buf can never be null.

+1
source share

All Articles