Liste des Groupes | Revenir à cl c |
To the question, I was reading this, but I am notStrictly speaking, the passage is misleading. It dues not matter whether the compiler decided to place const data into read-only storage. If you "hack around" data constness (i.e. if you attempt to modify const data), you _always_ get undefined behavior, regardless of where the data is actually stored.
sure what the quoted passage means:
Matt Stancliff, "So You Think You Can Const?",
<https://matt.sh/sytycc>
<< Your compiler, at its discretion, may also choose
to place any const declarations in read-only storage,
so if you attempt to hack around the const blocks,
you could get undefined behavior. >>
I do not understand if just declaring that a pointerIt can't. The original quoted passage is obviously meant to be about constant data, i.e. about _top-level_ constness.
is to constant data may incur in that problem
E.g. consider this little internal helper of mineIt is perfectly safe. One can even argue that standard declaration if `free` as `void free(void *)` is defective. It should have been `void free(const void *)` from the very beginning.
(which implements an interface that is public to
do an internal thing...), where I am casting to
pointer to non-constant data in order to free the
pointed data (i.e. without warning):
```c
static int MyStruct_free_(MyStruct_t const *pT) {
assert(pT);
free((MyStruct_t *)pT);
return 0;
}
```
Assuming, as said, that the data was originally
allocated with malloc, is that code safe or
something can go wrong even in that case?
Les messages affichés proviennent d'usenet.