Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Dumb C pointer error trying to query MySql - segmentation fault

Posted on 2012-03-22
6
Medium Priority
?
912 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 your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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 …
In this article, I’ll talk about multi-threaded slave statistics printed in MySQL error log file.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
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…

610 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