c - 重新分配内存会导致下一个大小无效

标签 c structure realloc

我正在用 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 ***

感谢您提供的任何帮助。

最佳答案

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

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

你想要:

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

关于c - 重新分配内存会导致下一个大小无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18761720/

相关文章:

c - 同时使用信号量和互斥体是一个好方法吗?

c - 指针初始化不会产生稳定的结果

c - 使用 switch case,默认部分出错

R - 改变列表结构

c++ - 如果缩小分配的内存大小,还要检查 realloc() 吗?

c - 在 qmake 中设置 C 标准

c - 带填充的结构的大小

c - 指向结构的指针数组

c - 重新分配二维数组 - valgrind 错误

c - 如何显示另一个结构的列表结构?