gpt4 book ai didi

c - 向 asprintf 字符串添加额外的 int 变量会导致段错误。

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

我目前正在做一个 C 项目,我遇到了一个我不明白的相当奇怪的问题。

我正在使用 asprintf 构建一个 SQL 语句,它工作正常,直到我向字符串添加一个 int 变量,然后它导致段错误。下面是我的函数代码。

int drilldownSetRowData(callLogSearchDataStruct * callLogSearchData, int dataRow, MYSQL *HandleDB, long inboundEpochTimeStamp)
{
char * inboundSql = NULL;
char * sql = NULL;
int sqlLen = 0;
char * tempSql = NULL;
char * outboundSql = NULL;

char epochBuffer[11];
int outboundLegCounter = 0;
callLogSearchOutboundStruct * outboundLeg = NULL;
if (dataRow == -1)
{
return 0;
}
char durationBuffer[8];

snprintf(durationBuffer, sizeof(durationBuffer), "%.1f", callLogSearchData[dataRow].duration);
snprintf(epochBuffer, sizeof(epochBuffer), "%ld", inboundEpochTimeStamp);

asprintf(&inboundSql, "INSERT INTO DataTable VALUES (%i, %i, '%s', '%s', %i),"
"(%i, %i, '%s', '%s', %i), (%i, %i, '%s', '%s', %i), (%i, %i, '%s', '%s', %i),"
"(%i, %i, '%s', '%s', %i), (%i, %i, '%s', '%s', %i)",
dataRow, D_DATE, callLogSearchData[dataRow].date, epochBuffer, outboundLegCounter,
dataRow, D_TIME, callLogSearchData[dataRow].time, epochBuffer, outboundLegCounter,
dataRow, D_APARTY, callLogSearchData[dataRow].aParty, epochBuffer, outboundLegCounter,
dataRow, D_BPARTY, callLogSearchData[dataRow].bParty, epochBuffer, outboundLegCounter,
dataRow, D_DURATION, durationBuffer, epochBuffer,outboundLegCounter,
dataRow, D_RESULT, callLogSearchData[dataRow].cleardownCause, epochBuffer, outboundLegCounter);

for (outboundLeg = callLogSearchData[dataRow].outboundLegs; outboundLeg != NULL && outboundLeg->target != NULL; outboundLeg = outboundLeg->nextLeg)
{
outboundLegCounter++;
snprintf(durationBuffer, sizeof(durationBuffer), "%.1f", outboundLeg->duration);

if (outboundSql == NULL)
{
printf("outboundSql is NULL\n");
asprintf(&tempSql, "(%i, %i, '%s', '%s', 6),"
"(%i, %i, '%s', '%s', 7), (%i, %i, '%s', '%s', 8)",
dataRow, D_TARGET, outboundLeg->target, epochBuffer,
dataRow, D_TARGET_DURATION, durationBuffer, epochBuffer,
dataRow, D_TARGET_RESULT, setCallResultBackToCallResultNumber(outboundLeg->cleardownCause));
}
else
{
printf("outboundSql is not NULL\n");
asprintf(&tempSql, "%s, (%i, %i, '%s', '%s', %i),"
"(%i, %i, '%s', '%s', %i), (%i, %i, '%s', '%s', %i)",
outboundSql, dataRow, D_TARGET, outboundLeg->target, epochBuffer, outboundLegCounter,
dataRow, D_TARGET_DURATION, durationBuffer, epochBuffer, outboundLegCounter,
dataRow, D_TARGET_RESULT, setCallResultBackToCallResultNumber(callLogSearchData->cleardownCause), epochBuffer, outboundLegCounter);
}

}
outboundSql = tempSql;
if (outboundSql != NULL)
{
sqlLen = asprintf(&sql, "%s, %s", inboundSql, outboundSql);
}
else
{
sqlLen = asprintf(&sql, "%s", inboundSql);
}
SL_DebugAll(DBG_INFO, sql);
if ((mysql_real_query(HandleDB, sql, sqlLen))) return 1;

return 0;
}

问题出在以下几行:

if (outboundSql == NULL)
{
printf("outboundSql is NULL\n");
asprintf(&tempSql, "(%i, %i, '%s', '%s', %i),"
"(%i, %i, '%s', '%s', %i), (%i, %i, '%s', '%s', %i)",
dataRow, D_TARGET, outboundLeg->target, epochBuffer, outboundLegCounter
dataRow, D_TARGET_DURATION, durationBuffer, epochBuffer, outboundLegCounter,
dataRow, D_TARGET_RESULT, setCallResultBackToCallResultNumber(outboundLeg->cleardownCause), outboundLegCounter);
}

如果我从 asprintf 中删除 outboundLegCounter 参数并将一个 int 值硬编码到字符串中(替换每行插入末尾的 %i)程序工作正常但使用该参数会引发段错误。

正如您在代码中看到的那样,outboundLegCounter 设置为 0,循环中发生的第一件事是 outboundLegCounter 递增,所以我不明白为什么这会导致段错误。

感谢您提供的任何帮助。

最佳答案

您在这一行缺少一个 epochBuffer:

dataRow, D_TARGET_RESULT, setCallResultBackToCallResultNumber(outboundLeg->cleardownCause), outboundLegCounter);

关于c - 向 asprintf 字符串添加额外的 int 变量会导致段错误。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19856612/

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