gpt4 book ai didi

c - 如何在c中有效地构建字符串?

转载 作者:太空宇宙 更新时间:2023-11-04 00:37:56 25 4
gpt4 key购买 nike

我一直在从事一个项目,从一些日志文件中收集记录,并将其托管在数据库中以进行分析,我需要处理数百万行。对于 sql 插入目的,我正在尝试批量插入 1000 条或更多记录。

我创建了一个自己的函数来连接 1000 条记录的 sql 查询。

步骤

  • 为 sql 插入构建字符串的循环。
  • 在每个第 1000 个循环中,将使用 free() 处理和释放 sql 查询
  • 我在每个第 1000 个循环中放置了一个 sleep(1)。这样我就有时间检查任务管理器上的资源监视器并按 ctrl+c
  • 停止程序
  • 继续,直到处理完所有记录。

问题是,

我的系统有 8GB 内存,在运行程序之前,内存使用量为 1.8GB。下面是循环计数和内存使用量(近似值)。

loop      memory usage
10,000 2.2 GB
20,000 3.6 GB
40,000 5.0 GB

它继续......当达到 60,000+ 时,内存使用率变为 100%。

恐怕我在某处做错了什么。我想不通。如何在每次执行时释放内存并在程序结束时保持系统稳定?

下面是我的完整测试代码...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>


typedef struct _device_data
{
int uid;
double stime;
double dur;
char *location;
} DeviceData;

char *buildSQLstr(const char *sql, const DeviceData *deviceData);

int main(void)
{

char *sql = "";
int loop = 0;
int sqlIsReady = 0;

for (loop = 0; loop<60000; loop++)
{
DeviceData deviceData = {};
deviceData.stime = 343434.34343;
deviceData.dur = 1.00343;
deviceData.location = malloc(50);
deviceData.location[0] = '\0';
sprintf(deviceData.location, "Location No: - %d", loop);

if (loop % 1000 == 0) {
if (sqlIsReady) {
printf("\nLoop = %d\tLength of ssql : %d\n", loop, strlen(sql));
free(sql);
sql = "";
sqlIsReady = 0;
sleep(1);
}
}
else {
sql = buildSQLstr(sql, &deviceData);
if (strlen(sql) > 0)
sqlIsReady = 1;
}
}

printf("%d\n", strlen(sql));
getchar();

return 0;
}



char *buildSQLstr(const char *sql, const DeviceData *deviceData)
{
char *insert_pattern = "INSERT INTO access(stime,dur,location,uid) VALUES (datetime(%f, 'unixepoch'), %f, '%s', %d);";
int sql_size = strlen(insert_pattern) + (sizeof(double) * 2) + strlen(deviceData->location) + sizeof(int) + 1;
char *sql_insert = malloc(sql_size);
sql_insert[0] = '\0';
sprintf(sql_insert, insert_pattern, deviceData->stime, deviceData->dur, deviceData->location, deviceData->uid);


char *ptrRetSql = (char *) malloc(strlen(sql) + strlen(sql_insert) + 1);
if (ptrRetSql != NULL) {
ptrRetSql[0] = '\0';
strncpy(ptrRetSql, sql, strlen(sql));
strncat(ptrRetSql, sql_insert, strlen(sql_insert));
}
else {
fprintf(stderr, "Malloc failed : %s\n", strerror(errno));
}

free(sql_insert); // here I am freeing the memory allocated for sql_insert pointer
return ptrRetSql;
}

.

请指教我怎样才能克服这个问题..

编辑(最终工作副本)

谢谢大家,根据下面的回复,我修改了我的代码,如下图所示。它现在工作顺利。我的内存使用量表现在对于任何数量的循环都是稳定的。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>


typedef struct
{
int uid;
double stime;
double dur;
char *location;
} DeviceData_t;

char *buildSQLstr(const char *sql, const DeviceData_t *deviceData);

int main(void)
{

char *sql = NULL;
int loop = 0;
int sqlIsReady = 0;

for (loop = 0; loop<100010; loop++)
{
DeviceData_t *deviceData = (DeviceData_t *) malloc(sizeof(DeviceData_t));
deviceData->stime = 343434.34343;
deviceData->dur = 1.00343;
deviceData->location = (char *) malloc(50);
if (deviceData->location != NULL) {
deviceData->location[0] = '\0';
sprintf(deviceData->location, "Location No: - %d", loop);
}

if (loop % 1000 == 0) {
if (sqlIsReady) {
if (sql != NULL) free(sql);
// Process here the sql
sql = NULL;
sqlIsReady = 0;
}
printf("Current loop count : %d\n", loop);
}
else {
char *tmpSql = buildSQLstr(sql, deviceData);
if (sql != NULL) free(sql);

sql = malloc(strlen(tmpSql) + 1);
sql[0] = '\0';
strcpy(sql, tmpSql);
free(tmpSql);
if (strlen(sql) > 0)
sqlIsReady = 1;
}
if (deviceData != NULL) free(deviceData);
}

if (sql != NULL) {
printf("%Remaining SQL Length : %d\n%s\n", strlen(sql), sql);
free(sql);
}

return 0;
}



char *buildSQLstr(const char *sql, const DeviceData_t *deviceData)
{
char *insert_pattern = "INSERT INTO access(stime,dur,location,uid) VALUES (datetime(%f, 'unixepoch'), %f, '%s', %d);";
int sql_size = strlen(insert_pattern) + (sizeof(double) * 2) + strlen(deviceData->location) + sizeof(int) + 1;
char *sql_insert = (char *) malloc(sql_size);
sql_insert[0] = '\0';
sprintf(sql_insert, insert_pattern, deviceData->stime, deviceData->dur, deviceData->location, deviceData->uid);

int ptrRetSql_size;
if (sql != NULL ) ptrRetSql_size = strlen(sql) + strlen(sql_insert) + 1;
else ptrRetSql_size = strlen(sql_insert) + 1;

char *ptrRetSql = (char *) malloc(ptrRetSql_size);

if (ptrRetSql != NULL) {
ptrRetSql[0] = '\0';
if (sql != NULL) strcat(ptrRetSql, sql);

if (sql != NULL) strcat(ptrRetSql, sql_insert);
else strcpy(ptrRetSql, sql_insert);
}
else {
fprintf(stderr, "Malloc failed : %s\n", strerror(errno));
}

if (sql_insert != NULL) free(sql_insert);
return ptrRetSql;
}

这里出现了另一个问题。我应该单独清理 deviceData->location 分配的内存吗?

最佳答案

您的代码 mallocbuildSQLstr 函数的返回值(从底部开始第 13 行),然后将字符串返回给 main .但是,mainbuildSQLstr 都不会释放该字符串,直到迭代被 1000 整除,从而在剩余的 999 次迭代中造成内存泄漏。

由于传递给 buildSQLstrsql 的旧值在 strcat 之后变得无关紧要,您可以释放旧值字符串就在那里:

if (ptrRetSql != NULL) {
ptrRetSql[0] = '\0';
strncpy(ptrRetSql, sql, strlen(sql));
free(sql); // <<==== Here
strncat(ptrRetSql, sql_insert, strlen(sql_insert));
}

您还需要在 mainfree sql 的最终值以避免泄漏最后的 sql字符串。为了正确地做到这一点,您应该将声明更改为

char *sql = malloc(1);
sql[0] = '\0';

否则,当日志为空时,您可能会将指向 "" 字符串文字的指针传递给 free,从而导致未定义的行为。当您释放它时,在 if 的顶部分支中分配 "" 也是如此。

关于c - 如何在c中有效地构建字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22506192/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com