Thread: Bus Error (Core Dumped) with sscanf

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    8

    Bus Error (Core Dumped) with sscanf

    I have a C program with the following structs and main function.

    Code:
    struct Block {
      int tag;
      int valid;
    };
    
    
    struct Set {
        struct Block *blocks;
        int mru;
    };
      
    struct Cache {
        int BlockSize, NumSets, NumWays; // block size, number of sets, associativity
        struct Set *sets;
    
    };
     
    
    struct Cache *cache_init(const char *name, const char *config)
    {
    
        struct Cache *c = (struct cache_t *)malloc(sizeof(struct cache_t));
    
        if (sscanf(config, "%u:%u:%u", &c->bsize, &c->nsets, &c->nways) != 3) {
            fprintf(stderr, "%s configuration: none|<bsize>:<nsets>:<nways>\n", name);
            exit(-1);  }
        else if (c->BlockSize < 1 || !IS_POWEROF2(c->BlockSize)) {
            fprintf(stderr, "%s bsize (%u) must be positive and a power of 2\n",name, 
    
    c->BlockSize);        exit(-1);
        }
      
        else if (c->NumSets < 1 || !IS_POWEROF2(c->NumSets)) {
            fprintf(stderr, "%s sets (%u) must be positive and a power of 2\n", name, 
    
    c->NumSets);               
        exit(-1);
        }
      
        else if (c->NumWays < 1) {
            fprintf(stderr, "%s ways (%u) must be positive\n", c->Numways);
            exit(-1);
         }
      
        return c;
    
    }
    
    int main(int argc, char **argv) {  
      char buf[256];
      FILE *f = fopen("house.trace", "r");
    
      struct Cache *InstructionCache = NULL;
      struct Cache *DataCache = NULL;
      
    
      // check if program was called with the correct number of arguments
      // if not, print usage message and exit
    
      if (argc != 3) {
        fprintf(stderr, "usage: MyProgram none|<i-arg1>:<i-arg2>:<i-arg3> 
    
    none|i|<d-arg1>:<d-arg2>:<d-arg3>\n");
        exit(-1);
        }
     
      printf("argv[0] = %s\n", argv[0]) ;
      printf("argv[1] = %s\n", argv[1]) ;
      printf("argv[2] = %s\n", argv[2]) ;
     
      printf("strcmp(argv[1], none) = %d", strcmp(argv[1], "none")) ;
    
        
      if (strcmp(argv[1], "none") == 0) {
        icache = cache_init("icache", argv[1]);
      }
      
      else {
    
        InstructionCache = cache_init("icache", argv[1]) ;
        DataCache = cache_init("dcache", argv[2]) ;
          
      }
    
      /* More code */
    
    }

    The program gives a Bus error (core dumped) at the line:

    if (sscanf(config, "%u:%u:%u", &c->bsize, &c->nsets, &c->nways) != 3)

    Does anyone know how to fix this?

    I thank the solver in advance.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    But but but ... you don't have a bsize, nsets, or nways element in your struct. Also you have a lot of Cache's and some cache_t's. If somehow that's all fixed in your actual code (which I suppose it must be if it compiles?) then who knows.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    struct Cache *c = (struct cache_t *)malloc(sizeof(struct cache_t));
    do not cast malloc - read FAQ

    use

    Code:
    struct Cache *c = malloc(sizeof(*c));
    to avoid mix-up of types
    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

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You need to post the actual code which compiles, runs and crashes.

    Not some cut-down code, or worse, something recited from memory.
    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.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Cut down code is fine, as long as it can be immediately tested and really demonstrates the problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jul 2008
    Posts
    8

    Bus Error at scanf - Corrected code, malloc not casted but still getting error

    Sorry, the correct code should be:

    <code>


    Sorry, the code should be:


    Code:
    
    struct Block {
      int tag;
      int valid;
    };
    
    
    struct Set {
        struct Block *blocks;
        int mru;
    };
      
    struct Cache {
        int BlockSize, NumSets, NumWays; // block size, number of sets, associativity
        struct Set *sets;
    
    };
     
    
    struct Cache *cache_init(const char *name, const char *config)
    {
    
        struct Cache *c = (struct Cache*)malloc(sizeof(struct Cache));
    
        if (sscanf(config, "%u:%u:%u", &c->BlockSize, &c->NumSets, &c->NumWays) != 3) {
            fprintf(stderr, "%s configuration: none|<BlockSize>:<NumSets>:<NumWays>\n", name);
            exit(-1);  }
        else if (c->BlockSize < 1 || !IS_POWEROF2(c->BlockSize)) {
            fprintf(stderr, "%s BlockSize (%u) must be positive and a power of 2\n",name, 
    
    c->BlockSize);        exit(-1);
        }
      
        else if (c->NumSets < 1 || !IS_POWEROF2(c->NumSets)) {
            fprintf(stderr, "%s sets (%u) must be positive and a power of 2\n", name, 
    
    c->NumSets);               
        exit(-1);
        }
      
        else if (c->NumWays < 1) {
            fprintf(stderr, "%s ways (%u) must be positive\n", c->Numways);
            exit(-1);
         }
      
        return c;
    
    }
    
    int main(int argc, char **argv) {  
      char buf[256];
      FILE *f = fopen("house.trace", "r");
    
      struct Cache *InstructionCache = NULL;
      struct Cache *DataCache = NULL;
      
    
      // check if program was called with the correct number of arguments
      // if not, print usage message and exit
    
      if (argc != 3) {
        fprintf(stderr, "usage: MyProgram none|<i-arg1>:<i-arg2>:<i-arg3> 
    
    none|i|<d-arg1>:<d-arg2>:<d-arg3>\n");
        exit(-1);
        }
     
      printf("argv[0] = %s\n", argv[0]) ;
      printf("argv[1] = %s\n", argv[1]) ;
      printf("argv[2] = %s\n", argv[2]) ;
     
      printf("strcmp(argv[1], none) = %d", strcmp(argv[1], "none")) ;
    
        
      if (strcmp(argv[1], "none") == 0) {
        icache = cache_init("icache", argv[1]);
      }
      
      else {
    
        InstructionCache = cache_init("icache", argv[1]) ;
        DataCache = cache_init("dcache", argv[2]) ;
          
      }
    
      /* More code */
    
    }
    But still giving the afore-mentioned Bus-Error.

    I have tried vart's suggestion to not cast malloc , but I still get the Bus-Error.

  7. #7
    Registered User
    Join Date
    Jul 2008
    Posts
    8
    Here is the gdb output.


    run 4:16:2 4:16:2
    Starting program: cache 4:16:2 4:16:2
    Here

    After 3 arguments check.

    argc = 3

    argv[0] = cache
    argv[1] = 4:16:2
    argv[2] = 4:16:2

    Program received signal SIGSEGV, Segmentation fault.
    0xff311678 in number () from /usr/lib/libc.so.1

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You need to run a backtrace (bt) to see where in your code you actually are/were to do the debugging.

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you are using %u format for reading ints - use %d,
    you should check return values of malloc before using it

    add printfs traces to see where your program crashes... Do you use somewhere not initialized pointer Sets?
    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

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Apart from using %d instead of %u, it would probably help if you had an #include'd stdio.h, string.h, and stdlib.h in your source file. Without them, the compiler is making particular assumptions about the signatures of various functions which may or may not be valid.

    On a quick look, the error is occurring just after printing out argv[0] - argv[2]. The output from this line
    Code:
    printf("strcmp(argv[1], none) = %d", strcmp(argv[1], "none")) ;
    is not evident. That suggests (unless stdout is being buffered) the error is occurring earlier than the sccanf() call.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  11. #11
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Methinks! grumpy is on the right track so post the command used to invoke the program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sscanf error
    By roaan in forum C Programming
    Replies: 6
    Last Post: 08-01-2009, 06:44 AM
  2. Problems reading formatted input with sscanf
    By Nazgulled in forum C Programming
    Replies: 17
    Last Post: 05-10-2006, 12:46 AM
  3. sscanf()
    By task in forum C Programming
    Replies: 4
    Last Post: 11-22-2003, 04:43 PM
  4. Simple sscanf mystery
    By registering in forum C Programming
    Replies: 4
    Last Post: 06-10-2003, 11:47 PM
  5. sscanf (I think)
    By RyeDunn in forum C Programming
    Replies: 7
    Last Post: 07-31-2002, 08:46 AM