Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.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/4/postgresql/11.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/7/vb6/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-将结构的固定大小向量转换为动态分配_C_Postgresql_Vector_Struct - Fatal编程技术网

C-将结构的固定大小向量转换为动态分配

C-将结构的固定大小向量转换为动态分配,c,postgresql,vector,struct,C,Postgresql,Vector,Struct,在下面的ANSI C代码中,如何将向量conns[]从固定大小转换为动态分配(即,可能通过使用malloc()和free()函数) pg_conn是在/src/interfaces/libpq/libpq-int.h中找到的结构: typedef struct pg_conn PGconn; struct pg_conn { char *pghost; char *pghostaddr; char *pgport; char *pgunixsocket;

在下面的ANSI C代码中,如何将向量
conns[]
从固定大小转换为动态分配(即,可能通过使用
malloc()
free()
函数)

pg_conn是在
/src/interfaces/libpq/libpq-int.h
中找到的结构:

typedef struct pg_conn PGconn;
struct pg_conn
{
    char *pghost;
    char *pghostaddr;
    char *pgport;
    char *pgunixsocket;
    ...
};
尽管大小固定,但上面的代码仍能成功运行。可以使用以下指令编译它(需要PostgreSQL源代码):


您不必做太多更改,只需使用
calloc

PGconn** conns = calloc(MAX_DATABASES, sizeof(PGConn *));
最后记得
free(conns)


您不需要
memset()
,因为
calloc()
将使用
0
s初始化数组。

您可以这样做

PGconn **connections;
size_t number_of_connections;

number_of_connections = 10; // Do not exceed max_connections 
                            // from postgresql.conf 
                            // (default 100)
connections = malloc(number_of_connections * sizeof(*connections));
if (connections == NULL)
    return -1; // Allocation error, cannot continue
for (size_t i = 0 ; i < number_of_connections ; ++i)
    connections[i] = PQconnectdb("dbname=template1");
// Do whatever you want with connections, and free
for (size_t i = 0 ; i < number_of_connections ; ++i)
    PQfinish(connections[i]);
free(connections);
PGconn**连接;
连接的大小和数量;
连接数=10;//不要超过最大连接数
//来自postgresql.conf
//(默认值为100)
连接数=malloc(连接数*sizeof(*连接数));
if(连接==NULL)
返回-1;//分配错误,无法继续
对于(大小i=0;i<连接数;++i)
连接[i]=PQconnectdb(“dbname=template1”);
//通过连接做任何你想做的事,而且是免费的
对于(大小i=0;i<连接数;++i)
PQ饰面(连接件[i]);
免费(连接);

您不需要将所有指针设置为
NULL
,如果
PQconnectdb()
失败,它们将自动设置,因此您可以在尝试使用连接之前进行检查。

您还没有尝试任何操作,对吗?这是为什么?我尝试了一些
malloc()
ing,但没有成功,主要是因为缺乏ANSI C方面的经验。不幸的是,似乎没有LIBPQ处理多个连接的例子(我有一个连接管理器,可以在连接可用时(或连接池)选择连接)如果您预先输入并使用多线程处理多个连接。编写并不困难,只需了解和pthread的基本知识。也许这就是为什么没有太多示例的原因。如果您愿意,我可以将代码发送给您。请在hollidays之后,您可以在这里与我联系iharob@gmai.com.联系方式也在我的个人资料上。谢谢,@iharob!我很欣赏这段代码。事实上,昨天我研究了pthreads并用它开发了一段代码。唯一的问题是我使用了固定大小的向量:[]No
memset()
calloc()
是必需的。因为不需要将数组初始化为
0
。@iharob:最好习惯于初始化,然后关注微优化,当你无法应用它们时,可能会导致细微错误。在这种情况下,它几乎是免费的,因为你只需要使用calloc而不是malloc。不是,bec因为它可能隐藏代码中的一个bug。此外,考虑例如“代码> CalOrthe”(<代码> > ING或<代码> MeMSET()>代码>一个用于字符串的缓冲区,您的算法可能有bug,但是程序将完全执行。编译时引发了一些错误:
pqc.c:在函数“main”中:pqc.c:34:30:错误:对不完整类型“PGconn”pqc的“sizeof”应用无效。c:39:3:错误:对未定义类型“struct pg_conn”pqc的使用无效。c:39:8:错误:取消对不完整类型的指针的引用
@iharob你真的在争论我在哪里是否放置指针说明符?这是没有意义的。这是一个偏好问题,不存在强制标准。由于
空闲(连接[i]);
,程序会引发内核转储。
空闲(连接);
是否足以释放
malloc()中分配的内存
?这还不够。当然它会崩溃。请稍等。抱歉,请查看编辑。我没有注意到
connections[I]
不是从
malloc()
分配的。由于您使用
PQconnectdb()
创建连接,因此必须使用
PQfinish()正确关闭连接
或者你可能让连接处于“活动”状态,然后程序的后续运行将失败。顺便说一句:现在我明白了为什么会投反对票。但最好是发表评论,以便修复答案,而不是将错误留给未来的读者。如果OP没有尝试代码,我不会注意到错误。
PGconn** conns = calloc(MAX_DATABASES, sizeof(PGConn *));
PGconn **connections;
size_t number_of_connections;

number_of_connections = 10; // Do not exceed max_connections 
                            // from postgresql.conf 
                            // (default 100)
connections = malloc(number_of_connections * sizeof(*connections));
if (connections == NULL)
    return -1; // Allocation error, cannot continue
for (size_t i = 0 ; i < number_of_connections ; ++i)
    connections[i] = PQconnectdb("dbname=template1");
// Do whatever you want with connections, and free
for (size_t i = 0 ; i < number_of_connections ; ++i)
    PQfinish(connections[i]);
free(connections);