?
Solved

Dumb C pointer error trying to query MySql - segmentation fault

Posted on 2012-03-22
6
Medium Priority
?
875 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 800 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:Bill Nolan
Bill Nolan earned 800 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 7

Expert Comment

by:Bill Nolan
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:Bill Nolan
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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

752 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