Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Cast pMemory to the correct type and move it around from pMemory to pMemory + size of the mapped memory - size of the type to which you refer...</p> <p>In other words, you have effectively allocated memory and associated the menory with a file that is changed as you change the memory.</p> <p>In <code>C</code> assuming pMemory is the pointer returned by <code>MapViewOfFile</code>:</p> <pre><code>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 = &amp;p[14]; p-&gt;x = 0; p-&gt;y = 0; p-&gt;z = 0; strcpy( p-&gt;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 &gt; endP ) { n -= (end - endP); // This might be wrong... end = endP; } if( end &lt; start ) return; // end boundary check for( ; start &lt; end; start++, i++ ) { buffer[i] = *start; } } </code></pre> <p>Here's the asm code generated from the code above by the compiler with -O2</p> <pre><code> .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 $LN5@readBytes mov ecx, edx $LN5@readBytes: cmp eax, ecx jae SHORT $LN1@readBytes push esi mov esi, DWORD PTR _buffer$[esp] sub esi, eax $LL3@readBytes: mov dl, BYTE PTR [eax] mov BYTE PTR [esi+eax], dl inc eax cmp eax, ecx jb SHORT $LL3@readBytes pop esi $LN1@readBytes: ret 0 _readBytes ENDP _TEXT ENDS END </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload