Reputation: 19243
For a long time, I used simple char[]
buffer when reading and writing files.
Let's assume I have a very simple function like:
int f(int fd_in, int fd_out)
{
char buf[4096];
char* bufp = buf;
ssize_t ret, wr;
ret = read(fd_in, buf, sizeof(buf));
/* ... */
while (ret > 0)
{
wr = write(fd_out, bufp, ret);
/* ... */
}
return wr;
}
Now, that I'm a bit more aware of alignment issues, I'm starting to think this will actually be suboptimal because the buffer will be aligned for char
.
Does it seem reasonable to use a different (larger) integral type for buffer in order to obtain a 'stronger' alignment? Will it make reads/writes more optimal? How far do the benefits go? Is using posix_memalign
to get even more alignment than integral types can achieve a better solution?
Upvotes: 1
Views: 993
Reputation: 44250
Upvotes: 3