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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:39:41+00:00 2026-06-17T10:39:41+00:00

I have a huge contiguous array x that I fread from a file. How

  • 0

I have a huge contiguous array x that I fread from a file.

How do I drop this chunk into a std::vector<>? In other words, I prefer to have the result to be in std::vector<> rather than the array, but I want the resultant C++ code to be as efficient as this plain C-version which drops the chunk right into the array.

From searching around, I think I may have to use placement-new in some form, but I’m uncertain about the sequence of calls and ownership issues. Also, do I need to worry about alignment issues?

I am testing for with T = unsigned, but I expect a reasonable solution to work for any POD struct.

using T = unsigned;
FILE* fp = fopen( outfile.c_str(), "r" );
T* x = new T[big_n];
fread( x, sizeof(T), big_n, fp );

// how do I get x into std::vector<T> v
// without calling a gazillion push_backs() or copies ?!?

delete[] x;
fclose( fp );
  • 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-17T10:39:43+00:00Added an answer on June 17, 2026 at 10:39 am

    You use the std::vector constructor which sets the size of the vector, and use std::vector::data to get a pointer to allocated memory.

    Keeping with your use of fread:

    std::vector<T> x(big_n);
    fread(x.data(), sizeof(T), big_n, fp);
    

    As noted by others, using fread if the type T is not a POD type will most likely not work. You can then use C++ streams and std::istreambuf_iterator to read the file into the vector. However this have the drawback that it loops over all items in the file, and if big_n is as big as it sounds then this might be a performance problem.


    However, if the file truly is big, I rather recommend using memory mapping to read the file.

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

Sidebar

Related Questions

I have huge file (17 million lines) that contains content in this format: AB101XG,57.144165160000000|,-2.114847768000000|;
I have huge routes.rb file and I want to split into multiple manageable files.
I have huge amount of data from database, i need to get or store
I have a huge java codebase (more than 10,000 java classes) that makes extensive
I have a huge text file and I wanted to write a program which
I have a huge C file (~100k lines) which I need to be able
I don't know whether this question makes sense or not. I have huge amount
I have a huge data frame. One column is an integer ranging from 1
I have huge text files-- each file is as big as 10 GB. Now,
I have huge query on my SQL 2005 Server. This have to be run

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.