Memset 中的异常
当尝试执行 memset 时,会出现以下异常
“SendOutDllTestExe.exe 中 0x1023af7d (PxSmartInterface.dll) 处未处理的异常:0xC0000005:访问冲突写入位置 0x40e3a80e”。
我的 memset 语句将如下所示
memset(lpStatus, 0, csStatus.GetLength());
When try to do a memset it gives the following exception
"Unhandled exception at 0x1023af7d (PxSmartInterface.dll) in SendOutDllTestExe.exe: 0xC0000005: Access violation writing location 0x40e3a80e."
My memset statement will look like this
memset(lpStatus, 0, csStatus.GetLength());
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不是 C++ 异常,而是操作异常。要么您访问了不存在的内存,要么损坏了数据结构并使其析构函数崩溃。 (我假设您在
删除
块包含的结构之前尝试将其清零。)在C++中,您通常不会调用
memset
。std::fill
执行相同的操作(如果可能的话,通常会调用memset
),但具有类型安全性。如果您想在
释放
内存块之前将其清零,则需要一个调试库。在调用对象的析构函数之后和调用free
之前,没有干净的方法来访问对象的内存。调试 malloc 可能是您的开发环境的一个功能。编辑:您可以通过覆盖
delete
来访问对象的预空闲
内存,但不能访问数组。但这不是适合初学者/中级的活动。This is not a C++ exception, it's an operating exception. Either you accessed memory that didn't exist or you corrupted a data structure and crashed its destructor. (I'm assuming you're trying to zero out a block before
delete
ing the structure it contains.)In C++ you don't typically call
memset
.std::fill
does the same thing (and typically calls through tomemset
if possible), but with type safety.If you want to zero out blocks of memory before
free
ing them, you need a debugging library. There's no clean way to access an object's memory after its destructor has been called and beforefree
is called. Debug malloc is probably a feature of your dev environment.Edit: you might be able to access pre-
free
memory for objects, but not arrays, by overridingdelete
. But that is NOT an activity for a beginner/intermediate.最有可能的是,
lpStatus
不指向可写内存的csStatus.GetLength()
字节。您需要检查lpStatus
设置的逻辑。Most likely,
lpStatus
does not point tocsStatus.GetLength()
bytes of writable memory. You need to examine the logic of howlpStatus
is set.