访问冲突读取位置异常 [英] Access violation reading location exception

查看:339
本文介绍了访问冲突读取位置异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个简单的问题:


为什么以下代码没有捕获Access违规读取位置异常?


它崩溃了if(xyz)与Access违规读取位置异常的行。是不是应该赶上它?实际上,它没有。

A quick question:

Why doesn''t the following code catch the Access violation reading location exception?

it crashes on line if ( xyz ) with an Access violation reading location exception. Is not catch supposed to catch it? Practically, it doesnt.

展开 | 选择 | Wrap | 行号

推荐答案

C ++尝试{...} catch {.. 。}方法论捕获软件异常。访问无效的内存位置是一个硬件异常(也就是说,只有当你查看位置并找不到发生错误的硬件时,软件才明显出错。


你可以使用一个信号来尝试这种异常,但这对于你必须执行程序的一些主要重置操作(或退出它)的信号非常有帮助。


最好不要首先创建这种情况,你可以通过始终确保你的指针指向有效位置或者设置为NULL来做到这一点。


这应该不难做到,将它们变为NULL,然后你可以指定它们将它们指向有效的内存。当该内存不再有效时(它已被删除或超出范围)重置那里的值返回NULL。


将apointer保留为随机值漂浮是危险的。
The C++ try{...} catch {...} methodology catches software exceptions. Accessing an invalid memory location is a hardware exception (that is it is not obviously wrong in the software it is only when you look at the location and find no hardware that the error is realised.

You could try this sort of exception using a signal but that is not going to help very much as following signal you would have to peform some major reset operation of your program (or exit it).

It would be much better not to create the situation in the first place, you can do this by always ensuring that your pointers either point to a valid location or have been set to NULL.

This should not be hard to do, intiialise them to NULL, then you can assign to them to point them to valid memory. When that memory is no longer valid (it has been deleted or gone out of scope) reset there value back to NULL.


It is dangerous to leave apointer floating with a random value.



试试

{

如果(xyz)//在这里崩溃

{

abc = xyz-> ID;

}

}


catch(...)

{

//例外

}
try
{
if ( xyz ) // crashes here
{
abc = xyz->ID;
}
}

catch (...)
{
//exception
}



此代码不会在上崩溃语句,如果xyz是指针。 if 语句仅对true或false进行测试,并且不访问指针中的位置。


可能发生的事情是你从未初始化xyz所以它是非零(因此是真的)但包含垃圾值。那会让你失望。

This code shouild not crash on the if statement if xyz is a pointer. The if statement does a test for true or false only and does not access the location in the pointer.

What can happen is that you never initialized xyz so that it is non-zero (hence true) but contains a garbage value. That will bring you down.



如果xyz是一个指针,这段代码在 if 语句中不会崩溃。 if 语句仅对true或false进行测试,并且不访问指针中的位置。


可能发生的事情是你从未初始化xyz所以它是非零(因此是真的)但包含垃圾值。这会让你失望。
This code shouild not crash on the if statement if xyz is a pointer. The if statement does a test for true or false only and does not access the location in the pointer.

What can happen is that you never initialized xyz so that it is non-zero (hence true) but contains a garbage value. That will bring you down.



我也很惊讶它在if语句中崩溃了。


更容易理解崩溃在xyz-> ID ......


但由于某种原因它在if语句中崩溃了。它肯定是通过这种方式初始化的。

I am surprised too that it crashes at if statement.

it is more understandable crashing at xyz->ID ...

but it crashes at if statement for some reason. It is definitely initialized by the way.


这篇关于访问冲突读取位置异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆