pg.ddx.io pgsql-interfaces@postgresql.org mailing list archive
help / color / mirror / Atom feedSeg fault on completing query
3+ messages / 3 participants
[nested] [flat]
* Seg fault on completing query
@ 2003-03-13 15:28 Chris Jewell <vs0u8055@liv.ac.uk>
0 siblings, 2 replies; 3+ messages in thread
From: Chris Jewell @ 2003-03-13 15:28 UTC (permalink / raw)
To: pgsql-interfaces
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 <stdio.h>
#include <libpq-fe.h>
#include <stdlib.h>
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]$
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Seg fault on completing query
@ 2003-03-13 15:36 Gerhard Häring <haering_postgresql@gmx.de>
parent: Chris Jewell <vs0u8055@liv.ac.uk>
1 sibling, 0 replies; 3+ messages in thread
From: Gerhard Häring @ 2003-03-13 15:36 UTC (permalink / raw)
To: pgsql-interfaces
Chris Jewell <vs0u8055@liv.ac.uk> wrote:
> 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?
This is really a C/Linux/gcc question. I haven't looked at your source, so
just some general remarks:
You'll want to compile your program with "-g" as an additional compiler
switch. Then you also want core dumps to be written to disk
"ulimit -c unlimited" in your shell.
You can then analyze the core dump using gdb:
$ gdb /path/to/your/app
gdb> core /path/to/corefile # usually just 'core'
gdb> bt # short for 'backtrace'
You'll then see where your program crashed.
Alternatively, run your app under gdb (or a GUI frontend like ddd/gvd/...)
in the first place to see where it crashes.
HTH,
-- Gerhard
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Seg fault on completing query
@ 2003-03-13 17:14 Jeroen T. Vermeulen <jtv@xs4all.nl>
parent: Chris Jewell <vs0u8055@liv.ac.uk>
1 sibling, 0 replies; 3+ messages in thread
From: Jeroen T. Vermeulen @ 2003-03-13 17:14 UTC (permalink / raw)
To: Chris Jewell <vs0u8055@liv.ac.uk>; +Cc: pgsql-interfaces
On Thu, Mar 13, 2003 at 03:28:20PM +0000, Chris Jewell wrote:
> char *name;
[...]
> sprintf(name, "%s", PQgetvalue(res, i, 0));
[...]
So where do you allocate something for 'name' to point to?
Jeroen
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2003-03-13 17:14 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2003-03-13 15:28 Seg fault on completing query Chris Jewell <vs0u8055@liv.ac.uk>
2003-03-13 15:36 ` Gerhard Häring <haering_postgresql@gmx.de>
2003-03-13 17:14 ` Jeroen T. Vermeulen <jtv@xs4all.nl>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox