X-Original-To: pgsql-interfaces@postgresql.org Received: from mta06-svc.ntlworld.com (mta06-svc.ntlworld.com [62.253.162.46]) by postgresql.org (Postfix) with ESMTP id 720264763F3 for ; Thu, 13 Mar 2003 10:26:59 -0500 (EST) Received: from linux.chrisdomain ([81.97.70.38]) by mta06-svc.ntlworld.com (InterMail vM.4.01.03.37 201-229-121-137-20020806) with ESMTP id <20030313152703.BQR20605.mta06-svc.ntlworld.com@linux.chrisdomain> for ; Thu, 13 Mar 2003 15:27:03 +0000 Received: from liv.ac.uk (chris.chrisdomain [192.168.1.35]) by linux.chrisdomain (8.11.0/8.11.6) with ESMTP id h2DFQxO01831 for ; Thu, 13 Mar 2003 15:26:59 GMT Message-ID: <3E70A394.9080802@liv.ac.uk> Date: Thu, 13 Mar 2003 15:28:20 +0000 From: Chris Jewell Reply-To: vs0u8055@liv.ac.uk User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.0.2) Gecko/20021120 Netscape/7.01 X-Accept-Language: en-us, en MIME-Version: 1.0 To: pgsql-interfaces@postgresql.org Subject: Seg fault on completing query Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Archive-Number: 200303/64 X-Sequence-Number: 3813 Hi, I'm working on a frontend to a Postgresql database. I've chosen C in which to work, mainly because I wanted to learn the language. So, I'm using the pqlib frontend library. The C source I have is below. I compile it (on linux) with gcc -g -Wall -lpq -oantibug antibug.c. When I run the executable, it prints out the query to the screen ok, but also prints a "Segmentation Fault" on completion of the list. Does anyone know why? How can I prevent this? Thanks, Chris J Source: /************************************************************** * A test C program to perform a SELECT query on tblantibiotic* * on the bugswat database. * **************************************************************/ #include #include #include void exit_nicely(PGconn * conn) { PQfinish(conn); printf("Abnormal exit"); exit(1); } int main() { /* Declare variables for the connection and getting results */ int num_records; PGconn *connect; PGresult *res; int i; char *name; /* Connect to the database */ connect = PQconnectdb("host = linux.chrisdomain dbname=antibugdb user=cjewell"); if (PQstatus(connect) == CONNECTION_BAD) { fprintf(stderr, "Could not connect to database"); fprintf(stderr, "Error: %s\n", PQerrorMessage(connect)); exit_nicely(connect); } /* Perform our query */ res = PQexec(connect, "SELECT * FROM tblactivity;"); if ((!res) || (PQresultStatus(res) != PGRES_TUPLES_OK)) { fprintf(stderr, "SELECT query failed, error: %d\n", PQresultStatus(res)); fprintf(stderr, "Server error: %s\n", PQerrorMessage(connect)); PQclear(res); exit_nicely(connect); } num_records = PQntuples(res); /* put the number of tuples into num_records */ /* Now print out the results to the screen */ for(i=0; i < num_records; ++i) { sprintf(name, "%s", PQgetvalue(res, i, 0)); printf("%s\n", name); } PQclear(res); PQfinish(connect); return(0); } Output: [cjewell@chris cprogs]$ ./antibug good some poor none unknown Segmentation fault [cjewell@chris cprogs]$