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

The Archive Base Latest Questions

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

I keep getting a seg fault if and only if I attempt to close

  • 0

I keep getting a seg fault if and only if I attempt to close a file:

   FILE *outFilePtr = fopen(*(argv + 2), "w"); //open file, yes i'm sure it opens


   fclose(outFilePtr); //sometime later in the program.

The program runs from start to finish without the flcose(). Any suggestions?

The error on gdb redirects here: Assume it is a function with all variables declared.
Also gdb blames strtol which I’m not even using.

 int t;
     char line[50];

          for (t = 0; t < lines; t++){
              fgets(line, 50, filePtr);
             strcpy(*string[t], strtok(line, " "));
              *(num1 + t) = atoi(strtok(NULL, " "));
              *(num2 + t) = atoi(strtok(NULL, " "));
           }

Memory Allocation Function

 void dynamicArray(int** num1, int** num2, char*** str, int size)
 { 
     int i = 0;

*(num1) = (int*)malloc(sizeof(int) * size);
*(num2) = (int*)malloc(sizeof(int) * size);

*(str) = (char**)malloc(sizeof(char*) * size);

for( i = 0; i < size; i++){
    *(*(str) + i) = (char*)malloc(sizeof(char) *size);
}

return;
 }
  • 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-19T00:39:42+00:00Added an answer on June 19, 2026 at 12:39 am

    Just to be sure, check that outFilePtr is not null:

    if (outFilePtr) {fclose(outFilePtr); outFilePtr = NULL;}
    

    I always do it when closing the file and I also put the pointer to NULL to avoid trying to close the same file twice (that may cause trouble as well).

    But most likely the cause is some memory leak or undefined behaviour that messes things around and segfault is just triggered by the fclose().

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

Sidebar

Related Questions

Keep getting The constructor SimpleCursorAdapter(MyProgram, int, Cursor, int) is undefined and am not sure
Im working on this bit of code and I keep getting a segmentation fault.
I keep getting a : HibernateException: No CurrentSessionContext configured! in my code. The only
I keep getting the following error when I dismiss the dictionary that opens from
I keep getting the following error: $ ./test.py -bash: ./test.py: cannot execute binary file
I keep getting this error when trying to open a connection using entity framework.
I keep getting the exception NoSuchElement relative to my scanning of the .txt file.
I keep getting a InvalidOperationException: ExecuteReader requires an open and available Connection. The connection's
I keep getting an error: unexpected end-of-file found and i am completely lost. I
int main( int argc, char** argv) { FILE *inFilePtr = fopen(*(argv + 1), r);

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.