r/C_Programming • u/TorontoDude12344 • 22d ago
Question What does the compiler do when we declare an array parameter's length as static?
I'm reading C Programming: A Modern Approach by K. N King
I'm on page 200 of that book, under the heading Using static in Array Parameter Declarations
The example given is: int sum_array(int a[static 3], int n)
And there is a short description:
Using static in this way has no effect on the behavior of the program. The presence of static is merely a “hint” that may allow a C compiler to generate faster instructions for accessing the array. (If the compiler knows that an array will always have a certain minimum length, it can arrange to “prefetch” these elements from memory when the function is called, before the elements are actually needed by statements within the function.)
So I was wondering, what exactly does a compiler do in this situation? Does it simply create a cache?
Also, what does it do line by line? How does the compiler the keyword static is in the context of a array parameter declaration?
If you were to write a compiler, and implement this feature, how would you do it (pseudocode)?
23
u/tstanisl 22d ago
It has a few applications. For example it allows to express that arrays are non-empty.
Compiling with
-O3produces:The compiler can assume that
arris never NULL infooand optimize accordingly. See godbold.Theoretically, it could help auto-vectorization or loop unrolling. But I haven't seen big impact of this feature yet.
11
u/aocregacc 22d ago
it can also help you catch bugs like the following:
Some compilers can see that the memory passed to
foohere is smaller than the parameter indicates and warn you about it.10
u/mjmvideos 22d ago
You could always write a little test program and have the compiler generate assembly code using different compiler options and see what it does.
6
u/Broad-Promise6954 22d ago
It's a special case added in the 1999 C standard. It promises that the pointer passed to a function points to the first of at least that many elements. Some of the committee members at the time were sure this was a great way to enable vectorization optimization in certain compilers.
4
u/rubidus-api 22d ago
I mostly use this for one thing: a pointer parameter that must not be NULL. As far as I know,
[static 1]is the only way to say that in standard C (GCC and Clang also have__attribute__((nonnull)), but that's an extension). I picked it up from a YouTube video, and it's about the only place I use this syntax.It has limits, though. It only catches a NULL the compiler can see at compile time, such as a literal
NULL. A pointer that might be NULL at run time passes without any warning, and no check is added at run time.The promise also works against you. If a NULL does arrive at run time, that's undefined behavior, so the compiler may delete a NULL check inside the function.
GCC at least warns about this with
-Wall(-Wnonnull-compare); Clang 19 removes it without a warning. So if the pointer comes from outside, check it before the call, not inside the function.As for what the compiler does: it doesn't create a cache, and it generates no code for
staticitself. The parameter is still a plain pointer; the compiler just records a fact about it: "not NULL, and at least N elements can be read". You can see this in Clang's LLVM IR, where the parameter ofint sum3(int a[static 3], int c)is markeddereferenceable(12)(three ints) and a plainint *ais not. So in a toy compiler, I'd store N on the parameter's type when parsing, and pass it on as that kind of attribute. The optimizer may then read the array earlier than the code says or drop NULL checks, and the warnings can catch calls with NULL or with an array that's too small.The "prefetch" in the book is possible in principle, because the compiler knows those elements are safe to read. But in small tests with GCC 14 and Clang 19 at -O2, there were no prefetch instructions and no change in the loads; the only differences were the warnings and the removed NULL check. If you want to know for your own code, compare the assembly (
-S, or Compiler Explorer) and benchmark it.5
u/EpochVanquisher 22d ago
I believe it allows the compiler to reorder loads from a[3] more freely. This is usually not a big deal, and there are other ways to get the same effect.
1
u/Avikonette 22d ago
To wherewhat? x86_64 as Intel(R)?
1
u/mivanchev 22d ago
This is indispensable for catching bugs and the modern way of not ruining your relationship with the compiler and providing it hints. Use it as much as you can.
1
u/Linguistic-mystic 22d ago
The other answers don't mention an important other effect: it's a hint to the reader (maintainer) that communicates array size. This is indispensable and we should all use it whenever we know the size.
-3
u/flyingron 22d ago edited 22d ago
Welcome to the massive stupidity which is the C array type. Any time an array is mentioned in either a function parameter or a return type whether it was given a definite size or not, it is converted to a pointer to the type contained in the array. This was a short cut hack that was done in early C that they were never able to get away form (they did fix structs so they pass/return/assign properly).
By the way... have fun sometimes and print the sizeof a.
The static thing is a red herring. It dates back to the days of very dumb compilers/optimizers. It's about as useful as declaring something register these days.
-3
u/gnash117 22d ago
I agree with other commenters this will likely have zero effect with modern compilers.
The prefetch comment is about pulling memory from the hard drive (ssd) into ram and CPU cache. Time wise getting values from a SSD or even ram is quite an expensive operation. If your the compiler knows it needs that memory before it is used it can issue a prefetch instruction which basically tells the computer to start reading values before it is needed. Since the instruction is not involved with calculations, branching, or the final result most people never think about it but reducing SSD / ram / cache read time is one of the top ways to gain performance. High performance BLAS libraries often hand code the prefetch instructions. Compilers typically do better than humans since they have really complex code to insert the prefetch instructions at the most valuable instance in the code.