C Segmenation fault

This is a discussion on C Segmenation fault within the C Programming forums, part of the General Programming Boards category; Hello, Does sombody know what could be wrong with the following code: It compiles alright but when i run the ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    1

    C Segmenation fault

    Hello,
    Does sombody know what could be wrong with the following code:
    It compiles alright but when i run the binary file it give's a Segmentation fault.

    Code:
    #include "/usr/local/mysql/include/mysql/mysql.h"
    #include <stdio.h>
    #include <string.h>
    
    main() {
    
       MYSQL *subconn;
       MYSQL *domeinconn;
       MYSQL_RES *sub;
       MYSQL_RES *domein;
       MYSQL_ROW subrow;
       MYSQL_ROW domeinrow;
    
    
       char *server = "xxx";
       char *user = "xxx";
       char *password = "xxx";
       char *database = "xxx";
    
       subconn = mysql_init(NULL);
       domeinconn = mysql_init(NULL);
    
    
       if (!mysql_real_connect(subconn, server,
             user, password, database, 0, NULL, 0)) {
          fprintf(stderr, "%s\n", mysql_error(subconn));
          exit(0);
       }
    
     if (!mysql_real_connect(domeinconn, server,
            user, password, database, 0, NULL, 0)) {
          fprintf(stderr, "%s\n", mysql_error(domeinconn));
          exit(0);
       }
    
       /* send SQL query */
       if (mysql_query(subconn, "SELECT * FROM table1")) {
          fprintf(stderr, "%s\n", mysql_error(subconn));
          exit(0);
       }
    
       sub = mysql_use_result(subconn);
       domein = mysql_use_result(domeinconn);
    
    
       while ((subrow = mysql_fetch_row(sub)) != NULL) {
    
            if(mysql_query(domeinconn, "SELECT domein_naam FROM table2 WHERE domein_id=3")) {
                    fprintf(stderr, "%s\n", mysql_error(domeinconn));
                    exit(0);
            }
    
            while ((domeinrow = mysql_fetch_row(domein) != NULL) {
                    printf("%s%s\n", domeinrow[0], subrow[2]);
            }
       }
    
       mysql_close(domeinconn);
       mysql_close(subconn);
    }
    Greetz
    Last edited by OrbiT^; 04-19-2005 at 12:25 PM.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Try compiling it with the -g option and then when it seg faults and dumps a core you can use gdb to see which line it's crashing on.
    If you understand what you're doing, you're not learning anything.

  3. #3
    SleepWalker tjohnsson's Avatar
    Join Date
    Apr 2004
    Posts
    70
    Store result before reading from it.

    Code:
            if(mysql_query(domeinconn, "SELECT domein_naam FROM table2 WHERE domein_id=3")) {
                    fprintf(stderr, "%s\n", mysql_error(domeinconn));
                    exit(0);
            }
    
            domein = mysql_use_result(domeinconn); 
    
            while ((domeinrow = mysql_fetch_row(domein) != NULL) {
                    printf("%s%s\n", domeinrow[0], subrow[2]);
            }
    -- Add Your Signature Here --

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    > while ((domeinrow = mysql_fetch_row(domein) != NULL)
    Since this doesn't even compile (mismatched parens), we've no idea whether this is your real code or something you just managed to post by pressing random keys.

    > printf("%s%s\n", domeinrow[0], subrow[2]);
    Validate the call to make sure those array elements exist before you try and access them
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. weird seg fault
    By Vermelho in forum C Programming
    Replies: 3
    Last Post: 05-10-2008, 08:27 PM
  2. Segmenation Fault
    By sunoflight77 in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2005, 12:07 AM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 02:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM
  5. segmenation fault when executing
    By v3dant in forum C Programming
    Replies: 2
    Last Post: 11-15-2004, 03:07 PM

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