Big structure + file pointer = segmentation fault

This is a discussion on Big structure + file pointer = segmentation fault within the C Programming forums, part of the General Programming Boards category; Hi there, I need to use a big structure (fixed size), with multiple file operations. I could isolate the problem, ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    12

    Big structure + file pointer = segmentation fault

    Hi there,
    I need to use a big structure (fixed size), with multiple file operations.
    I could isolate the problem, contained inside the code below.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <ctype.h>
    #include <string.h>
    
    typedef struct library59k { 
      int numModel;         
      char combination[6];  
      double rmsd_ext;		
      double rmsdTotal; 
      double det;
      double coord[28][3]; 
    } lib; 
    
    int main()
    {
    
    lib member[59049];
    
    FILE *out;
    	
    	out = fopen("output", "w");
    	if (out==NULL)
    		{
    		printf("cant write file.\n");
    		exit(3);
    		}
    		
    return 0;
    }
    The program compiles, but when I execute it, I get segmentation fault error, unless that I reduce the size of the structure.
    Is there a size restriction for structures?
    I guess it probably has to do with the size of the coord[28][3] pointer.
    I'm using gcc 4.4 compiler.
    thanks in advance for any help.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,804
    Quote Originally Posted by ERJuanca View Post
    Is there a size restriction for structures?
    I guess it probably has to do with the size of the coord[28][3] pointer.
    I'm using gcc 4.4 compiler.
    thanks in advance for any help.
    No, it has to do with the 59049 elements you are trying to create on the stack. Assuming a struct of size 120 bytes (maybe 124?), creating that many of them yields about 6.7~6.8 Mb on the stack. If you need that many, then try dynamically allocating the array instead of putting it on the stack.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Local variables are instantiated on the stack, and most operating systems limit the amount of stack space available to a program. Try allocating from the heap instead (ie: malloc). Also, there are OS-specific API's that you can use to increase the size of the stack/heap.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    6 or 8 megs is not that much...

    I compiled this here and executed it, no problems. What kind of system is this on? Are you positive the code is exactly what you posted?

    [edit] sorry, it did after I put the file open back in.

    Quote Originally Posted by Sebastiani View Post
    Try allocating from the heap instead (ie: malloc).
    Also works if you make it global.
    Last edited by MK27; 03-02-2010 at 01:08 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    12
    I'm using Ubuntu 9.10. And yes, the code is exactly the same.
    But I made the declaration global, and it worked great!
    Next time I'll consider to dynamically alocate a big structure.
    thanks a lot,

    Juan Pablo

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by MK27 View Post
    6 or 8 megs is not that much...
    Considering you usually get 1-2MB, yes it is

    Code:
    zac@neux:~ (0) $ ulimit -s
    8192
    Make that 8KB...
    Last edited by zacs7; 03-02-2010 at 02:59 PM.

  7. #7
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by zacs7 View Post
    Considering you usually get 1-2MB, yes it is

    Code:
    zac@neux:~ (0) $ ulimit -s
    8192
    Make that 8KB...
    As far as I know ulimit prints the stack size in kbytes, not bytes. So that would be 8MB.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  3. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  4. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM

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