Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C postgreSQL客户端(libpq)中的分段错误_C_Debugging_Postgresql_Segmentation Fault_Libpq - Fatal编程技术网

C postgreSQL客户端(libpq)中的分段错误

C postgreSQL客户端(libpq)中的分段错误,c,debugging,postgresql,segmentation-fault,libpq,C,Debugging,Postgresql,Segmentation Fault,Libpq,我正在使用libpq开发postgreSQL客户端应用程序。gcc编译它时没有错误。但是,当我运行程序时,此函数: void print_column(PGconn *connection) { PGresult *id_list; int row; // process_query() calls PQexec(), checks for errors, then returns the result id_list = process_query(conn

我正在使用libpq开发postgreSQL客户端应用程序。gcc编译它时没有错误。但是,当我运行程序时,此函数:

void print_column(PGconn *connection) {

    PGresult *id_list;
    int row;

    // process_query() calls PQexec(), checks for errors, then returns the result
    id_list = process_query(connection, "SELECT id FROM customers WHERE state='CA'");

    for(row = 0; row < PQntuples(id_list); row++) {
        printf("%s\n", PQgetvalue(id_list, row, 0));

        // to pause the loop on each rep for debugging
        getchar();
    }
}
奇怪的是for循环在前五次重复中没有出现问题。然后在第六节导致分段错误


我没有发布整个程序,因为它有1000多行。任何建议都将不胜感激。

这不会导致分段错误,但您发布的代码中存在错误

printf("%i\n", PQgetvalue(id_list, row, 0));
PQgetvalue
函数返回一个
char*
,但您将其打印为一个
int
。GCC和Clang都有警告,它们会抱怨类型不匹配,我建议
-Wall-Wextra
或者至少
-Wall
。您可以使用
-Wformat
仅打开格式不匹配警告。(此错误几乎肯定不会导致分段错误,但会导致不正确的输出。)


但是
PQgetvalue
不应该导致分段错误(您没有调用
PQclear
,对吧?)。程序中的其他地方可能存在损坏内存的错误。您可以使用Valgrind等工具来尝试检测此类错误。1000行并不长,这样的问题是难以解决的,您还可以使用
#ifdef 0
..
#endif
注释各个部分,直到错误停止,如果您感到绝望。

程序中的%i是%s。我在格式化方面遇到了问题,在发布之前重写了那个部分。我将尝试给出您的调试建议,并重新检查我的代码。我会通知你的。
printf("%i\n", PQgetvalue(id_list, row, 0));