使用 C++ Codegear 而不是使用 Visual C++ 发生 Stackoverflow 异常

Stackoverflow exception occurring with C++ Codegear not with Visual C++

我有这个场景:

问题如下:

库的简化代码:

struct TmRecordValue
{
    UINT8 RecordValue[128];
};
struct TmRecord{
    TmRecordValue RecordValues[2];
};
struct TmStruct
{
    UINT32 NumberOfRecords;
    TmRecord* Records;
};
void InitializeStruct(TmStruct* pStruct, int pNumberOfRecords)
{
    memset(pStruct, 0, sizeof(TmStruct));
    pStruct->NumberOfRecords = pNumberOfRecords;
    pStruct->Records = new TmRecord[pStruct->NumberOfRecords];
    memset(pStruct->Records, 0, sizeof(TmRecord) * pStruct->NumberOfRecords);
}
void AddRecordToStruct(TmStruct* pStruct, int pRecordIndex, char* pFirstValue, char* pSecondValue)
{
    strcpy_s(reinterpret_cast<char*>(pStruct->Records[pRecordIndex].RecordValues[0].RecordValue), 128, pFirstValue);
    strcpy_s(reinterpret_cast<char*>(pStruct->Records[pRecordIndex].RecordValues[1].RecordValue), 128, pSecondValue);
}

程序的简化代码:

TmStruct *struct1 = new TmStruct();
int RecordsQuantity = 100000;
InitializeStruct(struct1, RecordsQuantity);
for (int i = 0; i < RecordsQuantity; i++)
{
    AddRecordToStruct(struct1, i, "first", "second");
}

评论:

提前致谢,

您正在使用 sizeof(TmStruct) 作为指向 TmRecord 的指针:

memset(pStruct->Records, 0, sizeof(TmStruct) * pStruct->NumberOfRecords);

检查调用约定。确保在任何地方都使用 cdeclstdcall

因为根据症状看起来 Visual Studio 库是 cdecl 但 Embarcadero 程序假定它是 stdcall,因此库和调用者都不会释放堆栈。