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 のコードをステップ実行したところcurrentSizenewSize変数が正しいことがわかりました。つまり、現在のサイズは 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