Deep Analysis of Const Qualifier in C

前端 未结 9 1002
离开以前
离开以前 2020-11-30 08:26

Where does a const variable gets stored exactly and how does it behaviour change? Say for example:

const int i=10; // stores where ?  
main()  
         


        
相关标签:
9条回答
  • 2020-11-30 08:57

    The second code shouldn't compile at all. And the compilers I've here agree: gcc gives an error with -pedantic-errors (whose purpose is to turn into error some mandatory diagnostics that historically gcc hasn't considered as error), xlc gives an error as well.

    If you want a reference: 6.3.16.1 in C90 standard describes when an assignment is possible:

    both operands are pointers to qualified or unqualified versions of compatible types, and the type pointed to by the left has all the qualifiers of the type pointed to by the right

    and c99 as a similar constraint.

    0 讨论(0)
  • 2020-11-30 08:59

    The keyword const indicates a variable that is read-only (i.e., cannot be changed at run-time). It does not indicate a compile-time constant. Therefore, all of the usual attributes of variables apply; specifically, it is allocated addressable storage space.

    Unlike with #define, your constant is not necessarily inlined by the compiler. Rather, the compiler will create a symbol corresponding to your const declaration in the object file so that it can be accessed from other code files—remember that const objects have external linkage by default in C (although some compilers will still inline the constant value within the file where it is defined).

    The reason the code snippet that you posted "works" is because the unary operator & can be applied to any lvalue, which includes a const object. Though the behavior here is undefined, I suspect that your compiler is detecting this usage and ensuring that your const declaration is given address space, and therefore not inlining it, even within the file it is declared.

    EDIT: Also see: http://publications.gbdirect.co.uk/c_book/chapter8/const_and_volatile.html

    Let's look at what is meant when const is used. It's really quite simple: const means that something is not modifiable, so a data object that is declared with const as a part of its type specification must not be assigned to in any way during the run of a program. It is very likely that the definition of the object will contain an initializer (otherwise, since you can't assign to it, how would it ever get a value?), but this is not always the case. For example, if you were accessing a hardware port at a fixed memory address and promised only to read from it, then it would be declared to be const but not initialized.

    Taking the address of a data object of a type which isn't const and putting it into a pointer to the const-qualified version of the same type is both safe and explicitly permitted; you will be able to use the pointer to inspect the object, but not modify it. Putting the address of a const type into a pointer to the unqualified type is much more dangerous and consequently prohibited (although you can get around this by using a cast). For example...

    0 讨论(0)
  • 2020-11-30 08:59

    It really shouldn't work.

    Constants are generally not stored anywhere. They are expanded inline.

    It's possible that your compiler is being nice to you and giving you a memory location to modify, but normally that's impossible.

    What warnings are you getting? I imagine you must get some...

    0 讨论(0)
提交回复
热议问题