SIGSEGV, Segmentation fault

This is a discussion on SIGSEGV, Segmentation fault within the C Programming forums, part of the General Programming Boards category; Here is my initiating code: Code: #define NUM 20 static struct tab { int count; int use; } tab[NUM]; int ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    12

    SIGSEGV, Segmentation fault

    Here is my initiating code:
    Code:
    #define NUM 20
    
    static struct tab {
    	int count;
    	int use;
    } tab[NUM];
    
    int curtab = 0;
    
    int tab_create(int tab_count)
    {
    	curtab++;
    	tab[curtab].use = 1;
    	tab[curtab].count = tab_count;
            kprintf("here!");
    	return curtab;
    }
    When I run my program main() which calls intval = tab_create(1); I receive this error
    Code:
    here!Exception 13 at EIP=00000003: General protection violation
    I used GDB for debugging and stepped through the process and received this output:
    Code:
    main () at prodcons.c:39
    39          intval = tab_create(1);
    (gdb) s
    
    Program received signal SIGSEGV, Segmentation fault.
    0x3 in ?? ()

    Any ideas why this is a seg-fault?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You'll probably need to supply more information than that. Just as a guess, perhaps you should return curtab-1?

    Also make sure that curtab doesn't exceed NUM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    If you have a main() which just calls that, and nothing more, does it still work?

    If you use regular printf() rather than kprintf() - assuming you're not actually writing kernel level modules being called from user-level programs, does it work?

    It's a hell of a lot easier for us if you post something we can just copy/paste/compile and observe what happens, than it is to guess at what random bit of information you've left out.
    Like is it in a loop and you've called it too many times?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Prying open my third eye.
    Join Date
    Jun 2005
    Posts
    45
    Maybe he is incrementing curtab before he should, because it is accessing the array of structs starting at index 1.
    "So you're one of those condescending UNIX computer users?"

    "Here's a nickel, kid. Get yourself a better computer."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 02:29 PM
  2. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM
  3. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM
  4. debugging: sigsegv, segmentation fault???
    By Gonzo in forum C Programming
    Replies: 9
    Last Post: 09-16-2003, 06:56 AM
  5. SIGSEGV, segmentation fault
    By StuBarnes4Prez in forum C++ Programming
    Replies: 4
    Last Post: 01-04-2002, 08:26 PM

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