演員pMemory到正確的類型,然後從pMemory移動它被映射的內存的pMemory +大小 - 你提到的類型的大小...
換句話說,您已經有效地分配了內存,並將這個菜單與一個隨着您更改內存而更改的文件關聯起來。
在C
假設pMemory是由MapViewOfFile
返回的指針:
int x = (*(int *)pMemory); // Read the first int
char c = (*(char *)pMemory); // Read the first char
typedef struct oddball { int x, int y, int z, char str[256] } oddball; // assume the typedef syntax is right...
oddball *p = (oddball *)pMemory; // point to the base of the mapped memory
p += 14; // p now points to the 15th instance of oddball in the file.
// Or... p = &p[14];
p->x = 0;
p->y = 0;
p->z = 0;
strcpy(p->str("This is the 0, 0, 0 position"));
// You've now changed the memory to which p[14] refers.
// To read every byte... (Again in C, use the compiler to generate asm
// Assumes:
// fileSize is the size of the mapped memory in bytes
// pMemory is the pointer returned by MapViewOfFile
// buffer is a block of memory that will hold n bytes
// pos is the position from which you want to read
// n is the number of bytes to read from position pos and the smallest size in bytes to which buffer can point
void readBytes(unsigned int fileSize, char *pMemory, char *buffer, unsigned int n, unsigned int pos)
{
char *endP = pMemory + fileSize;
char *start = pMemory + pos;
char *end = start + n;
int i = 0;
// Code to stay within your memory boundaries
if(end > endP)
{
n -= (end - endP); // This might be wrong...
end = endP;
}
if(end < start)
return;
// end boundary check
for(; start < end; start++, i++)
{
buffer[i] = *start;
}
}
下面是從由編譯器上面的代碼生成的彙編代碼與-O2
.686P
.XMM
.model flat
PUBLIC _readBytes
_TEXT SEGMENT
_fileSize$ = 8 ; size = 4
_pMemory$ = 12 ; size = 4
_buffer$ = 16 ; size = 4
_n$ = 20 ; size = 4
_pos$ = 24 ; size = 4
_readBytes PROC ; COMDAT
mov eax, DWORD PTR _pMemory$[esp-4]
mov edx, DWORD PTR _fileSize$[esp-4]
mov ecx, DWORD PTR _n$[esp-4]
add edx, eax
add eax, DWORD PTR _pos$[esp-4]
add ecx, eax
cmp ecx, edx
jbe SHORT [email protected]
mov ecx, edx
[email protected]:
cmp eax, ecx
jae SHORT [email protected]
push esi
mov esi, DWORD PTR _buffer$[esp]
sub esi, eax
[email protected]:
mov dl, BYTE PTR [eax]
mov BYTE PTR [esi+eax], dl
inc eax
cmp eax, ecx
jb SHORT [email protected]
pop esi
[email protected]:
ret 0
_readBytes ENDP
_TEXT ENDS
END