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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:05:15+00:00 2026-06-15T10:05:15+00:00

I’m trying to sort an array whose elements are read from a file which

  • 0

I’m trying to sort an array whose elements are read from a file which is approximately 5 GB in size and contains approximately 500000 data elements.

After a data size of 300.000.000, the program gives an error during sorting due to segmentation fault and terminates.

I think the problem occurs due to insufficient memory space allocated to program. How can I change it in my C code?

Could you help me about this? Thank you.

int arraysize = atoi(argv[1]);
int* array    = malloc(sizeof(int)*arraysize);
int* temp     = malloc(sizeof(int)*arraysize);
int i;

FILE *fi;
char buffer[20];
fi = fopen("DATASET.dat", "r");

for(i=0; i<arraysize; i++){
  fgets(buffer, 20, fi);
  array[i] = atoi(buffer);
}

fclose(fi);

//function is called to perform the sorting
mergesort_array(array, arraysize, temp);
  • 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-15T10:05:16+00:00Added an answer on June 15, 2026 at 10:05 am

    int* array = malloc(sizeof(int)arraysize);
    int
    temp = malloc(sizeof(int)*arraysize);

    In general, whenever you allocate memory, check that the allocation succeeded:

    int *array = NULL, *temp = NULL;
    
    if (NULL == (array = malloc(sizeof(int)*arraysize)))
    {
        fprintf(stderr, "Out of memory allocating %d bytes\n", sizeof(int)*arraysize);
        abort();
    }
    if (NULL == (temp = malloc(sizeof(int)*arraysize)))
    {
        fprintf(stderr, "Out of memory allocating %d bytes\n", sizeof(int)*arraysize);
        abort();
    }
    

    Then, a possibility would be to implement mergesorting on disk, using a file of integers (you can mmap() the file, too).

    But I find it strange that an allocation of 300000 integers on the heap – 4.8 megabytes at the most, using 64-bit integers – can cause an allocation error, so I think this is something in the mergesort implementation; maybe something having to do with a recursive implementation.

    I’d start with compiling the program with full debug information, and checking the core dump with gdb.

    A “simple” malloc problem

    Having to handle a very large array of ASCII strings representing numbers, you could start by first converting it to a file of integers.

    FILE *fi, *fo, *ft;
    char buffer[20];
    int  array[4096], b = 0;
    
    fi = fopen("DATASET.dat", "r");
    if (NULL == fi)
    {
        fprintf(stderr, "Cannot open input file\n");
        abort();
    }
    fo = fopen("INTEGER.dat", "w");
    if (NULL == fo)
    {
        fprintf(stderr, "Cannot open output file\n");
        abort();
    }
    ft = fopen("TEMP.dat", "w");
    if (NULL == ft)
    {
        fprintf(stderr, "Cannot open output file\n");
        abort();
    }
    for(i=0; i<arraysize; i++){
       fgets(buffer, 20, fi);
       array[b++] = atoi(buffer);
       if (4096 == b)
       {
           if (b != fwrite(buffer, sizeof(int), b, fo))
           {
               fprintf(stderr, "write error\n");
               abort();
           }
           if (b != fwrite(buffer, sizeof(int), b, ft))
           {
               fprintf(stderr, "write error\n");
               abort();
           }
           b = 0;
       }
    }
    if (b)
    {
        if (b != fwrite(buffer, sizeof(int), b, fo))
        {
            fprintf(stderr, "write error\n");
            abort();
        }
        if (b != fwrite(buffer, sizeof(int), b, ft))
        {
            fprintf(stderr, "write error\n");
            abort();
        }
    }
    fclose(fi); fi = NULL;
    fclose(fo); fo = NULL;
    fclose(ft); ft = NULL;
    

    Now you have an INTEGER.dat file which is made of integers of fixed size. It is, to all intents and purposes, a file copy of an array in memory. Same goes for the temporary array.

    And you can tell the system to treat that file as if it was an array in memory.

    int *sort = NULL;
    int *temp = NULL;
    
    // Temp is not shown -- identical treatment as sort
    
    fd = open ("INTEGERS.dat", O_RDWR);
    if (fd == -1)
    {
        fprintf(stderr, "cannot reopen output\n");
        abort();
    }
    if (MAP_FAILED == (sort = mmap (0, arraysize*sizeof(int), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0)))
    {
        fprintf(stderr, "mmap error\n");
        abort();
    }
    if (-1 == close (fd))
    {
        fprintf(stderr, "error closing output file\n");
        return 1;
    }
    
    do_sort(sort, temp, arraysize);
    
    if (-1 == munmap (sort, arraysize*sizeof(int)))
    {
        fprintf(stderr, "error releasing mmap for %s\n", "sort");
        abort();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I am trying to render a haml file in a javascript response like so:
I'm trying to select an H1 element which is the second-child in its group
I have an array which has BIG numbers and small numbers in it. I
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a text area in my form which accepts all possible characters from
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a reasonable size flat file database of text documents mostly saved in
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.