Strict aliasing and malloc
narwhal x <[email protected]>
| Newsgroups | gmane.comp.lib.newlib |
|---|---|
| Message-ID | <CADekCYGGXfAbW5S1J_WkaOHm30L=BzKtw0N3zFvGZNA8Pkyg3w@mail.gmail.com> |
Hello, I have a question regarding newlib and the -fstrict-aliasing implied by turning on O2. The strict aliasing implied by the ISO standard and enabled in gcc with O2 (This might be specific to gcc, but could be the case with any compiler with aliasing optimizations) makes it so you can only cast a pointer to a compatible type, and a special case is malloc, which should return an "undeclared type" *. I however did not find the -fno-strict-aliasing flag in any configuration or makefile (If I just overlooked it, and the flag is mandatory that would answer my question) My question: In newlib/libc/stdlib/mallocr.c on line 2212 you have the statement: "top = (mchunkptr)brk;" Here top is of type "mchunkptr" and brk is a "char *". The standard says that you can not just alias a incompatible type and dereference it (unless it's a malloc'ed variable, as it would change it's type when written to, but how do you inform the compiler?) As an example, see 4.2.1 (p. 63) in https://www.cl.cam.ac.uk/~pes20/cerberus/notes30.pdf So is this allowed? Or am I missing something. * After asking in the gcc IRC, they mentioned that the way they go about having the special case for malloc is making sure the libc library is linked from a library and no LTO is performed. My main reason for asking is just wanting to know how a malloc implementation should deal with these restrictions stated by the ISO C standard, and improve my understanding of the (sometimes confusing) aliasing rules. Thanks