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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:16:03+00:00 2026-05-25T01:16:03+00:00

I have two files, one is called N.bin and the other is called R.bin.

  • 0

I have two files, one is called N.bin and the other is called R.bin. After couple of months of using it, I just noticed that I have a mistake there. However, I thought the system would crash because of it. But first it didn’t and second it gives the correct result. Here is the code:

Please see Line 19 how I mistakenly streamed in from Nfile and not Rfile .

// Read file N

1       long world_features_lSize;
2       FILE* NFile;
3       double* N;
4       NFile=fopen("N.bin","r+b");
5   
6       fseek (NFile , 0 , SEEK_END);
7       lSize = ftell (NFile);
8       fseek (NFile , 0 , SEEK_SET);
9       N = (double*) malloc (sizeof(double)*lSize);
10      result = fread (N,1,lSize,NFile);
11      fclose(NFile);
    ////////////////// Read R

12      FILE* RFile;
13      double* R;
14      RFile=fopen("R.bin","r+b");

15      fseek (RFile , 0 , SEEK_END);
16      lSize = ftell (RFile);
17      fseek (RFile , 0 , SEEK_SET);
18      R = (double*) malloc (sizeof(double)*lSize);
19      result = fread (R,1,lSize,NFile);
20      fclose(RFile);

Kindly advice me why does this code work !!

  • 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-25T01:16:04+00:00Added an answer on May 25, 2026 at 1:16 am

    This is probably down to the way the C run time libraries handle memory allocation. fopen mallocs a buffer since it returns a FILE * object. fclose frees the buffer. The subsequent fopen will malloc a buffer the same size as before and it just happened to return the same memory the previous free released. If you compare the pointer values of R and N they will be the same.

    Note that if you had done any memory allocation \ freeing between lines 11 and 14, then the system would have crashed. Also, depending on how the debugger works and the run time, the free function can sometimes be made to not reuse the freed memory.

    To prevent this sort of bug in the future, always do this:

    fclose (handle);
    handle = 0; // so you get a null pointer error if you accidentally use it again
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created two JavaScript files. One file is "validators.js" and the other is
The situation is I have two xslt files: one is called from my ASP.NET
Hallo all, I have two files, one called part1.txt and another one called part2.txt,
i have two files one called stats.js one called storage.html in stats.js in contains
Suppose I have two text files. The first one, called reference.txt has the following
I have two nib files, one that will open at startup and another that
I have two files, one containing an array in PHP that is echo ed
So I have two files, one that contains my text, and another which I
I have two files one with senators who are retiring and one of complete
Say I have two files where there is one number per line File 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.