how const keyword works in c

后端 未结 6 1739
离开以前
离开以前 2020-12-05 20:48

I want to know about const internals in c and c++ . How compiler imposes constantness ? Can some one help me please.

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-05 21:20

    In addition to the compile-time enforced immutability provided by using the const keyword that other answers to your question have already mentioned, using it sometimes allows the compiler to place such data in the read-only section of a binary and memory. According to Section 2.4.2 "Forever const" in Ulrich Drepper's article How To Write Shared Libraries doing so potentially allows programs to (1) use less resources and (2) start up faster.

    Note that casting away the constness of data in such read-only areas of memory results in the undefined behavior, as usual.

提交回复
热议问题