gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 15:45:52 24 4
gpt4 key购买 nike

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

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