Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Imagine if I came to you with a piece of code like this:</p> <pre><code>void *foo; foo = malloc(n); if (foo) free(foo); foo = malloc(n); </code></pre> <p>Then I came to you and said, help! <code>foo</code> does not have the same address on the second allocation!</p> <p>I'd be crazy, right?</p> <p>It seems to me like you've already demonstrated clear knowledge of why this doesn't work. There's a reason that the documention for any API that takes an explicit address to map into lets you know that the address is just a suggestion, and it can't be guaranteed. This also goes for <code>mmap()</code> on POSIX.</p> <p>I would suggest you write the program in such a way that a change in address doesn't matter. That is, don't store too many pointers to quantities inside the buffer, or if you do, patch them up after reallocation. Similar to the way you'd treat a buffer that you were going to pass into <code>realloc()</code>.</p> <p>Even the documentation for <a href="http://msdn.microsoft.com/en-us/library/aa366763(VS.85).aspx" rel="nofollow noreferrer"><code>MapViewOfFileEx()</code></a> explicitly suggests this:</p> <blockquote> While it is possible to specify an address that is safe now (not used by the operating system), there is no guarantee that the address will remain safe over time. Therefore, it is better to let the operating system choose the address. In this case, you would not store pointers in the memory mapped file, you would store offsets from the base of the file mapping so that the mapping can be used at any address. </blockquote> <p><b>Update from your comments</b></p> <p>In that case, I suppose you could:</p> <ul> <li><p>Not map into contiguous blocks. Perhaps you could map in chunks and write some intermediate function to decide which to read from/write to?</p></li> <li><p>Try porting to 64 bit.</p></li> </ul>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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