C 重新分配内存会导致下一个大小无效
我正在从事一个C语言的项目,我对C语言非常陌生,所以这可能是一个非常简单的答案,但我不知道如何解决这个问题 我拥有的是一个结构,它在头文件中以以下方式定义C 重新分配内存会导致下一个大小无效,c,structure,realloc,C,Structure,Realloc,我正在从事一个C语言的项目,我对C语言非常陌生,所以这可能是一个非常简单的答案,但我不知道如何解决这个问题 我拥有的是一个结构,它在头文件中以以下方式定义 typedef struct CallLogSearchDataStruct { char * date; char * time; char * bParty; char * aParty; float duration; char * cleardownCause; struct 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\u CALL\u STRUCT\u SIZE
设置为100
我有一些在while循环中循环的代码,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;
}
我已经仔细检查了GDB中的代码,发现currentSize
,newSize
变量是正确的,即当前大小是100,新大小将是200,但当它执行callLogSearchData的第一个realloc时,我的应用程序崩溃,出现以下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));
上述赋值的左侧是双指针,您正试图向其分配一个指针。您还可以使用双指针访问结构的内容。您的代码正在某个地方粉碎堆,否则
realloc
不会失败。我建议您使用。我已经查看了Valgrind并阅读了它的教程,但在日志中并非所有内容都有意义。它有助于解决我遇到的其他问题,但不幸的是,作为第一个响应,这个错误看起来更像是十六进制的地址,而不是数字200。。。在这个答案中,我怀疑您可能以某种方式过度运行了malloc数据结构。。。但是,你的问题很好,而且信息丰富。我会慢慢来的谢谢你放松,我在某个地方读到你应该施放,这只对malloc有效还是对calloc和realloc都一样,你不应该施放谢谢,这看起来很有效。至少没有得到realloc错误。然而,它在代码中的其他地方崩溃了,但我与我的原始问题无关,因为它似乎以某种方式将部分SQL查询存储在结构的一部分,然后访问超出范围的内存。谢谢你的帮助
*callLogSearchData = (callLogSearchDataStruct*) realloc(*callLogSearchData, newSize * sizeof (callLogSearchDataStruct));
callLogSearchData = (callLogSearchDataStruct*) realloc(callLogSearchData, newSize * sizeof (callLogSearchDataStruct));
callLogSearch = (callLogSearchResultStruct*) realloc(callLogSearch, newSize * sizeof (callLogSearchResultStruct));
switches = (switchIDStructure*) realloc(switches, newSize * sizeof (switchIDStructure));