At 6:42 PM -0700 5/17/1999, Yury Bukhman wrote:
>I am trying to create a simple MySQL client program in C. I'm getting a
>runtime "segmentation fault" error. I could not figure out why. Please,
>help!
>
>____
>Here is gdb diagnostic:
>(command: gdb programname core)
>Program terminated with signal 11, Segmentation fault.
>find_solib: Can't read pathname for load map: Input/output error
>
>#0 0x8049f4d in mysql_real_connect ()
>
>____
>Here is the relevant part of my program:
>
>#include <stdio.h>
>#include "mysql.h"
>
>int main(){
> MYSQL *mysql; /*server handle*/
> char *host = "localhost"; /*hostname or an IP address*/
> char *user = "guest"; /*user ID*/
> char *passwd = NULL; /*password*/
> char *db = "dom_base"; /*database name*/
> unsigned int port = 0; /*TCP-IP port*/
> char *unix_socket = "/tmp/mysql.sock"; /*UNIX socket for mysql
>connection*/
> unsigned int client_flag = 0;
> char *query;
> MYSQL_RES *result;
> MYSQL_ROW row;
>
> char *id, *method; /*pdb table field values*/
> float resoln, r; /*pdb table field values*/
>
>
> if(mysql_init(mysql)==NULL){ /*initialize server handle*/
> printf("mysql_init failed\n");
> return 1;
> }
> if(mysql_real_connect(mysql, host, user, passwd, db, port,
> unix_socket, client_flag)==NULL){
>(at this statement, program crushes)
Not surprising! You're passing a pointer to a MYSQL structure to
mysql_init() that doesn't point to anything useful. You're supposed
to pass a pointer to an already-allocated structure.
Try changing this:
MYSQL *mysql;
to this:
MYSQL mysql_struct, *mysql = &mysql_struct;
Another way of doing this would be to let mysql_init() allocate
the structure for you by passing NULL. But then you need to save
the return value. You could do this like so:
MYSQL *mysql;
...
if((mysql = mysql_init(mysql))==NULL){ /*initialize server handle*/
printf("mysql_init failed\n");
return 1;
}
...
--
Paul DuBois, paul@stripped
Northern League Chronicles: http://www.snake.net/nl/