[英]C++ “Access violation reading location” Error
I have the following Vertex
struct in a Graph
class: 我在
Graph
类中有以下Vertex
结构:
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()
,我不断得到相同的Unhandled Exception
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*
. 方法
findvertex()
返回Vertex*
类型的值。 Why do I keep receiving this error? 为什么我一直收到这个错误?
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;
You haven't posted the findvertex
method, but Access Reading Violation with an offset like 0x00000048
means that the Vertex* f;
你还没有发布
findvertex
方法,但是像0x00000048
这样的偏移量的Access Reading Violation意味着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. 在你的getCost函数中接收null,并且当试图访问
null
顶点指针中的成员adj
时(即在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
进行比较以保持自己的理智(或使用断言):
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
. 只要确保你仍然小心处理它确实返回
NULL
的错误情况。 Otherwise, you'll keep getting this access violation. 否则,您将继续获得此访问冲突。
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;
否则这个
f->adj;
is trying to do 正在努力做到
NULL->adj;
Which causes access violation. 这会导致访问冲突。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.