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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:44:24+00:00 2026-06-11T05:44:24+00:00

I am having trouble getting some C code to run correctly (please bear with

  • 0

I am having trouble getting some C code to run correctly (please bear with me if this question is stupid, as I’m new to C; additionally, so everyone is clear, this is code I am having trouble running. The only part I’ve written is the null filehandle check). Basically, my program is crashing on fclose. Code first:

At the top of the file:

int *label;

Method specific:

void load_dat ()
{
  int    i, j, t, k;
  FILE   *in;
  char   t_file[16];

  printf ("\nName of Raw Data File > ");
  scanf ("%s", t_file);
  in = fopen (t_file, "r");
  if (in == NULL){
    perror("fopen error");
  }
 fscanf (in, "%d %d %d", &num_pats, &a_length, &b_length);

 dpt = (float **) malloc (sizeof(float *)*num_pats);

 for (k=0; k<num_pats; k++){
    dpt[k] = (float *) malloc (sizeof(float)*(a_length+b_length));
 }

 label = (int *) malloc (sizeof(int)*num_pats);

  for (i=0; i<num_pats; i++)
  {
      for (j=0; j<a_length; j++)
      {
        fscanf (in, "%f", &dpt[i][j]);
      }

  fscanf (in, "%d", &label[i]);


  if (label[i]<0 || label[i]>3)
    printf ("ERROR: Label corrupted.\n");

    for (t=0; t<b_length; t++){
        dpt[i][t+a_length] = 0.0;
        dpt[i][label[i]+a_length] = 1.0;
    }
  }
  fclose (in); 
}      

My error message from the program is: Abort trap: 6. Googling for this eventually led to the suggestion that I use GBD, which gave me:

Program received signal SIGABRT, Aborted.

And

#0  0x00007fff8c12582a in __kill ()
#1  0x00007fff871a3b6c in __abort ()  
#2  0x00007fff871a0070 in __stack_chk_fail ()
#3  0x000000010000175f in load_dat ()
#4  0x0000000100001baa in main ()
#5  0x00000001000013e4 in start ()

If I step through the entire method, the program doesn’t crash until I reach the final line fclose(in). Also, the value for in remains the same throughout the program.

Searching for problems with fclose, I came across this SO post, which led me to try using Valgrind, the output of which (using --leak-check=yes) is:

==22688== 
==22688== Process terminating with default action of signal 6 (SIGABRT)
==22688==    at 0x2DD82A: __kill (in /usr/lib/system/libsystem_kernel.dylib)
==22688==    by 0x18A06F: __stack_chk_fail (in /usr/lib/system/libsystem_c.dylib)
==22688==    by 0x10000175E: load_dat (in ./dataPre) 
==22688==    by 0x100001BA9: main (in ./dataPre)
==22688== 
==22688== HEAP SUMMARY:
==22688==     in use at exit: 28,781 bytes in 83 blocks
==22688==   total heap usage: 84 allocs, 1 frees, 32,877 bytes allocated
==22688== 
==22688== LEAK SUMMARY:
==22688==    definitely lost: 0 bytes in 0 blocks
==22688==    indirectly lost: 0 bytes in 0 blocks
==22688==      possibly lost: 0 bytes in 0 blocks
==22688==    still reachable: 28,781 bytes in 83 blocks
==22688==         suppressed: 0 bytes in 0 blocks
==22688== Reachable blocks (those to which a pointer was found) are not shown.
==22688== To see them, rerun with: --leak-check=full --show-reachable=yes
==22688== 
==22688== For counts of detected and suppressed errors, rerun with: -v
==22688== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Abort trap: 6

At this point I am at a loss as to where to look or what to do.

Sample data:

44  96  3                                                                                                                                                                                                                                                                                                                                                                                       
0   0   17.57298681 24.18012088 0   24.07599728 0   0   0   19.53417371 22.61467731 15.5650829  18.65720893 21.70631048 26.8811321  23.88086356 23.73544942 0   0   22.63088094 21.11777268 22.06847477 22.38688445 19.6794802  20.95594497 22.56472976 15.5058779  0   16.89366861 21.23974633 0   19.01608872 22.58492673 22.39564384 18.17000387 0   0   25.85404904 23.80483437 22.64271243 0   17.09819014 24.60634479 0   24.74696139 29.27117194 20.8931952  19.08648917 23.95167438 0   0   17.2386599  0   0   23.22304254 22.86712074 0   21.45687449 21.45146304 0   0   0   20.98717232 0   18.09871479 17.8226754  23.72508288 23.34563846 21.26201041 17.44038043 22.49848573 18.99848797 16.43222002 14.8132735  22.28093734 17.78931496 0   20.46914933 17.87742323 21.07936723 23.52102135 0   17.90498094 21.93199281 0   0   16.3020812  0   18.17972854 16.43234906 19.0756696  0   0   22.98048214 23.22184013 21.54024161 0

Note that num_pats refers to the number rows, a_length to the number of columns. b_length is the number of different input types (the last number of each row). There are 44 rows in my sample file.

  • 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-11T05:44:25+00:00Added an answer on June 11, 2026 at 5:44 am

    What is the size of string that gets loaded into t_file? You only allocate 16 bytes there…

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

Sidebar

Related Questions

I fear this is a very trivial question. But I'm having some trouble getting
I'm having some trouble getting this code to compile on Linux but it works
I'm having some trouble getting this layout to work correctly in IE. The problem
I'm having trouble getting a custom config section to work. It's some code I
I'm having some trouble getting a jQuery plugin to work correctly in my MVC
I have a problem with some excel code that I am having trouble getting
I'm having trouble getting Code Analysis to run on the build server. My goal
I am having trouble getting my code to run in a callback using the
I am having some trouble getting some code to work. Here it is: Imports
For some reason I'm having trouble getting my program to run a while loop.

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.