Solved

Dumb C pointer error trying to query MySql - segmentation fault

Posted on 2012-03-22
6
809 Views
Last Modified: 2012-06-21
My C skills are obviously rusty.  I'm getting a segmentation fault on the mysql_query call -- line 21 in the snippet below.

void OpenConnection(MYSQL* conn, char* sDB){
    conn = mysql_init(NULL);
   /* Connect to database */
   if (!mysql_real_connect(conn, server,
         user, password, sDB, 0, "/var/run/mysqld/mysqld.sock", 0)) {
      fprintf(stderr, "%s\n", mysql_error(conn));
      exit(1);
   }
}
main ( int argc, char** argv){
    
   MYSQL *conn;
   MYSQL_RES *res;
   MYSQL_ROW row;
    char* dateSql;
    
    //find the DB's to update
    OpenConnection(conn, "wb_manage");
    
   /* send SQL query */
   if (mysql_query(conn, "select distinct DB from Site where Active=1")) {
      fprintf(stderr, "%s\n", mysql_error(conn));
      exit(1);
   }
...
}

Open in new window


Can you tell what I'm messing up?  I suspect it has to do w/ *'s or &'s.

Thanks!
0
Comment
Question by:Daniel Wilson
  • 3
  • 2
6 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
ID: 37753793
Passing the pointer 'by value' causes that problem, the varaible will be unchanged (== uninitialized) after 'OpenConnection()' returns - make that either

void OpenConnection(MYSQL** conn, char* sDB){
    *conn = mysql_init(NULL);
   /* Connect to database */
   if (!mysql_real_connect(conn, server,
         user, password, sDB, 0, "/var/run/mysqld/mysqld.sock", 0)) {
      fprintf(stderr, "%s\n", mysql_error(conn));
      exit(1);
   }
}
main ( int argc, char** argv){
    
   MYSQL *conn;
   MYSQL_RES *res;
   MYSQL_ROW row;
    char* dateSql;
    
    //find the DB's to update
    OpenConnection(&conn, "wb_manage");
    
   /* send SQL query */
   if (mysql_query(conn, "select distinct DB from Site where Active=1")) {
      fprintf(stderr, "%s\n", mysql_error(conn));
      exit(1);
   }
...
}
                                  

Open in new window


or

MYSQL* OpenConnection(, char* sDB){
    MYSQL* conn = mysql_init(NULL);
   /* Connect to database */
   if (!mysql_real_connect(conn, server,
         user, password, sDB, 0, "/var/run/mysqld/mysqld.sock", 0)) {
      fprintf(stderr, "%s\n", mysql_error(conn));
      exit(1);
   }

  return conn;
}
main ( int argc, char** argv){
    
   MYSQL *conn;
   MYSQL_RES *res;
   MYSQL_ROW row;
    char* dateSql;
    
    //find the DB's to update
    conn = OpenConnection("wb_manage");
    
   /* send SQL query */
   if (mysql_query(conn, "select distinct DB from Site where Active=1")) {
      fprintf(stderr, "%s\n", mysql_error(conn));
      exit(1);
   }
...
}
                                  

Open in new window

0
 
LVL 7

Assisted Solution

by:Slimfinger
Slimfinger earned 200 total points
ID: 37753827
You are passing the MYSQL pointer by value into "OpenConnection" so you are losing scope on the memory; pass it by reference instead:

OpenConnection(&conn, "wb_manage");

void OpenConnection(MYSQL** conn, char* sDB)
{
    *conn = mysql_init(NULL);

   /* ...etc.*/
}
0
 
LVL 86

Expert Comment

by:jkr
ID: 37753837
Um, isn't that what I wrote? And your example isn't really fixing it...
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 7

Expert Comment

by:Slimfinger
ID: 37753855
LOL - jkr was faster!

Anyhow, just thought I'd point out that you may need to de-reference the pointer in mysql_real_connect as well (I think jkr missed that in his example).
0
 
LVL 7

Expert Comment

by:Slimfinger
ID: 37753879
Um, I never saw your post (I started my reply before yours was entered).

As far as "not fixing it" - I told him what was wrong.  Do I need to write every line?  But if you want to be anal about it, then you may want to fix the potential overwrite you are causing on line 4 of your fix.
0
 
LVL 32

Author Comment

by:Daniel Wilson
ID: 37753929
A combination of those solutions worked.  thanks!
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem with SqlConnection 4 168
Acrinis True image 2 77
sql_mode 1 24
NEED HELP WITH VISUAL STUDIO 2017 (beginner) 6 38
I use MySQL for many of my development projects in a Windows environment. To manage my databases (and perform queries) for years I used a tool called MySQL administrator.  This tool has since been replaced by MySQL Workbench. So I decided to m…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question