Fer*_*eak 1 c++ linker static templates g++
我遇到了以下奇怪的情况:我设法有一个带有两个实例的全局静态变量......这是正常的还是这是编译器中的一个错误,还是这个隐藏的C++领域?下面的复制是一个较大项目的摘录(行为是相同的),显然名称被更改以保护罪魁祸首(是的,我知道此代码中存在内存泄漏).
代码如下:
// other.h
#ifndef _OTHER_H_
#define _OTHER_H_
struct other
{
long longer;
int inter;
char charer;
};
void dosomething();
#endif
Run Code Online (Sandbox Code Playgroud)
和
// other.cpp
#include "other.h"
#include "util.h"
void dosomething()
{
other* something = alloc_mem(other, 4);
}
Run Code Online (Sandbox Code Playgroud)
和
// util.h
#ifndef _UTIL_H_
#define _UTIL_H_
#include <memory.h>
#include <string>
#include "test_class.h"
template <class T> T* allocate(size_t count, const char* f, long l, const char* sth)
{
T* tmp = new T[count];
memset(tmp, 0, count * sizeof(T));
test_class<T*>::instance().throwIn(tmp, f, l, sth, count);
return tmp;
}
#define alloc_mem(type,count) allocate<type>(count, __FILE__, __LINE__, (char*)0)
#endif
Run Code Online (Sandbox Code Playgroud)
和
// main.cpp
#include "other.h"
#include "util.h"
int main()
{
int* i = alloc_mem(int, 1);
int* i1 = alloc_mem(int, 20);
char* c = alloc_mem(char, 1);
dosomething();
int* i3 = alloc_mem(int, 1);
}
Run Code Online (Sandbox Code Playgroud)
主要部分:
// test_class.h
#ifndef test_class_H
#define test_class_H
#include <stdlib.h>
#include <iostream>
#include <typeinfo>
#include <cxxabi.h>
static long int all_alloc = 0; // THIS will get linked in two times!
template <typename T>
class test_class
{
private:
test_class() {}
static test_class<T>* pinstance;
public:
~test_class() {}
static test_class& instance() {
if(pinstance == NULL) {
pinstance = new test_class();
}
return *pinstance;
}
void throwIn(T item, const char* file, long line, const char* _compiler, long count) {
int status;
char* s = abi::__cxa_demangle(typeid(T).name(), 0, 0, &status) ;
std::cout << "request:" << sizeof(T) * count << " bytes, type:" << s << " @ "<<
file << ":" << line << " global_addr:" << &all_alloc << std::endl;
all_alloc += sizeof(T) * count ;
free(s);
std::cout<<"All memory:" << all_alloc << std::endl;
}
};
template <class T> test_class<T>* test_class<T>::pinstance = NULL;
#endif
Run Code Online (Sandbox Code Playgroud)
所以,你必须编译为:
g++ main.cpp other.cpp -o test
Run Code Online (Sandbox Code Playgroud)
并运行它,并:
$ ./test
request:8 bytes, type:int* @ main.cpp:6 global_addr:0x6022d8
All memory:8
request:160 bytes, type:int* @ main.cpp:7 global_addr:0x6022d8
All memory:168
request:8 bytes, type:char* @ main.cpp:8 global_addr:0x6022d8
All memory:176
request:32 bytes, type:other* @ other.cpp:6 global_addr:0x6022f8
All memory:32
request:8 bytes, type:int* @ main.cpp:11 global_addr:0x6022d8
All memory:184
Run Code Online (Sandbox Code Playgroud)
所以,正如我可以看到一个非常大的惊喜,我有两个全球地址all_alloc...确实,nm -C test显示:
00000000006022d8 b all_alloc
00000000006022f8 b all_alloc
Run Code Online (Sandbox Code Playgroud)
所以,显然问题是:
为什么?这怎么可能?有没有允许这种行为的东西,或者这是编译器或链接器中某处的错误?
在这种情况下,我认为你不想声明你的all_alloc静态.在这种情况下,这意味着链接是内部的.在您的情况下,您将获得两个副本,因为两个文件包含标题.
如果删除static,则会有两个冲突的副本,并导致链接器错误.哪个不好.
我相信你想要做的是改变static对extern,然后在一个 CPP文件中,定义的变量及其值.
标题:
extern long int all_alloc;
Run Code Online (Sandbox Code Playgroud)
CPP:
long int all_alloc = 0;
Run Code Online (Sandbox Code Playgroud)
这将提供all_alloc您的代码之间共享的一个副本.