Wrong pointer calculation without casting on struct
tcak via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Feb 5 20:10:07 PST 2015
On Friday, 6 February 2015 at 03:59:51 UTC, tcak wrote:
> I am on 64-bit Linux.
>
> I defined a struct that it 8 bytes in total.
>
> align(1) struct MessageBase{
> align(1):
> ushort qc;
> ushort wc;
> ushort id;
> ushort contentLength;
> void[0] content;
> }
>
>
> I defined a function in this struct that tries to set a pointer
> to "contentLength" field.
>
> writeln( "Without: ", (&this + id.offsetof) );
> writeln( "With : ", (cast(size_t)&this + id.offsetof) );
>
> Results:
> Without: 7FFFF74F5030
> With : 140737342558228
>
> 0x7FFFF74F5030 => 140737342558256
>
>
> As it is seen, there is 28 bytes of difference between them.
> What is this behaviour exactly?
By the way,
writeln("Base Normal: ", &this);
writeln("Base Cast : ", cast(size_t)&this);
Result:
Base Normal: 7FFFF74F5010
Base Cast : 140737342558224
0x7FFFF74F5010 => 140737342558224
These are same. So, the issue is about addition process.
More information about the Digitalmars-d-learn
mailing list