0

我正在用 C 开发一个项目,我对 C 很陌生,所以这可能是一个非常简单的答案,但我不知道如何解决这个问题。

我所拥有的是一个结构,它以下列方式在头文件中定义。

typedef struct CallLogSearchDataStruct
{
    char * date;
    char * time;
    char * bParty;
    char * aParty;
    float duration;
    char * cleardownCause;
    struct CallLogSearchOutboundStruct * outboundLegs;
} callLogSearchDataStruct;

然后使用以下代码片段对其进行引用和分配。

callLogSearchDataStruct * callLogSearchData = NULL;

callLogSearchData = (callLogSearchDataStruct*)calloc(INITIAL_CALL_STRUCT_SIZE,sizeof(callLogSearchDataStruct));

INITIAL_CALL_STRUCT_SIZE设置为100

我有一些代码在 while 循环中循环,该循环递增一个名为currentStructIndexValue. 每次增加这个值时,我都会调用一个名为reallocateStructures. 这包含各种参数,例如我要重新分配的结构数组。

即应该发生什么,callLogSearchDataStructure 被调用为 100 的大小。当currentStructIndexValue值变为 101 并reallocateStructures调用函数时,该结构应该调整大小以包含另一个 100,即现在包含 200。

下面是reallocateStructures我遇到问题的函数的代码。

int reallocateStructures(callLogSearchResultStruct **callLogSearch, callLogSearchDataStruct ** callLogSearchData, 
        switchIDStructure ** switches, int *timesStructHasBeenReallocated, int currentStructIndexValue,
        int dataRow)
{
    int INITIAL_CALL_STRUCT_SIZE = 100;
    int currentSize = 0;
    int newSize = 0;
    int initFromIndex = 0;

    if (currentStructIndexValue == INITIAL_CALL_STRUCT_SIZE) {
        printf("REALLOCATING STRUCTURES");
        currentSize = currentStructIndexValue * *timesStructHasBeenReallocated;

        newSize = currentSize + INITIAL_CALL_STRUCT_SIZE;
        *timesStructHasBeenReallocated = *timesStructHasBeenReallocated + 1;

        callLogSearchData = (callLogSearchDataStruct*) realloc(callLogSearchData, newSize * sizeof (callLogSearchDataStruct));
        callLogSearch = (callLogSearchResultStruct*) realloc(callLogSearch, newSize * sizeof (callLogSearchResultStruct));
        switches = (switchIDStructure*) realloc(switches, newSize * sizeof (switchIDStructure));

        for (initFromIndex = currentSize; initFromIndex < newSize; initFromIndex++) {
            callLogSearchData[initFromIndex]->aParty = NULL;
            callLogSearchData[initFromIndex]->bParty = NULL;
            callLogSearchData[initFromIndex]->cleardownCause = NULL;
            callLogSearchData[initFromIndex]->date = NULL;
            callLogSearchData[initFromIndex]->duration = 0;
            callLogSearchData[initFromIndex]->outboundLegs = NULL;
            callLogSearchData[initFromIndex]->time = NULL;

            callLogSearch[initFromIndex]->date = NULL;
            callLogSearch[initFromIndex]->dRowIndex = dataRow;

            switches[initFromIndex]->switchID = NULL;
        }
        return 0;
    }
    return 1;
}

下面是我如何调用上述方法

if (reallocateStructures(&callLogSearch, &callLogSearchData, &switches, &timesStructHasBeenReallocated, currentStructIndexValue, dataRow) == 0)
{
   //Structures have been reallocated so reset the index
   currentStructIndexValue = 0;
}

我已经逐步检查了 GDB 中的代码,发现currentSize,newSize变量是正确的,即当前大小为 100,新大小将为 200,但是当它为 callLogSearchData 执行第一次重新分配时,我的应用程序崩溃并显示以下 GDB 消息

*** glibc detected *** realloc(): invalid size: 0xbfffed18 ***
*** glibc detected *** realloc(): invalid pointer: 0xbfffed1c ***
*** glibc detected *** realloc(): invalid pointer: 0xbfffed14 ***

感谢您的任何帮助,您可以提供。

4

2 回答 2

1

在您的reallocateStructures函数中,您为每个项目传递一个指向指针的指针。当您调用时,realloc()您应该遵循指向指针的指针以获取原始指针,无论是输入参数还是分配结果时。换句话说,您目前拥有:

   callLogSearchData = (callLogSearchDataStruct*) realloc(callLogSearchData, newSize * sizeof (callLogSearchDataStruct));

你要:

   *callLogSearchData = (callLogSearchDataStruct*) realloc(*callLogSearchData, newSize * sizeof (callLogSearchDataStruct));
于 2013-09-12T12:29:49.220 回答
0
        callLogSearchData = (callLogSearchDataStruct*) realloc(callLogSearchData, newSize * sizeof (callLogSearchDataStruct));
        callLogSearch = (callLogSearchResultStruct*) realloc(callLogSearch, newSize * sizeof (callLogSearchResultStruct));
        switches = (switchIDStructure*) realloc(switches, newSize * sizeof (switchIDStructure));

上述分配的左侧是双指针,您试图为其分配单个指针。您还可以使用双指针来访问结构的内容。

于 2013-09-12T12:41:51.787 回答