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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:05:49+00:00 2026-05-20T14:05:49+00:00

I am running a simulation in C, and need to store 3 100×100 matrices

  • 0

I am running a simulation in C, and need to store 3 100×100 matrices ~1000 times. My program runs just fine when I’m not writing the data to file. But when I run my program and write the data, I get a segmentation error after 250 time steps or so. And I don’t understand why.

My save function looks like this

void saveData(Simulation* sim, int number) {
   sprintf(pathname_vx, "data/xvel%d.dat", number);
   sprintf(pathname_vy, "data/yvel%d.dat", number);
   sprintf(pathname_rho, "data/rho%d.dat", number);
   FILE* vx_File = fopen(pathname_vx, "w");
   FILE* vy_File = fopen(pathname_vy, "w");
   FILE* rho_File = fopen(pathname_rho, "w");
   int iX, iY;
   double ux, uy, rho;
   for (iY=0; iY<sim->ly; ++iY) {
       for (iX=0; iX<sim->lx; ++iX) {
           computeMacros(sim->lattice[iX][iY].fPop, &rho, &ux, &uy);
           fprintf(vx_File, "%f ", ux);
           fprintf(vy_File, "%f ", uy);
           fprintf(rho_File, "%f ", rho);
       }
       fprintf(vx_File, "\n");
       fprintf(vy_File, "\n");
       fprintf(rho_File, "\n");
   }
   fclose(vx_File);
   fclose(vx_File);
   fclose(vy_File);
}

where ‘Simulation’ is a struct containing a lattice (100×100 matrix) with 3 different variables ‘rho’, ‘ux’, ‘uy’. The ‘number’ argument is just a counting variable to name the files correctly.

gdb says the following, but it doesn’t help me much.

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000010
0x00007fff87c6ebec in __vfprintf ()

I’m not that experienced in programing, so I guess there are better ways to write data to file. Any attempt to clarify why my approach doesn’t work is highly appreciated.

Thanks

jon

  • 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-20T14:05:50+00:00Added an answer on May 20, 2026 at 2:05 pm

    Looks like you’re closing vx_File twice, and not closing rho_File at all. This means that you’re leaving rho_File open each iteration, and thus using up a file descriptor each time through.

    I’d guess the program fails you’re running out of file descriptors. (Since this happens on the 250’th iteration, I’d guess your limit is 256). Once you’re out of file descriptors, one of the fopen() calls will return NULL. Since you don’t check the return value of fopen(), the crash will occur when you attempt to fwrite to a NULL handle.

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

Sidebar

Related Questions

I'm running a long simulation in MATLAB that I've realized I need to stop
We currently use List<T> to store events from a simulation project we are running.
I have a situation where i need to send commands to a running java
I am running simulation code that is largely bound by CPU speed. I am
I am running a script that animates a plot (simulation of a water flow).
I have a multi-process .NET (F#) scientific simulation running on Windows Server 2008 SE
I have a Python program that runs a series of experiments, with no data
We're running a J2EE Application inside a Tomcat WebServer on AIX. We need to
I am writing some simulation code and managed to get my model running as
I need to import a very large dictionary into python and I'm running into

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.