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

  • Home
  • SEARCH
  • 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 613935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:03:27+00:00 2026-05-13T18:03:27+00:00

I have a legacy data structure that’s 672 bytes long. These structs are stored

  • 0

I have a legacy data structure that’s 672 bytes long. These structs are stored in a file, sequentially, and I need to read them in.

While I can read them in one-by-one, it would be nice to do this:

// I know in advance how many structs to read in
vector<MyStruct> bunchOfStructs;
bunchOfStructs.resize(numberOfStructs);

ifstream ifs;
ifs.open("file.dat");
if (ifs) {
    ifs.read(&bunchOfStructs[0], sizeof(MyStruct) * numberOfStructs);
}

This works, but I think it only works because the data structure size happens to be evenly divisible by my compiler’s struct alignment padding. I suspect it’ll break on another compiler or platform.

The alternative would be to use a for loop to read in each struct one-at-a-time.

The question –> When do I have to be concerned about data alignment? Does dynamically allocated memory in a vector use padding or does STL guarantee that the elements are contiguous?

  • 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-13T18:03:27+00:00Added an answer on May 13, 2026 at 6:03 pm

    The standard requires you to be able to create an array of a struct type. When you do so, the array is required to be contiguous. That means, whatever size is allocated for the struct, it has to be one that allows you to create an array of them. To ensure that, the compiler can allocate extra space inside the structure, but cannot require any extra space between the structs.

    The space for the data in a vector is (normally) allocated with ::operator new (via an Allocator class), and ::operator new is required to allocate space that’s properly aligned to store any type.

    You could supply your own Allocator and/or overload ::operator new — but if you do, your version is still required to meet the same requirements, so it won’t change anything in this respect.

    In other words, exactly what you want is required to work as long as the data in the file was created in essentially the same way you’re trying to read it back in. If it was created on another machine or with a different compiler (or even the same compiler with different flags) you have a fair number of potential problems — you might get differences in endianness, padding in the struct, and so on.

    Edit: Given that you don’t know whether the structs have been written out in the format expected by the compiler, you not only need to read the structs one at a time — you really need to read the items in the structs one at a time, then put each into a temporary struct, and finally add that filled-in struct to your collection.

    Fortunately, you can overload operator>> to automate most of this. It doesn’t improve speed (for example), but it can keep your code cleaner:

    struct whatever { 
        int x, y, z;
        char stuff[672-3*sizeof(int)];
    
        friend std::istream &operator>>(std::istream &is, whatever &w) { 
           is >> w.x >> w.y >> w.z;
           return is.read(w.stuff, sizeof(w.stuff);
        } 
    };
    
    int main(int argc, char **argv) { 
        std::vector<whatever> data;
    
        assert(argc>1);
    
        std::ifstream infile(argv[1]);
    
        std::copy(std::istream_iterator<whatever>(infile),
                  std::istream_iterator<whatever>(),
                  std::back_inserter(data));  
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 353k
  • Answers 353k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Just get cookies from Set-Cookie param in response headers and… May 14, 2026 at 7:48 am
  • Editorial Team
    Editorial Team added an answer It's not programmatically, but in the 12 Hive of your… May 14, 2026 at 7:48 am
  • Editorial Team
    Editorial Team added an answer I would assume you're wrapping the code in $(document).ready( function… May 14, 2026 at 7:48 am

Related Questions

I've inherited a less-than-ideal table structure, and I'm trying to improve it as much
I am currently in the process of restructuring my local Subversion repository by adding
I am new to threads and in need of help. I have a data
I am in the process of integrating a number of legacy systems. They each
I have a stored procedure in an old SQL 2000 database that takes a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.