将元素插入到全局映射中时访问冲突 [英] Access violation when inserting element into global map

查看:108
本文介绍了将元素插入到全局映射中时访问冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图调试这个小时,没有运气。我知道你们会在几分钟内解决这个问题,所以这里的情况:



我有〜400 .cpp / .h文件,名为ProblemX.cpp / ProblemX。 h(其中X是1至400的数)。每个文件包含一个数学相关问题的解决方案。我想让这些问题在编译时注册到具有唯一键的全局映射(只是一个int将工作),并且该值是指向启动数学问题解决方案的函数的指针。



全局映射是在名为Problem.h / Problem.cpp的文件中创建和处理的。但是,当第一个问题尝试在地图中自我注册时,我收到一个访问冲突读取位置0x00000004。代码如下:



在ProblemX.h文件中(问题1启动此问题的解决方案):

  #ifndef PROBLEM1_H 
#define PROBLEM1_H

#includeProblems.h
#include< string&

std :: string problem1();
static int rc1 = registerProblem(1,problem1);

#endif

在Problems.h文件使用全局映射来调用适当的函数指针):

  #ifndef PROBLEMS_H 
#define PROBLEMS_H

#include< string>

int registerProblem(int problemNum,std :: string(* problemFunc)(void));
std :: string problemFinder(int problemNum);

#endif

在Problems.cpp中:

  #includeProblems.h
#include< iostream>
#include< map>

using namespace std;

map< int,std :: string(*)(void)>问题

int registerProblem(int problemNum,string(* problemFunc)(void)){
int rc = 0;
problems_map [problemNum] = problemFunc;
return rc;
}


string problemFinder(int problemNum){
string retStr =;
retStr = problems_map [problemNum]();
return retStr;
}

访问冲突发生在problems_map [problemNum] = problemFunc; / p>

感谢!

解决方案



为了避免静态初始化顺序fiasco,make不能保证在全局变量之前构造全局变量 problems_map a static static,将首次使用它:

  map< int,std :: string(*)(void)& 
get_problems_map()
{
static map< int,std :: string(*)(void)>问题
return problems_map;
}

然后使用它:

  get_problems_map()[problemNum] = problemFunc; 

这可以确保地图在需要时立即创建,而不仅仅是当< c $ c> Problems.cpp get run,在全局 rc1 变量初始化之后,这可能是(在你的情况下肯定是) / p>

I've been trying to debug this for hours now with no luck. I know you guys will solve the problem within minutes, so here's the situation:

I've got ~400 .cpp/.h files called ProblemX.cpp/ProblemX.h (where X is a number from 1 to 400). Each file contains the solution for a math related problem. I would like to have the problems register themselves at compile time to a global map with a unique key (just an int will work) and have the value be a pointer to the function that kicks off the math problem solution.

The global map is created and handled in files called Problem.h/Problem.cpp. However, I'm getting an "Access violation reading location 0x00000004" when the first Problem attempts to self-register in the map. The code is as follows:

In the ProblemX.h files (problem1 kicks off the solution for this problem):

#ifndef PROBLEM1_H
#define PROBLEM1_H

#include "Problems.h"
#include <string>

std::string problem1();
static int rc1 = registerProblem(1, problem1);

#endif

In the Problems.h file (problemFinder is the function that uses the global map to call the appropriate function pointer):

#ifndef PROBLEMS_H
#define PROBLEMS_H

#include <string>

int registerProblem(int problemNum, std::string (*problemFunc)(void));
std::string problemFinder(int problemNum);

#endif

In Problems.cpp:

#include "Problems.h"
#include <iostream>
#include <map>

using namespace std;

map<int,std::string (*)(void)> problems_map;

int registerProblem(int problemNum, string (*problemFunc)(void)) {
    int rc = 0;
    problems_map[problemNum] = problemFunc;
    return rc;
}


string problemFinder(int problemNum) {
    string retStr = "";
    retStr = problems_map[problemNum]();
    return retStr;
}

The access violation occurs where "problems_map[problemNum] = problemFunc;".

Thanks!

解决方案

As the enigmatically named user93353 answered, the problems_map global is not guaranteed to be constructed before globals in other files.

To avoid the static initialization order fiasco, make problems_map a local static, that will be init'd on its first use:

map<int,std::string (*)(void)>&
get_problems_map()
{
  static map<int,std::string (*)(void)> problems_map;
  return problems_map;
}

Then use it like so:

get_problems_map()[problemNum] = problemFunc;

This ensures the map gets created as soon as it's needed, not just when the global constructors from Problems.cpp get run, which might be (and in your case definitely is) after the global rc1 variable gets initialised.

这篇关于将元素插入到全局映射中时访问冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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