Solved

Dumb C pointer error trying to query MySql - segmentation fault

Posted on 2012-03-22
6
780 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
Comment Utility
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
Comment Utility
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
Comment Utility
Um, isn't that what I wrote? And your example isn't really fixing it...
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 7

Expert Comment

by:Slimfinger
Comment Utility
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
Comment Utility
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
Comment Utility
A combination of those solutions worked.  thanks!
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

All XML, All the Time; More Fun MySQL Tidbits – Dynamically Generate XML via Stored Procedure in MySQL Extensible Markup Language (XML) and database systems, a marriage we are seeing more and more of.  So the topics of parsing and manipulating XM…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now