title: is there a portable way to manage structure data stored in a mapped file in Linux, without reading it raw byte-by-byte?
body: <p>Let's say I want to store a C array (of fixed length <code>N</code>) of structures:</p>
  
  <pre><code>typedef struct {
    type0 field0;
    type1 field1;
  } foo_struct;
  
  foo_struct array[N];
  </code></pre>
  
  <p>in a file, so that the program can read the array from the file, manipulate it, and write it back. </p>
  
  <p>The program could use system call <code>write</code> to write each instance of <code>typen</code>. Then when we call the program to read, if we assume that <code>sizeof(typen)</code> returns the same as before, we can allocate the array in memory, and use <code>read</code> to stuff fields.  I assume that due to portability issues, there is no way to reliably stuff the whole structure at once, please correct me if I am wrong. </p>
  
  <p>But that is too slow for my purposes.  Even if I read everything at once into a big buffer, I then have to copy to fields.  What happens is that my data size is "yuuge", but manipulation is sporadic. Reading and copying would take way more time than actual data access.  </p>
  
  <p>So I prefer to use <code>mmap</code> and I am assuming <code>mmap</code> works on-demand basis - again please correct me if I am wrong. </p>
  
  <p>Now, this may be faster, but I will have some trouble accessing the data in memory. </p>
  
  <p>Specifying a return value from <code>mmap</code> yourself being a bad idea, the function returns the buffer for you, which is not guaranteed to be aligned, and even if it were aligned on the multiples of <code>sizeof(foo_struct)</code>, this would still not portably guarantee that I can access fields with pointers to the struct and <code>-&gt;</code> operator. </p>
  
  <p>So I think, I have to forget about identifying the structure at all, and just think of my array as a series of chunks of sizes <code>S0 = sizeof(field0)</code> <code>S1 = sizeof(field1)</code> <code>S = S0 + S1</code>, and calculate by hand where the data is, by using pointer arithmetic:</p>
  
  <pre><code>buffer + M * S
  buffer + M * S + S0
  </code></pre>
  
  <p>Then, even that pointer is not aligned, so if I want to read or write a <code>fieldn</code>, I have to split the data into bytes and do it byte-by-byte, which is slow.  Even though there are not so many of these accesses, but then, there are many iterations of this whole process, so I still care to have it as fast as possible. </p>
  
  <p>Is there a way to use <code>mmap</code> (or some other way so that I don't have to read the whole yuuge file), but, not have to access data field-by-field and byte-by-byte?</p>
  
  <p>Please also share if some of what I wrote, goes against Linux or common decency, as I am not completely sure that it does not. </p>