STL地图范围问题 [英] STL Map Scoping Issue

查看:62
本文介绍了STL地图范围问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码需要在静态范围内声明一个STL Map,

在一个类的函数中将值插入到地图中并访问

值在其他类的功能。这看起来像是一个非常好的b $ b b直接请求,但是当我实现它时,当我尝试访问它时,我得到了垃圾

数据,或者我得到了一个Access Violation运行时

错误(取决于我的代码状态,而我试图找出如何使用这个非常简单的想法来工作)。有没有搞错?!什么

我做错了什么?下面是一些示例代码,提供了访问

违规:


typedef struct _ShipType {

int a;

int b;

int c;

} ShipType;


static std :: map< int,ShipType * GT; shipTypeMap;


void GameLoader :: LoadGame()

{

shipTypeMap [0] = new ShipType();

shipTypeMap [0] - > a = 1;

shipTypeMap [0] - > b = 2;

shipTypeMap [0] - > ; c = 3;

}


int main(int argc,char * argv [])

{

printf(回调函数test.\ n);


GameLoader * gl = new GameLoader();


gl-> LoadGame();


std :: cout<< shipTypeMap [0] - > a<< std :: endl; //< - ---- 0xC000000005访问

违反这里


删除shipTypeMap [0];


getchar();


返回0;

}

I have some code that needs to declare an STL Map in the static scope,
insert values into the map in the function of one class and access the
values in the functions of other classes. This seems like a pretty
straight forward request, but when I implement it I get either garbage
data when I try to access it or I get an Access Violation runtime
error (depending on the state of my code while I try to figure out how
to get this rediculously simple idea to work). What the heck?! What
am I doing wrong? Here is some sample code that gives an Access
Violation:

typedef struct _ShipType {
int a;
int b;
int c;
} ShipType;

static std::map<int, ShipType *> shipTypeMap;

void GameLoader::LoadGame()
{
shipTypeMap[0] = new ShipType();
shipTypeMap[0]->a = 1;
shipTypeMap[0]->b = 2;
shipTypeMap[0]->c = 3;
}

int main(int argc, char* argv[])
{
printf("Callback function test.\n");

GameLoader * gl = new GameLoader();

gl->LoadGame();

std::cout<<shipTypeMap[0]->a<<std::endl;//<-----0xC000000005 Access
Violation here

delete shipTypeMap[0];

getchar();

return 0;
}

推荐答案

您好!


到目前为止,我不知道导致AV的原因。
sf ***** @ gmail.com schrieb:
Hi!

So far I have no idea what is causing the AV.

sf*****@gmail.com schrieb:

delete shipTypeMap [0];
delete shipTypeMap[0];



至少你有未定义的行为:删除的指针是

无效。无效指针可能无法读取或复制,但只能分配给或销毁(当超出范围时)
。但是

地图的元素必须是可复制的。所以你应该首先从

地图中删除指针(将其存储在局部变量中),然后删除它指向的对象。


为了避免这种情况麻烦尝试使用boost :: shared_ptr。


Frank

At least you have undefined behaviour here: deleted pointers are
invalid. invalid pointers may not be read or copied, but only be
assigned to or destructed (when going out of scope). But elements of a
map must be copyable. So you should first remove the pointer from the
map (store it in a local variable) and then delete the object it points to.

To avoid this hassle try using boost::shared_ptr.

Frank


嗨!


我运行了以下代码。它不会在我的系统上崩溃。我不能认为这是因为它应该在cout线崩溃的原因。无论如何,我不会理解为什么你需要静态存储。


#include< iostream>

#include < ostream>

#include< map>

#include< stdio.h>


struct GameLoader

{

void LoadGame();

};


typedef struct _ShipType {

int a;

int b;

int c;

} ShipType;


static std :: map< int,ShipType *> shipTypeMap;


void GameLoader :: LoadGame()

{

shipTypeMap [0] = new ShipType();

shipTypeMap [0] - > a = 1;

shipTypeMap [0] - > b = 2;

shipTypeMap [0] - > ; c = 3;

}


int main(int argc,char * argv [])

{

printf(回调函数test.\ n);


GameLoader * gl = new GameLoader();


gl-> LoadGame();


std :: cout<< shipTypeMap [0] - > a<< std :: endl; //< - ---- 0xC000000005

这里的AccessViolation


删除shipTypeMap [0];


getchar();


返回0;

}


Frank
Hi!

I ran the following code. It doesn''t crash on my system. I can''t think
of a reason why it should crash at the cout line. Anyway, I don''t
understand why you need static storage.

#include <iostream>
#include <ostream>
#include <map>
#include <stdio.h>

struct GameLoader
{
void LoadGame();
};

typedef struct _ShipType {
int a;
int b;
int c;
} ShipType;

static std::map<int, ShipType *> shipTypeMap;

void GameLoader::LoadGame()
{
shipTypeMap[0] = new ShipType();
shipTypeMap[0]->a = 1;
shipTypeMap[0]->b = 2;
shipTypeMap[0]->c = 3;
}

int main(int argc, char* argv[])
{
printf("Callback function test.\n");

GameLoader * gl = new GameLoader();

gl->LoadGame();

std::cout<<shipTypeMap[0]->a<<std::endl;//<-----0xC000000005
AccessViolation here

delete shipTypeMap[0];

getchar();

return 0;
}

Frank


sf*****@gmail.com 写道:

我有一些代码需要在静态范围内声明一个STL Map,

在函数中将值插入到地图中e class并访问其他类函数中的

值。这看起来像是一个非常好的b $ b b直接请求,但是当我实现它时,当我尝试访问它时,我得到了垃圾

数据,或者我得到了一个Access Violation运行时

错误(取决于我的代码状态,而我试图找出如何使用这个非常简单的想法来工作)。有没有搞错?!什么

我做错了什么?下面是一些示例代码,提供了访问

违规:


typedef struct _ShipType {

int a;

int b;

int c;

} ShipType;


static std :: map< int,ShipType * GT; shipTypeMap;
I have some code that needs to declare an STL Map in the static scope,
insert values into the map in the function of one class and access the
values in the functions of other classes. This seems like a pretty
straight forward request, but when I implement it I get either garbage
data when I try to access it or I get an Access Violation runtime
error (depending on the state of my code while I try to figure out how
to get this rediculously simple idea to work). What the heck?! What
am I doing wrong? Here is some sample code that gives an Access
Violation:

typedef struct _ShipType {
int a;
int b;
int c;
} ShipType;

static std::map<int, ShipType *> shipTypeMap;



^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^


我只能认为你在多个编译单元中看到了这个。

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

I can only think that you''re seeing this in more than one compilation unit.


>

void GameLoader :: LoadGame()

{

shipTypeMap [0] = new ShipType();

shipTypeMap [0] - > a = 1;

shipTypeMap [0] - > b = 2;

shipTypeMap [0] - > c = 3;

}


int main(int argc,char * argv [])

{

printf(" Callback function test.\ n);;

GameLoader * gl = new GameLoader( );


gl-> LoadGame();


std :: cout<< shipTypeMap [0] - > a< < std :: endl; //< ----- 0xC000000005访问

违反此处


删除shipTypeMap [0];


getchar();


返回0;

}
>
void GameLoader::LoadGame()
{
shipTypeMap[0] = new ShipType();
shipTypeMap[0]->a = 1;
shipTypeMap[0]->b = 2;
shipTypeMap[0]->c = 3;
}

int main(int argc, char* argv[])
{
printf("Callback function test.\n");

GameLoader * gl = new GameLoader();

gl->LoadGame();

std::cout<<shipTypeMap[0]->a<<std::endl;//<-----0xC000000005 Access
Violation here

delete shipTypeMap[0];

getchar();

return 0;
}



下面的代码按预期编译和运行。

#include< map>

#include< iostream>


typedef struct _ShipType {

int a;

int b;

int c;

} ShipType;


static std :: map< int,ShipType * shipTypeMap;


struct GameLoader

{

void LoadGame();

};


void GameLoader :: LoadGame()

{

shipTypeMap [0] = new ShipType();

shipTypeMap [0] - > a = 1;

shipTypeMap [0] - > b = 2;

shipTypeMap [0] - > c = 3;

}


int main(int argc,char * argv [])

{

printf(" Callback function test.\ n);


GameLoader * gl = new GameLoader();


gl-> LoadGame();


std :: cout<< shipTypeMap [0] - > a<< std :: endl;


删除shipTypeMap [0];


getchar();


返回0;

}

The code below compiles and runs as expected.
#include <map>
#include <iostream>

typedef struct _ShipType {
int a;
int b;
int c;
} ShipType;

static std::map<int, ShipType * shipTypeMap;

struct GameLoader
{
void LoadGame();
};

void GameLoader::LoadGame()
{
shipTypeMap[0] = new ShipType();
shipTypeMap[0]->a = 1;
shipTypeMap[0]->b = 2;
shipTypeMap[0]->c = 3;
}

int main(int argc, char* argv[])
{
printf("Callback function test.\n");

GameLoader * gl = new GameLoader();

gl->LoadGame();

std::cout<<shipTypeMap[0]->a<<std::endl;

delete shipTypeMap[0];

getchar();

return 0;
}


这篇关于STL地图范围问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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