Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Looking at your program, here is what happens:</p> <ol> <li>You fill the memory initially pointed to by <code>buffer0+buffPositionOutput</code> (Which is where, precisely? I don't know based on the code you give.) up to <code>buffer0+outputbuf.st_blksize</code> with data.</li> <li>You pass the address of the <code>buffer0</code> pointer to <code>posix_memalign</code>, which ignores its current value and overwrites it with a pointer to <code>outputbuf.st_blksize</code> bytes of newly-allocated memory.</li> <li>You write data from the newly-allocated block to disk; this might be anything, since you just allocated memory and haven't written anything there yet.</li> </ol> <p>This won't work, obviously. You probably want to initialize your buffer via <code>posix_memalign</code> at the top of your function, and then just overwrite the block's worth of data in it as you use your aligned buffer to repeatedly write data into the file. (Reset <code>buffpositionoutput</code> to zero after each time you write data, but don't re-allocate.) Make sure you <code>free</code> your buffer when you are done.</p> <p>Also, why are you using <code>pwrite</code> instead of <code>write</code>?</p> <p>Here's how I would implement <code>writeThis</code> (keeping your variable names so you can match it up with your version):</p> <pre><code>void writeThis(char *text) { char *buffer0; size_t buffPositionOutput = 0; posix_memalign(&amp;buffer0, outputbuf.st_blksize, outputbuf.st_blksize); while (*text != 0) { ++text; ++buffPositionOutput; if (buffPositionOutput == outputbuf.st_blksize) { write(fdOutputFile, buffer0, outputbuf.st_blksize); buffPositionOuput = 0; } } if (buffPositionOutput != 0) { // what do you want to do with a partial block of data? Not sure. } } </code></pre> <p>(For speed, you might consider using <code>memcpy</code> calls instead of a loop. You would need to know the length of the data to write ahead of time though. Worry about that after you have a working solution that does not leak memory.)</p>
 

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