C++“访问冲突读取位置”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15995021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ "Access violation reading location" Error
提问by Alex Alex
I have the following Vertex
struct in a Graph
class:
我Vertex
在一个Graph
类中有以下结构:
struct Vertex
{
string country;
string city;
double lon;
double lat;
vector<edge> *adj;
Vertex(string country, string city, double lon, double lat)
{
this->country = country;
this->city = city;
this->lon = lon;
this->lat = lat;
this->adj = new vector<edge>();
}
};
When calling the method I've written called getCost()
, I keep getting the same Unhandled Exception
调用我编写的方法时getCost()
,我不断收到相同的未处理异常
Access violation reading location 0x00000048
访问冲突读取位置 0x00000048
and I cannot figure out why.
我不知道为什么。
the getCost()
method:
的getCost()
方法:
void Graph::getCost(string from, string to)
{
Vertex *f = (findvertex(from));
vector<edge> *v = f->adj; // Here is where it gives the error
vector<edge>::iterator itr = v->begin();
for (; itr != v->end(); itr++)
{
if (((*itr).dest)->city == to)
cout << "\nCost:-" << (*itr).cost;
}
}
The method findvertex()
returns a value of type Vertex*
. Why do I keep receiving this error?
该方法findvertex()
返回一个类型的值Vertex*
。为什么我一直收到这个错误?
the findVertex method:
findVertex 方法:
Vertex* Graph::findvertex(string s)
{
vmap::iterator itr = map1.begin();
while (itr != map1.end())
{
if (itr->first == s){
return itr->second;
}
itr++;
}
return NULL;
}
Where map1
is defined:
在哪里map1
定义:
typedef map< string, Vertex *, less<string> > vmap;
vmap map1;
回答by
You haven't posted the findvertex
method, but Access Reading Violation with an offset like 0x00000048
means that the Vertex* f;
in your getCost function is receiving null, and when trying to access the member adj
in the null
Vertex pointer (that is, in f
), it is offsetting to adj
(in this case, 72 bytes ( 0x48 bytes in decimal )), it's reading near the 0
or null
memory address.
您还没有贴findvertex
的方法,但使用读冲突与像偏移0x00000048
方式的Vertex* f;
在你的getCost函数接收空,而当试图访问该成员adj
的null
顶点指针(即在f
),它被抵消到adj
(在这种情况下,72 字节(十进制 0x48 字节)),它在0
或null
内存地址附近读取。
Doing a read like this violates Operating-System protected memory, and more importantly means whatever you're pointing at isn't a valid pointer. Make sure findvertex
isn't returning null, or do a comparisong for null on f
before using it to keep yourself sane (or use an assert):
像这样读取会违反操作系统保护的内存,更重要的是意味着您指向的任何内容都不是有效指针。确保findvertex
没有返回 null,或者f
在使用它之前对 null 进行比较以保持理智(或使用断言):
assert( f != null ); // A good sanity check
assert( f != null ); // A good sanity check
EDIT:
编辑:
If you have a map
for doing something like a find, you can just use the map's find
method to make sure the vertex exists:
如果你有一个map
像查找这样的东西,你可以使用地图的find
方法来确保顶点存在:
Vertex* Graph::findvertex(string s)
{
vmap::iterator itr = map1.find( s );
if ( itr == map1.end() )
{
return NULL;
}
return itr->second;
}
Just make sure you're still careful to handle the error case where it does return NULL
. Otherwise, you'll keep getting this access violation.
只要确保您仍然小心处理它返回的错误情况NULL
。否则,您将不断收到此访问冲突。
回答by Alex Alex
Vertex *f=(findvertex(from));
if(!f) {
cerr << "vertex not found" << endl;
exit(1) // or return;
}
Because findVertex
can return NULL
if it can't find the vertex.
因为如果找不到顶点findVertex
可以返回NULL
。
Otherwise this f->adj;
is trying to do
否则这f->adj;
是试图做
NULL->adj;
Which causes access violation.
这会导致访问冲突。