How to force gcc to blackhole registers so that things maybe garbage collected?
Segher Boessenkool
segher@kernel.crashing.org
Thu May 23 07:48:00 GMT 2019
On Wed, May 22, 2019 at 10:54:19PM +0300, Alexander Monakov wrote:
> On Wed, 22 May 2019, Hamad Ahmed wrote:
> > As an example,
> > the C standard disallows casting a pointer to an int,
> > doing operations on the int, and then casting back to a pointer.
>
> Not true; if the int is sufficiently wide so that pointer representation
> is not truncated, if after your operations you got back the same int, you're
> guaranteed that casting back to a pointer results in the original pointer.
This is implementation-defined, see 6.3.2.3/5 and /6.
GCC defines it as follows:
When casting from pointer to integer and back again, the resulting
pointer must reference the same object as the original pointer,
otherwise the behavior is undefined. That is, one may not use
integer arithmetic to avoid the undefined behavior of pointer
arithmetic as proscribed in C99 and C11 6.5.6/8.
> Moreover, in C you can write a pointer representation into a file using
> printf("%p"), then some time later read it back from that file using
> scanf("%p"), and you'll get back the original pointer. Of course between
> the printf and the scanf said pointer value might not exist in the address
> space of your program at all.
scanf %p:
p
Matches an implementation-defined set of sequences, which
should be the same as the set of sequences that may be produced by
the %p conversion of the fprintf function. The corresponding
argument shall be a pointer to a pointer to void. The input item
is converted to a pointer value in an implementation-defined
manner. If the input item is a value converted earlier during the
same program execution, the pointer that results shall compare
equal to that value; otherwise the behavior of the %p conversion
is undefined.
You cannot validly dereference a pointer if the object it points to does
no longer exist, not in standard C anyway.
Segher
More information about the Gcc-help
mailing list