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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:45:51+00:00 2026-05-26T20:45:51+00:00

I try to realise an external merge sort ( wiki ) and I want

  • 0

I try to realise an external merge sort (wiki) and I want to open 2048 ifstreams and read data to personal buffers.

ifstream *file;
file = (ifstream *)malloc(2048 * sizeof(ifstream));

for (short i = 0; i < 2048; i++) {
    itoa(i, fileName + 5, 10);
    file[i].open(fileName, ios::in | ios::binary); // Access violation Error
    if (!file[i]) {
        cout << i << ".Bad open file" << endl;          
    }
    if (!file[i].read((char*)perfile[i], 128*4)) {
        cout << i << ". Bad read source file" << endl;
    }       
}

But, it crashes with

Unhandled exception at 0x58f3a5fd (msvcp100d.dll) in sorting.exe: 0xC0000005: Access violation reading location 0xcdcdcdfd.

Is it possible to use so much opened ifstreams?
Or maybe it is very bad idea to have 2048 opened ifstreams and there is a better way to realize this algorithm?

  • 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-26T20:45:51+00:00Added an answer on May 26, 2026 at 8:45 pm

    Arrays of non-POD objects are allocated with new, not with malloc, otherwise the constructors aren’t run.

    Your code is getting uninitialized memory and “interpreting” it as ifstreams, which obviously results in a crash (because the constructor of the class hasn’t been run not even the virtual table pointers are in place).

    You can either allocate all your objects on the stack:

    ifstream file[2048];
    

    or allocate them on the heap if stack occupation is a concern;

    ifstream *file=new ifstream[2048];
    // ...
    delete[] file; // frees the array
    

    (although you should use a smart pointer here to avoid memory leaks in case of exceptions)

    or, better, use a vector of ifstream (requires header <vector>):

    vector<ifstream> file(2048);
    

    which do not require explicit deallocation of its elements.

    (in theory, you could use malloc and then use placement new, but I wouldn’t recommend it at all)


    … besides, opening 2048 files at the same time doesn’t feel like a great idea…

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

Sidebar

Related Questions

Its when I try to do stuff like this I realise I really need
Say I had an open-source project which I wanted to try and generate some
I'm doing some file validation and want to load an UploadedFile into an external
I realise that the structure of the data in database I am working with
I try to store the modules data in a specific order inside a map.
I realise that this error happens when you attempt to do some sort of
I try to realize a link (in fact many links) that update a table
Try this code - import java.io.StringReader; public class StringReaderTest { public static void main(String[]
Try this piece of code - public class WhitespaceTest { public static void main(String[]
try { if (myBoolean) { while (true) ; } else { System.exit(1); } }

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.