Skip to content

Comment on When static makes your C code 10 times fasterparent

Comments

They covered that with if the object wasn't const to begin with.

Partially "(const references or const pointers can refer to non-const objects)".

Here are the references to linker scripts?

I'm afraid I'm not following.

I believe this account of things is accurate (ignoring C++'s reference types for simplicity):

The C and C++ standards are written in such a way as to enable compilers to place constant data on read-only memory. Casting away constness is never illegal in and of itself, but if you do so and then assign to a variable which was declared as const, that is undefined behaviour.

Similarly, assigning into the character array of a string literal is undefined behaviour, whether or not const was used. Again that's to enable the compiler to make use of read-only memory.

Going in the other direction is safe, as preventing assignments doesn't introduce problems. That is to say, using a pointer-to-const type to point to a non-const variable poses no problem.

Related:

https://stackoverflow.com/a/9079161/

https://wiki.sei.cmu.edu/confluence/display/c/EXP05-C.+Do+no...

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.