关于ZeroMemory的问题

yumangmang 2008-09-19 11:36:38
大家好!
在我正在看得书里面,有一段关于消息循环的代码,在开始的时候首先是定义了一个消息变量,然后把对应的内存清空(个人的理解,不知道对否),代码如下:
	MSG msg;
::ZeroMemory(&msg, sizeof(MSG));

令人不解的是,一般我们定义一个变量并不需要将变量值的地址的内存清空,直接使用就好了,为什么这里要使用ZeroMemory()清空呢?
...全文
282 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
帅得不敢出门 2008-09-20
  • 打赏
  • 举报
回复
up
e_sharp 2008-09-20
  • 打赏
  • 举报
回复
ZeroMemory Macro

Fills a block of memory with zeros.

To avoid any undesired effects of optimizing compilers, use the SecureZeroMemory function.

Syntax

void ZeroMemory(
[in] PVOID Destination,
[in] SIZE_T Length
);


Parameters
Destination [in]
A pointer to the starting address of the block of memory to fill with zeros.

Length [in]
The size of the block of memory to fill with zeros, in bytes.

Return Value
This macro has no return value.

Remarks
Many programming languages include syntax for initializing complex variables to zero. There can be differences between the results of these operations and the ZeroMemory function. Use ZeroMemory to clear a block of memory in any programming language.

This macro is defined as the RtlZeroMemory macro. For more information, see Winbase.h and Winnt.h.
zclever 2008-09-20
  • 打赏
  • 举报
回复
最好清空,否则会遇到意想不到的后果
yjkwf 2008-09-20
  • 打赏
  • 举报
回复
避免出现一些不必要的麻烦,呵呵!
星羽 2008-09-20
  • 打赏
  • 举报
回复
一个编程在未初始化之前的值是未定义的,也就是可能是任何值

比如

int a;

a 的值得就是未知的

所以一般都这么写

int a = 0;


对于有些结构,比如 MSG

如果你不清空,那么它的值也是未知的,所以可能有意想不到的结果,为了安全,所以 ZeroMemory
wudeshou82666 2008-09-20
  • 打赏
  • 举报
回复
学习了
cattycat 2008-09-19
  • 打赏
  • 举报
回复
1楼right。这样是规范的编码方式,提倡的。
chen_yuan 2008-09-19
  • 打赏
  • 举报
回复
如果不ZeroMemory,可能结构体内的成员会得到系统赋的默认值,那样可能会出现意想不到的结果.
所以,对于结构体进行清零是有必要的,然后对相应的成员赋其需要的值.
串口通信C程序 //Set CommTimeOuts BOOL SetCommTimeOuts ( VOID ) { //SetComm Input & Output Buffer SetupComm ( hSerial ,MAXLEN * 2 ,MAXLEN * 2 ); //Read TimeOut TimeOuts.ReadIntervalTimeout = MAXDWORD; //Set Total Read TimeOut as MAXDWORD (0xFFFFFFFF) TimeOuts.ReadTotalTimeoutConstant = 0; //Read TimeOut Const TimeOuts.ReadTotalTimeoutMultiplier = 0; //Return Riht now //Write TimeOut TimeOuts.WriteTotalTimeoutMultiplier = 50; //TotalTimeOut = TimeAgr*NumOfChars+const time TimeOuts.WriteTotalTimeoutConstant = 2000; //Set Write TimeOuts return ( FALSE != SetCommTimeouts ( hSerial,&TimeOuts ) ); } //Error MsgBox BOOL ErrMsg ( HWND hWnd,TCHAR *szErr ) { return ( FALSE != MessageBox ( hWnd,szErr,NULL,MB_OK | MB_ICONWARNING ) ); } //Read Comm BOOL ReadCom ( HWND hSet,BYTE InBuff[],INT BytesNum ) { DWORD dwBytesRead = 0; //Record The bytes already read INT iBytesToRead = 0; DWORD dwErrMask = 0; //Clear Memory ZeroMemory ( &ComState ,sizeof(ComState) ); ZeroMemory ( &OvLap ,sizeof(OvLap) ); OvLap.Offset = 0; OvLap.OffsetHigh = 0; OvLap.hEvent = CreateEvent (NULL,TRUE,FALSE,NULL); //Clear All sPort error ClearCommError (hSerial,&dwErrMask,&ComState); //Get The Bytes to read from buff iBytesToRead = min (2000,ComState.cbInQue); if ( 0 == iBytesToRead ) return FALSE; else; if ( FALSE == ReadFile (hSerial,InBuff,iBytesToRead,&dwBytesRead,&OvLap ) ) { if ( GetLastError () == ERROR_IO_PENDING ) { WaitForSingleObject (OvLap.hEvent,2000); PurgeComm (hSerial,PURGE_TXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR ); } else; } else; return (TRUE); } ///Write Comm BOOL WriteCom ( BYTE *szBuff,DWORD dwBytes ) { DWORD dwErrMask = 0 ; DWORD dwBytesWritten = 0; ZeroMemory ( &OvLap ,sizeof(OvLap) ); ZeroMemory ( &ComState,sizeof(ComState) ); OvLap.Offset = 0; OvLap.OffsetHigh = 0; OvLap.hEvent = CreateEvent (NULL,TRUE,FALSE,NULL); //Clear All sPort error ClearCommError (hSerial,&dwErrMask,&ComState); if ( FALSE == WriteFile ( hSerial,szBuff,dwBytes,&dwBytesWritten ,&OvLap ) ) { if ( GetLastError () == ERROR_IO_PENDING ) { WaitForSingleObject (OvLap.hEvent,2000) ; PurgeComm (hSerial,PURGE_TXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR ); } else; } else; return ( dwBytesWritten == dwBytes ); } TCHAR* Caps ( TCHAR *szStr ) { UINT i = 0; for ( i = 0 ; szStr[i] != '\0'; i ++ ) { if ( szStr[i] >= 'A' && szStr[i] <= 'Z' ) szStr[i] += 0x20; } return szStr; }

64,668

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧