How can I point an array to existing data in memory while using Better C?
ag0aep6g
anonymous at example.com
Sun Jul 8 21:10:50 UTC 2018
On 07/08/2018 10:27 PM, Stijn Herreman wrote:
> https://forum.dlang.org/thread/ddckhvcxlyuvuiyazpqy@forum.dlang.org is
> similar to what I want to do, but the code by Adam cannot be used when
> using Better C (I assume slicing isn't supported).
Slicing a pointer works fine for me with -betterC (DMD 2.081.0). Why do
you think it doesn't work?
> I have the following code:
>
> environment.d
>
> public __gshared header* GPT_header;
> public __gshared partition_entry[128] GPT_entries;
>
> main.d
>
> GPT_header = cast(header*)0x00007e00;
> *(GPT_entries).ptr = *(cast(partition_entry*)0x00008000);
>
> Disassembled, the code from main.d looks like this:
>
> mov ds:_D4boot11environment10GPT_headerPSQBg3gpt6header, 7E00h
> mov esi, 8000h
> mov edi, offset
> _D4boot11environment11GPT_entriesG128SQBk3gpt15partition_entry
> mov ecx, 20h
> rep movsd
>
> So GPT_header points to 0x7e00 as desired, but for GPT_entries it copies
> the first array element to a different location and makes it point to
> that location instead of 0x8000.
As far as I can tell, the assembler code does exactly what your D code
says: Copy one partition_entry (size is apparently 128 bytes?) from
0x8000 to GPT_entries[0].
If you want to set the .ptr of GPT_entries explicitly, I don't know if
that's possible. But the code you have is not how you'd do it. You're
dereferencing GPT_entries.ptr, you're not setting it.
You can get a partition_entry[] that starts at 0x8000 by casting a
pointer and slicing that. Works for me with -betterC:
----
struct partition_entry {}
public __gshared partition_entry[] GPT_entries;
extern(C) void main()
{
GPT_entries = (cast(partition_entry*) 0x8000)[0 .. 128];
}
----
More information about the Digitalmars-d-learn
mailing list