Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8495215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:30:44+00:00 2026-06-10T23:30:44+00:00

For a long time, I used simple char[] buffer when reading and writing files.

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T23:30:45+00:00Added an answer on June 10, 2026 at 11:30 pm
    • don’t over-estimate the cost of memcpy(). On current machines, memcpy runs “faster than the bus”, and spends a lot of its time waiting for cache slots to be pulled in.
    • the 4096-sized buffers span multiple cache slots (64 byte size, IIRC).
    • The OS has its own buffers, too. (the chunks have to be copied from network buffers, or from DMA memory, for disk)
    • on a partial read+write, you are screwed anyway. Your code is prepared to recieve and send 13 bytes. or 133 or 1333. Or 4095. Or 4096. In most cases, the next read/write will not be aligned nicely. The good news: for diskfiles, the size will probably be a multiple of 512, anyway.
    • the cost of two system calls (read+write) is much higher than the memcpy’s (aligned or unaligned) (there is a nice table of estimated costs inside the cover of the Gray&Reuter book. maybe a bit outdated, but still very instructive)
    • (in Linux) there is a in-kernel bufferless function for this (copyfd, or such) UPDATE : sendfile() is the name, implemented as a systemcall.
    • you could always try to measure the difference: int buff[4096 / sizeof(int)]; would at least be int-aligned. Best way would be to use an union of an int array and a char array. Unions are always aligned to the requirements of the pickyest member.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It has been a long time since I used java and I have run
I have now used way too long time, trying to figure out a problem,
I have been using Java for a very long time and I have problem
I have been programming for a long time in BlueJ and get used to
I used long time Eclipse. In Eclipse it possible to press F2 and hints
Its a long time since I've used C but now I'm trying to compile
Long time reader, first time poster. Any help is greatly appreciated. I have crafted
I have the following function for reading a big-endian quadword (in a abstract base
I have some problems with a very simple yacc/lex program. I have maybe forgotten
I have a rather simple question for you.. I feel like I should have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.