如何解决分配了结构数组
我正在尝试为结构数组分配内存,但是在分配了该函数后,将传递给该函数的int设置为\'0 \'...当我增加数组的大小时,问题就消失了。这是我的代码:wchar_t* ISTFallSensor::JSON_EventLog(int nRecords) {
wchar_t* returnstring = new wchar_t[8192]; memset( returnstring,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,PDWORD))GetProcAddress(hIstDLL,L\"IST_GetMotionEventLogCount\");
IST_GetMotionEventLogRecords = (DWORD (__cdecl *)(HANDLE,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_LOG_RECORD
的最大数目。
但是,您不会在呼叫IST_GetMotionEventLogRecords
时使用它。而是使用use5ѭ,它等于dropsD
,但不限于32。
只需使用nRecords
而不是drops
:
DWORD getStatus = IST_GetMotionEventLogRecords(phIst,eventlog,nRecords,&pnRecords);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。