简体   繁体   English

C ++指针,无法读取内存

[英]C++ Pointers, Unable To Read Memory

Why, when I initiate my pointer, I can read the assigned value: 为什么,当我初始化指针时,我可以读取分配的值:

DWORD *pBytesReturned = new DWORD[0];
_result =  EnumProcesses(pProcessIds, 1000, pBytesReturned);

在此处输入图片说明

But when I initialize it like this I can't read the assigned value: 但是当我这样初始化它时,我无法读取分配的值:

DWORD *pBytesReturned = 0;
_result =  EnumProcesses(pProcessIds, 1000, pBytesReturned);

在此处输入图片说明

Here's the complete code if needed: 如果需要,这是完整的代码:

#include "stdafx.h"
#include <Psapi.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pCmdLine, int iCmdShow)
{
    bool _result;
    DWORD *pProcessIds = new DWORD[1000];
    DWORD cb;
    DWORD *pBytesReturned =0;
    _result =  EnumProcesses(pProcessIds, 1000, pBytesReturned);
}

The intent of the function 功能的意图

BOOL
WINAPI
EnumProcesses (
    _Out_writes_bytes_(cb) DWORD * lpidProcess,
    _In_ DWORD cb,
    _Out_ LPDWORD lpcbNeeded
    );

is to have a pointer as a third parameter, and this pointer must point to a valid memory location where the function will be storing a dword. 将指针作为第三个参数,并且该指针必须指向函数将在其中存储双字的有效内存位置。 The expected function call should look like this 预期的函数调用应如下所示

DWORD *pProcessIds = new DWORD[1000];
DWORD bytesReturned = 0;
bool _result = EnumProcesses(pProcessIds, 1000, &bytesReturned);

or like this 或像这样

DWORD *pProcessIds = new DWORD[1000];
DWORD *pBytesReturned = new DWORD[1];
bool _result = EnumProcesses(pProcessIds, 1000, pBytesReturned);

but you should not be using a NULL pointer and expect the debugger to dereference it. 但您不应该使用NULL指针,并且希望调试器取消引用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM