If they're even supported by your C99 compiler, it isn't mandated that they be; the standard defines how they behave if your C99 does support them. The implementations use stack-math to implement them. You can't do that at global or file data-segment compilation level. Show 7 more comments. GCC does not seem to allow this const is interpreted as read-only See explanation in this thread: "Initializer element is not constant" error for no reason in Linux GCC, compiling C.
Community Bot 1 1 1 silver badge. PolarBear PolarBear 5 5 silver badges 12 12 bronze badges. The 'const' keyword in C does not really mean 'constant'. This misleads some users. Lowpower I agree - this is a detail of the implementation.
Scott Franco Scott Franco 1 1 silver badge 12 12 bronze badges. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown.
The Overflow Blog. Podcast Making Agile work for data science. Stack Gives Back Visual Studio, Tasking toolchain do not have this problem. The actual code has a number of define 's; I took the resulting precompiler output for simplicity. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 6 years, 7 months ago. Active 6 years, 7 months ago. Viewed times. Cygwin gcc 4. Robert A.
Robert 1 1 silver badge 7 7 bronze badges. Possible duplicate of Variably modified array at file scope — pipe. The value cannot be used in a constant expression simply because the C standard does not say it can be. Casting away const and storing a value is permitted if the destination object is defined without const or dynamically allocated; string literals are not const but may not be written to.
Furthermore, this does have another very good reason: there can be extern constants in different TUs of which the value is not known when compiling the current TU. A way to improve this answer would be to show how to resolve this issue. Wow that's really cool I did not know that was possible. I assume the cost of this computation is negligible. Might I also assume that a compiler could optimize this to a static value?
Yes, the result of sizeof on objects like that is a compile-time constant. Jim Buck Jim Buck It is also possible to use enumeration. CygnusX1 CygnusX1 That sounds pretty bad, since you can take the address of a static const variable.
The behavior you're describing might be a valid optimization, but it's certainly not something that could always work. It is actually fine. It is OK for the C compiler to replace individual uses of const global variables with the constant value wherever possible. If all references to a variable are converted to constants, then the compiler can remove it entirely. If you use the address anywhere, it won't be removed. None of that changes that according to the language standard, C does not allow global arrays with an variable as the size, whether the variable is const or not.
Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password.
0コメント