4

我启动了搜索引擎,但找不到我的问题的相应答案:

基本上我想要一个地图,每个条目都包含一个结构列表。一个结构本身包含 2 个std::string变量和一个std::list<string>.

尽管访问了结构中的列表,但一切都按预期工作。

一种方法(此处为:getRules)在必要时创建一个映射条目,并为其附加一个列表,并向其添加一个结构(此处为:Rule)元素。在此方法中,调用了另一个方法(此处为:getRuleParams),该方法应负责将元素添加到结构内的列表中。

在 getRuleParams 方法中,添加元素的列表可以通过 struct 元素正确访问。在“环绕”方法中,添加元素的列表也可以通过结构元素直接访问。

但是,如果我想访问结构的列表,或者更好的是列表元素,那么奇怪的事情就会发生。我发现地址发生了变化(结构,结构内的列表......)

您可以在我的代码产生的输出中看到(假设它编码正确)。

由于我是 C++ 新手,也许我错过了一些非常简单/必要的东西。它是否与一些被调用的复制构造函数有关,但为什么只使用指向所有“变量”的指针的行为相同?

我真的希望这里有人可以启发我。

我尽可能地简化/隔离了我的问题。(方法的签名必须是这样的,例如void*,因为我依赖于其他功能)

请参阅下面的代码并提前致谢:

#include <list>
#include <map>
#include <string>
#include <iostream>

using namespace std;

struct Rule {
    string target;
    string action;
    list<string> params;
};

static int getRuleParams(void *prule) {
    Rule* rule = (Rule*) (prule);
    string param = "Entry!";

    //Fill struct with ruleparams
    rule->params.push_back(param);
    cout << "Method getRuleParams()\n" << "parameter: "
    << *(rule->params.begin()) << " \nadress of rule:\t\t\t" << rule
    << " \nadress of rule.params:\t\t" << &(rule->params) << std::endl
    << std::endl;
    return 0;
}

static int getRules(void *dbpolicies) {
    string policy = "FileIO";
    string target = "TARGET";
    string action = "DENY";

    std::map<std::string, std::list<Rule> >* policies = (std::map<std::string,
                                                         std::list<Rule> >*) (dbpolicies);

    //Create std::list<DBRule> (list) for Policy Map-Entry, if not existing
    if ((*policies).find(policy) == (*policies).end())
        (*policies)[policy] = std::list<Rule>();

    //Fill struct
    Rule rule = { target, action };
    (*policies).find(policy)->second.push_back(rule);

    //call Method which manipulates params in struct
    getRuleParams(&rule);

    cout << "Method getRulesforPackage() (access rule directly):";
    cout << "\nparameter = " << *(rule.params.begin())
    << "\naddress of rule:\t\t" << &rule;
    cout << "\nadress of rule.params:\t\t" << &(rule.params) << std::endl
    << std::endl;

    cout << "Method getRulesforPackage() access rule via map:" << std::endl;

    //READ params from map entry -> EVIL
    std::list<Rule> dbrules = (*policies).find(policy)->second;
    Rule firstrule = *dbrules.begin();
    std::list<std::string> dbruleparams = firstrule.params;
    string ruleparam = *(firstrule.params.begin()); //EVIL!
    //  string ruleparam = *(dbruleparams.begin()); // <- REALLY EVIL! (program crashes)

    //Variant 2: pointers only
    std::list<Rule>* dbrules2 = &(*policies).find(policy)->second;
    Rule* firstrule2 = &*(dbrules2->begin());
    std::list<std::string>* dbruleparams2 = &(firstrule2->params);


    cout << "rule.target = " << firstrule.target << "\nrule.action = "
    << firstrule.action << std::endl;
    cout << "address of rule:\t\t" << &firstrule << std::endl;
    cout << "address of rule (pointer):\t" << firstrule2 << std::endl;

    //  string ruleparam2 = *(dbruleparams2->begin()); //REALLY EVIL! (program crashes)

    //  cout << "parameter: " << ruleparam << std::endl;
    //  cout << "parameter (pointer): " << ruleparam2 << std::endl;


    return 0;
}

static std::map<std::string, std::list<Rule> > getPolicies() {
    std::map<std::string, std::list<Rule> > policies;
    getRules(&policies);
    return policies;

}

int main() {
    std::map<std::string, std::list<Rule> > policies = getPolicies();
}
4

1 回答 1

3

这本身就是一个重大问题:

Rule rule = { target, action };
(*policies).find(policy)->second.push_back(rule);

//call Method which manipulates params in struct
// **NOTE**: Fills the local variable "rule", not the one just pushed into the list
getRuleParams(&rule); 

尽管我不一定建议使用此模型,但您可以通过以下方式解决此问题:

Rule rule = { target, action };
getRuleParams(&rule); // fill here, before the push

// then push.
(*policies).find(policy)->second.push_back(rule);

但要正确执行此操作,您应该将空白规则推入列表,然后引用规则对象(列表中的那个)进行初始化:

Rule rule = { target, action };
(*policies)[policy].push_back(rule);
getRuleParams(&(*policies)[policy].back());

老实说,范式本身需要做一些工作,这里有很多不必要的find()事情,有条不紊地使用引用和迭代器可以清理很多。


返工示例:

以下已剥离所有输出子句。他们实际上使发现问题变得更加困难。请查看以下内容,希望能给您一些想法:

#include <list>
#include <map>
#include <string>
#include <iostream>

using namespace std;

// basic rule with strin parameter list
typedef std::list<std::string> ParamList;
struct Rule
{
    string target;
    string action;
    ParamList params;
};

typedef std::list<Rule> RuleList;
typedef std::map<std::string, RuleList> MapStringToRuleList;

static int getRuleParams(void *prule)
{
    Rule* rule = (Rule*) (prule);
    rule->params.push_back("Entry!");
    return 0;
}

static int getRules(void *dbpolicies) {

    string policy = "FileIO";
    string target = "TARGET";
    string action = "DENY";

    MapStringToRuleList* policies = (MapStringToRuleList*)(dbpolicies);

    // push a new rule into the list.
    Rule rule = {target, action};
    RuleList& rules = (*policies)[ policy ];
    rules.push_back(rule);

    //call Method which manipulates params in struct
    getRuleParams(&rules.back());

    // for each rule in our policy's rule list...
    for (RuleList::const_iterator rit = rules.begin(); rit != rules.end(); rit++)
    {
        cout << "Rule: " << policy << endl;

        // for each rule in our curent rule's params list
        const ParamList& params = rit->params;
        for (ParamList::const_iterator pit = params.begin(); pit != params.end(); pit++)
        {
            cout << "  Rule Param: " << *pit << endl;
            // TODO: use current param for something.
        }            
    }
    return 0;
}

static MapStringToRuleList getPolicies()
{
    MapStringToRuleList policies;
    getRules(&policies);
    return policies;
}

int main()
{
    MapStringToRuleList policies = getPolicies();
}
于 2013-01-10T04:05:13.533 回答