Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 分配结构数组后内存被覆盖_C++_Arrays_Memory Leaks_Memory Management - Fatal编程技术网

C++ 分配结构数组后内存被覆盖

C++ 分配结构数组后内存被覆盖,c++,arrays,memory-leaks,memory-management,C++,Arrays,Memory Leaks,Memory Management,我试图为结构数组分配内存,但在分配内存后,函数中传递的int被设置为“0”。。。当我增加数组的大小时,问题就消失了。这是我的密码: wchar_t* ISTFallSensor::JSON_EventLog(int nRecords) { wchar_t* returnstring = new wchar_t[8192]; memset( returnstring, 0, 8192 * sizeof(TCHAR) ); HINSTANCE hIstDLL; DWORD (*IST_Open)

我试图为结构数组分配内存,但在分配内存后,函数中传递的int被设置为“0”。。。当我增加数组的大小时,问题就消失了。这是我的密码:

wchar_t* ISTFallSensor::JSON_EventLog(int nRecords) {
wchar_t* returnstring = new wchar_t[8192]; memset( returnstring, 0, 8192 * sizeof(TCHAR) ); 

HINSTANCE hIstDLL;
DWORD (*IST_Open)(TCHAR *, HANDLE *)                                        = 0;
DWORD (*IST_Close)(HANDLE)                                                  = 0;
DWORD (*IST_GetMotionEventLogCount)(HANDLE, DWORD, PDWORD)                  = 0;
DWORD (*IST_GetMotionEventLogRecords)(HANDLE, IST_LOG_RECORD[], int, PINT)  = 0;

hIstDLL = LoadLibrary(L"ISTAPI32.dll");
if(hIstDLL && nRecords > 0 ){
    IST_Open                        = (DWORD (__cdecl *)(TCHAR *, HANDLE *))GetProcAddress(hIstDLL, L"IST_Open");
    IST_Close                       = (DWORD (__cdecl *)(HANDLE))GetProcAddress(hIstDLL, L"IST_Close");
    IST_GetMotionEventLogCount      = (DWORD (__cdecl *)(HANDLE, DWORD, PDWORD))GetProcAddress(hIstDLL, L"IST_GetMotionEventLogCount");
    IST_GetMotionEventLogRecords    = (DWORD (__cdecl *)(HANDLE, IST_LOG_RECORD[], int, PINT))GetProcAddress(hIstDLL, L"IST_GetMotionEventLogRecords");

    HANDLE  phIst = INVALID_HANDLE_VALUE;
    DWORD openStatus = IST_Open( _T("IST1:"), &phIst );

    if ( openStatus == IST_ERROR_SUCCESS ) {
        DWORD dropsD; IST_GetMotionEventLogCount(phIst, FREEFALL, &dropsD);
        int drops = (int)dropsD;
        if ( nRecords > drops ) nRecords = drops; if ( nRecords > 32 ) nRecords = 32;
        int pnRecords = 0; 
        IST_LOG_RECORD eventlog[32] = {0};

        DWORD getStatus = IST_GetMotionEventLogRecords(phIst, eventlog, drops, &pnRecords);
最后一个函数获取事件列表,并使用给定的数组存储该信息。当函数返回时,数组被正确填充,但nRecords值被“0”覆盖


有人知道我做错了什么吗?

你的内存溢出了

您可以调整变量
nRecords
,使其不超过32,这是适合
eventlog
数组的最大
IST\u LOG\u记录数

但是,在调用
IST\u GetMotionEventLogRecords
时不使用它。相反,您使用的是
drops
,它等于
dropsD
,而不限制为32

只需使用
nRecords
而不是
drops

DWORD getStatus = IST_GetMotionEventLogRecords(phIst, eventlog, nRecords, &pnRecords);