我正在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, ×StructHasBeenReallocated, 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 ***
感谢您提供的任何帮助。
答案 0 :(得分:1)
在reallocateStructures
函数中,为每个项目传递一个指向指针的指针。当你调用realloc()
时,你应该使用指针到指针来获得原始指针,既可以输入参数,也可以在分配结果时使用。换句话说,您目前拥有:
callLogSearchData = (callLogSearchDataStruct*) realloc(callLogSearchData, newSize * sizeof (callLogSearchDataStruct));
你想:
*callLogSearchData = (callLogSearchDataStruct*) realloc(*callLogSearchData, newSize * sizeof (callLogSearchDataStruct));
答案 1 :(得分:0)
callLogSearchData = (callLogSearchDataStruct*) realloc(callLogSearchData, newSize * sizeof (callLogSearchDataStruct));
callLogSearch = (callLogSearchResultStruct*) realloc(callLogSearch, newSize * sizeof (callLogSearchResultStruct));
switches = (switchIDStructure*) realloc(switches, newSize * sizeof (switchIDStructure));
上述赋值的左侧是您尝试分配单个指针的双指针。您还可以使用双指针来访问结构的内容。