ss
might be some global variable because you could call foo
with some global array like char str[100];
as its argument (e.g. by having foo(str);
in your main
)...
and bar
could modify that global variable (then strlen(ss)
could change at every loop).
BTW restrict
has perhaps not the meaning you believe. Read carefully section §6.7.3 of the C11 standard and §6.7.3.1. IMHO restrict
is in practice mostly useful on two formal arguments of the same function to express the fact that they are not "aliases" or "overlapping" pointers (if you guess what I really mean), and perhaps optimization efforts on restrict
have probably been focused on such cases.
Perhaps (but unlikely), on your particular program, a compiler might optimize as you want if you invoke it as gcc -flto -fwhole-program -O3
(on every translation unit, and at program link time). I won't bet on that (but I leave you to check).
Why is this case?
As to why current GCC (or Clang) does not optimize like you want it to, it is because nobody has written such an optimization pass and have it enabled at -O3
.
Compilers are not required to do optimizations, just allowed to do some of them (at the choice of their implementors).
Since it is free software, feel free to propose a patch by contributing to GCC (or to Clang). You might need a whole year of work, and you are not sure that your optimization would be accepted (because in practice nobody codes like you show, or because your optimization would be too specific, so unlikely to be triggered, but still slowing down the compiler). But you are welcome to try.
Even if §6.7.3.1 allows your optimization (as the answer by user2357112 demonstrates), it might practically not worth the effort to implement it.
(my intuition is that implementing such an optimization is hard, and won't profit much to existing programs in practice)
BTW, you can definitely experiment such an optimization by coding some GCC plugin doing it (since the plugin framework was designed for such experimentations). You might discover that coding such an optimization is a lot of work and that practically speaking it does not improve the performance of most existing programs (e.g. in a Linux distribution), because few people code that way.
Both GCC and Clang are free software projects, and contributors to them are (from the point of view of e.g. the FSF) volunteers. So feel free to improve GCC (or Clang) like you want it to optimize. By past experience, contributing even a small piece of code to GCC takes a lot of time. And GCC is a huge program (about ten millions lines of code), so understanding its internals is not easy.
bar()
? – Swordbillfoo
], let L be any lvalue that has &L based on [ss
]. If L is used to access the value of the object X that it designates, and X is also modified (by any means), then the following requirements apply: [the typess
points to] shall not be const-qualified... If these requirements are not met, then the behavior is undefined." – Exhibitori
variables were stored on a slow hard disk on a server farm in Botswana :-) – Derinarestrict
has, which apparently differ from the OP's. I think the OP is asking in order to see whether they didn't know, overlooked or misunderstood something. An answer of "there is nothing in the language or the code forbidding it" is therefore valuable. – Commute