SIGSEGV, memory allocation/freeing problem

This is a discussion on SIGSEGV, memory allocation/freeing problem within the C Programming forums, part of the General Programming Boards category; Hello, I'm getting the following message in gdb: Program received signal SIGSEGV, Segmentation fault. 0x400cc1ce in _IO_flush_all () at genops.c:825 ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    6

    SIGSEGV, memory allocation/freeing problem

    Hello, I'm getting the following message in gdb:

    Program received signal SIGSEGV, Segmentation fault.
    0x400cc1ce in _IO_flush_all () at genops.c:825
    825 genops.c: No such file or directory.
    in genops.c

    when running the code given below.
    File main.c:
    Code:
    1:  int main(){
    2:    extern int *p;      //defined in main.h (not shown here)
    3:    extern int **a;     //dito line 2  
    4:
    5:    p=ivector(1,K);     //defined in nrutil.c from "Numerical Recipes in C" W.H.Press et al.
    6:    p=read_p();         //prototype in main.h
    7:    a=imatrix(1,K,1,N); //dito to comment on line 5
    8:    a=read_a();         //prototype in main.h
    9: 
    10:  //printing p and a here works fine.
    11:
    12:   free_ivector(p,1,K);     //dito to comment on line 5
    13:   free_imatrix(a,1,K,1,N); //dito to comment on line 5 
    14:   return 0; 
    15: }
    the functions are given in another read_file.c (and probably not relevant to the problem?).
    File read.c:
    Code:
    int *read_p(void){
     
      char line[4];
      FILE *file;
      int i=1;
      extern int *p;
     
      file = fopen("data_p.dat","r");
    
      while(fgets(line,sizeof(line),file)!=NULL)
        sscanf(line,"%i", &p[i++]);
    
      free(file); 
      return p;
    }
    
    int **read_a(void){
    
      char line[4];
      FILE *file;
      int i=1, j=1;
      extern int **a;
    
      file = fopen("data_a.dat","r");
    
      while(fgets(line,sizeof(line),file)!=NULL){
        sscanf(line,"%i", &a[i][j]);
        //appropriate incrementing of i and j guaranteed
      }
    
      free(file);
      return a;
    }
    When I comment out either lines 2, 5, 6 and 12 (vector p) or lines 3, 7, 8, and 13 (matrix a) in main.c, it works fine.
    But together, it seems there is some sort of endless loop running after line 14, that is, the code runs fine up to the return command.

    Thanks for your time looking into this and your comment!
    Michael

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Show the whole code as it's hard to know what variables "K" and "N" do and if the sigsegv isn't coming from ivector() or imatrix().

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,248
    You called free(file). I'm amazed it didn't crash there, but apparently it freed the block of memory taken by the FILE object. Later on during fclose() (probably when the program exits and it tried to close everything that's still open), it tried to free it again and crashed.

    Use fclose(file) instead of free(file)
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    fopen pairs with fclose not with free

    I do not see your allocating memory for p before writing there
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    6
    problem solved.
    I was blind for the last couple of days from the free* nrutil.c memory allocation/freeing functions I was dealing with a lot recently.
    'brewbuck' + 'vart': quite rightly, fopen() pairs with fclose() and not free()!

    I assume, if only one of the functions read_x(void), x=p or a, is invoked, main() can handle one wild pointer.
    However, as soon as both read_x functions are invoked, there's two FILE* pointers going around.

    Thanks for your help!

    ps1: itCbitC: K and N are integers. They determine the size of the vector or matrix.
    ps2: I consider this thread as closed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with linked list and shared memory
    By Sirfabius in forum C Programming
    Replies: 10
    Last Post: 11-10-2008, 03:45 PM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  4. Memory problem...?
    By Xzyx987X in forum Windows Programming
    Replies: 4
    Last Post: 06-30-2004, 05:02 PM
  5. Program abort due to memory problem
    By cazil in forum C++ Programming
    Replies: 5
    Last Post: 01-21-2002, 11:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21