How to do something like memcpy in D

I have a memory location a and I want to copy a certain number of bytes to another location quickly, how would I do in D?

For example, how would I do this:

int main() { void* src_data = 0x40001255; void* dst_data = 0x47F22000; u32 size = 0x200; memcpy(dst_data, src_data, size); } 

Also, how to quickly fill in the structure:

 struct data_struct { u32 block1; u32 block2; u32 block3; u32 block4; u32 block5; u62 block6; u128 bigblock; } data_struct_t; int main() { void* src_data = 0x40001255; struct data_struct_t dst_data; u32 size = sizeof(data_struct); memcpy(dst_data, src_data, size); } 

Thanks! Rule

+6
source share
2 answers

The slice assignment will perform a copy of the array, which internally calls memcpy.

 void main() { void* src_data = 0x40001255; void* dst_data = 0x47F22000; uint size = 0x200; dst_data[0..size] = src_data[0..size]; } 

For the second:

 struct data_struct { uint block1, block2, block3, block4, block5; ulong block6; uint[4] bigblock; } void main() { auto src_data = cast(data_struct*) 0x40001255; // unaligned, WTF?! auto dst_data = *src_data; } 
+11
source

Note that you also have access to C memcpy in D. D can directly access C throughout the standard library.

+7
source

Source: https://habr.com/ru/post/896471/


All Articles