Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #162692
| From | Andrey Tarasevich <andreytarasevich@hotmail.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Usage of static in array declaration |
| Date | 2021-09-10 07:58 -0700 |
| Organization | A noiseless patient Spider |
| Message-ID | <shfrrm$4ej$1@dont-email.me> (permalink) |
| References | <51e13b69-37ba-46e1-95eb-f30dd76590efn@googlegroups.com> |
On 9/10/2021 7:38 AM, Thiago Adams wrote:
>
> It is not clear to me why this static was created and
> what is the behavior.
I think it is pretty clearly explained in the standard and elsewhere: to
help the compiler to optimize the code.
> For instance,
> void F(char name[static 10]){
> }
>
> Can I pass a pointer to char*?
Of course. The parameter type is still `char *`. What prompted the question?
> I will check some compilers to see what is the actual behavior
>
> gcc 11.2
>
> void f(char name[static 10]){}
>
> int main()
> {
> char *p = "abc";
> f(p);
> }
> gives me the same warning with our without static.
>
> warning: 'f' accessing 10 bytes in a region of size 4 [-Wstringop-overflow=]
>
> no warning here
>
> void f(char name[static 10]){}
>
> extern char* s1;
> int main()
> {
> f(s1);
> }
>
For obvious reasons, the compiler was "smart enough" to issue the
warning in the first case. In the second case it is simply impossible.
This feature will produce a tangible difference in code generation. For
example, such parameter cannot be null. For example, Clang will
aggressively discard branches that would've been taken for a null parameter
https://godbolt.org/z/Y5KKzPbv8
--
Best regards,
Andrey Tarasevich
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Usage of static in array declaration Thiago Adams <thiago.adams@gmail.com> - 2021-09-10 07:38 -0700
Re: Usage of static in array declaration Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-09-10 07:58 -0700
Re: Usage of static in array declaration Thiago Adams <thiago.adams@gmail.com> - 2021-09-10 09:17 -0700
Re: Usage of static in array declaration Thiago Adams <thiago.adams@gmail.com> - 2021-09-10 09:37 -0700
Re: Usage of static in array declaration "james...@alumni.caltech.edu" <jameskuyper@alumni.caltech.edu> - 2021-09-10 09:46 -0700
Re: Usage of static in array declaration "james...@alumni.caltech.edu" <jameskuyper@alumni.caltech.edu> - 2021-09-10 08:32 -0700
Re: Usage of static in array declaration Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-09-10 11:26 -0700
csiph-web