Thread: SIGSEGV, memory allocation/freeing problem

  1. #1
    Registered User
    Join Date
    Feb 2009

    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:
    1:  int main(){
    2:    extern int *p;      //defined in main.h (not shown here)
    3:    extern int **a;     //dito line 2  
    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
    10:  //printing p and a here works fine.
    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:
    int *read_p(void){
      char line[4];
      FILE *file;
      int i=1;
      extern int *p;
      file = fopen("data_p.dat","r");
        sscanf(line,"%i", &p[i++]);
      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");
        sscanf(line,"%i", &a[i][j]);
        //appropriate incrementing of i and j guaranteed
      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!

  2. #2
    Registered User
    Join Date
    Oct 2008
    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
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Portland, OR
    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)
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Rishon LeZion, Israel
    fopen pairs with fclose not with free

    I do not see your allocating memory for p before writing there
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  5. #5
    Registered User
    Join Date
    Feb 2009
    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, 04: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, 12:55 PM