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 5980595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:49:24+00:00 2026-05-22T21:49:24+00:00

In the Advanced Programming in the Unix Environment book there’s a part (ch 8.14,

  • 0

In the “Advanced Programming in the Unix Environment” book there’s a part (ch 8.14, page 251) in which the author shows us the definition of the “acct” struct (used to store accounting records info). He then shows a program in which he reads the accounting data from a file into the struct (the key part of which is):

fread (&acdata, sizeof(acdata), 1, fp)

The trouble I’m having is that I’ve heard that C compilers will sometimes rearrange the elements of a struct in memory in order to better utilize space (due to alignment issues). So, if this code is just taking all of the content of the file and sticking it into acdata (and the contents of the file are arranged to match the ordering specified in the struct definition) if some of the elements of struct have been moved, then if I refer to them in code, I may not be getting what I expected (since the data in the file did not get rearranged the way the struct did in memory).

What am I missing (because from what I’m getting this doesn’t seem reliable)?

Thanks for your help (my apologies if I’ve done something wrong procedurally – this is my first time posting)

  • 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-05-22T21:49:25+00:00Added an answer on May 22, 2026 at 9:49 pm

    Yes

    Your program will be stable.

    Your question has touched off a bonfire of portability recommendations that you didn’t actually ask for. The question you seemed to be asking is “is this code pattern and my program stable?”. And the answer to that is yes.

    You structure will not be reordered. C99 specifically prohibits rearranging the structure members.1

    Also, the layout and alignment do not depend on optimization level. If they did, all programs would have to be entirely built with the same optimization level, as well as all library routines, the kernel, all kernel interfaces, etc.

    Users would also have to track, forever, the optimization level of every one of those interfaces listed above that ever had been compiled as part of the system.

    The memory alignment rules are really a kind of hidden ABI. They can’t change without adding very specialized and by definition rarely-used compiler flags. They tend to work just fine over different compilers. (Otherwise, every element of a system identified above would ALSO have to be compiled by the same compiler, or be useless. Every compiler that supports a given system uses the exact same alignment rules. Nothing would work, otherwise.) The compiler flags that change alignment policies are usually intended to be built into the compiler configuration for a given OS.

    Now, your binary file layout, while perfectly reasonable, is a bit old-school. It has certain drawbacks. While none of these are show-stoppers and none are generally worth rewriting an app, they include:

    • it’s hard to debug binary files
    • they do lock in a single byte order and a single alignment policy. In the (sadly, increasingly unlikely) case where you need to port to a new architecture, you might end up needing to unpack the record with memcpy(3). Not the end of the world.
    • they aren’t structured. Things like YAML and, ahem, even XML are sort of self-parsing, so it becomes a lot easier to read in a file, and certain types of file manipulations can be done with tools. Even more important, the file format itself becomes more flexible. Your ability to take advantage of the auto-parsed-object is limited, however, in C and C++.

    As I understand Paxdiablo’s request, he would like me to agree that there exist compiler options and pragmas that, if used, will alter the alignment rules. That’s true. Obviously these options are used only for specific reasons.



    1. C99 6.7.2.1(13) Within a structure object, the non-bit-field members and the units in which bit-fields
    reside have addresses that increase in the order in which they are declared.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading APUE (Stevens' Advanced Programming in the UNIX Environment ), so there
I'm reading a book (Advanced Programming in the UNIX Environment) and I'm going through
When reading about pipes in Advanced Programming in the UNIX Environment, I noticed that
I am learning APUE.2e (Advanced Programming in the UNIX® Environment, Second Edition) these days
i have a unix c programming assignment in which i do some advanced C
I'm currently reading Advanced Programming in the Unix Environment by Stevens/Rago. Within the process
Advanced Programming in the UNIX Environment by W. Richard Stevens states: What are the
a strange idea comes to me when I'm reading APUE(Advanced Programming in UNIX Environment).
all, The code below comes from Advanced Programing in Unix Environment, it creates a
Which are the most advanced frameworks and tools there are available for python for

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.