Solved

Dumb C pointer error trying to query MySql - segmentation fault

Posted on 2012-03-22
6
863 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 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:Bill Nolan
Bill Nolan 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

688 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