静态插入时有关stl Map核心的信息 [英] about stl Map core when static insert

查看:113
本文介绍了静态插入时有关stl Map核心的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

dll.cpp:

#include <stdio.h>

#ifdef __cplusplus
extern "C" {
#endif

void show(){
    fprintf(stdout,"%s","dll show call.");
}

#ifdef __cplusplus
}
#endif


dll_1.cpp:


dll_1.cpp:

#include "dll.h"

map<string,value> g_map;

int register_funk(string xx,fn_cmd_handler_t fn, string dex) { // removed obscenity
    fprintf(stdout,"%s","core begin\n");
    fprintf(stdout,"%u",g_map.size());
    fprintf(stdout,"%s","core end\n");
    fprintf(stdout,"-------fn-%p",fn);
    fprintf(stdout,"%s","----\n");
    fprintf(stdout,"%s","----\n");
    g_map[xx].fn = fn; //---------->core at this, why ?? who can tell me????
    g_map[xx].des = dex;
    return 0;
}


dll_2.cpp:


dll_2.cpp:

#include "dll.h"

int samxxxx( int machine_id, vector<string> &vecValue );

static int __inss = register_funk("xxxx",samxxxx,"funk"); // removed obscenity

int samxxxx( int machine_id, vector<string> &vecValue )
{
    fprintf(stdout,"%s","ssss--------->");
    return 0;
}


dll.h:


dll.h:

#ifndef __xxxx__
#define __xxxx__

#include ,map>
#include <vector>
#include <string>
#include <stdio.h>
using namespace std;
typedef int ( *fn_cmd_handler_t )( int machine_id, vector<string> &vecValue );

typedef struct {
    fn_cmd_handler_t fn;
    string des;
}VALUE;


int register_funk(string xx,fn_cmd_handler_t fn, string dex);  // removed obscenity

#endif


//我建立了这个,所以使用
//g ++ -g -O2 -shared -Wall -o a.so * .cpp

//以下是exe文件


//and i build this so use
//g++ -g -O2 -shared -Wall -o a.so *.cpp

//the follow is exe file

//main.cpp
#include <dlfcn.h>
#include <stdio.h>

int main()
{
    void *handle = dlopen("/home/samuel/Temp/funk/a.so", RTLD_LAZY);
    fprintf(stdout, "%s\n", dlerror());
    dlclose(handle);
    return 0;
}


//i建立此用途
//g ++ main.cpp -rdynamic -ldl
当我在centos中键入此内容时
> ./a.out
它核心
谁能告诉我为什么?
非常感谢:)


//i build this use
//g++ main.cpp -rdynamic -ldl
when i type this in centos
>./a.out
it core
who can tell me why?
thanks a lot :)

推荐答案

您的static int __inssmap<string,value> g_map;都是全局变量,并且未指定初始化顺序,因为它们位于单独的位置.cpp和.obj文件.在您的情况下,您并不幸运,并且__inssg_map之前进行了初始化,因此g_map在其实际初始化之前就已使用,因为__inss的初始化使用了g_map. g_map.size()不会崩溃,因为它只是减去两个指针,即使两个指针未初始化或为零也不会崩溃,但是将项目放到地图上要生存就更复杂了.您可以通过将g_map__inss放在相同的.cpp文件中并确保g_map__inss以上,因为在同一.cpp(编译单元)中,初始化顺序是从上到下,因此可以解决该问题.破坏是自下而上的.

如果您很幸运,并且g_map会在__inss之前初始化,那么当您的链接器决定交换初始化顺序时,您可能会在项目的以后遇到此错误.

我的一般建议:根本不需要使用全局变量.为您的DLL声明一个主类,并将其声明为全局变量,然后将所有内容作为成员变量放入该类中.像这样的东西:
Both your static int __inss and map<string,value> g_map; are global variables and the order of their initialization isn''t specified because they reside in separate .cpp and .obj files. In your case you were not lucky and __inss was initialized before g_map so g_map is used before its actual initialization because the initialization of __inss uses g_map. g_map.size() doesn''t crash because its just substraction of two pointers that doesn''t crash even if the pointers are uninitialized or zero but putting an item to the map is more complex to survive. You can solve the problem by putting g_map and __inss to the same .cpp file and making sure that g_map is above __inss becuase inside the same .cpp (compilation unit) the initialization order is top to bottom while the destruction is bottom to top.

If you were lucky and g_map would initialize before __inss then you would probably face this error at a later point in your project when your linker decides to swap the initialization order.

My general advice: Don''t use global variables at all if you don''t need them. Declare a main class for your DLL, and instatiate just that as a global variable and then put everything into that class as member variables. Something like this:
#include "OtherClass1.h"
#include "OtherClass2.h"

class OtherClass3;

class CDll
{
public:
   CDll();
   ~CDll();
   void func();
   void func2();

private:
   std::map<x,y> m_Map;
   int m_BlahBlah;
   int m_XXX;
   OtherClass1 m_OtherClass1;
   OtherClass2 m_OtherClass2;
   OtherClass3* m_OtehrClass3;
};

// The only static variable in the DLL
CDll g_Dll;


// functions exported by the shared lib:
void shared_dll_func()
{
   g_Dll.func();
}

void shared_dll_func2()
{
   g_Dll.func2();
}

...


您尚未在地图中添加任何值,因此无法使用索引器来引用其中的元素.我的错


不,当我使用g_map [xx]时,它将自动创建key = xx和一个值.
no , when i use g_map[xx],it will auto create key=xx and a value.


这篇关于静态插入时有关stl Map核心的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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