带模板的自定义内存分配器

时间:2019-03-02 16:40:09

标签: c++ templates memory allocator

我正在学习有关内存管理的一些知识,因此我想实现自己的自定义内存分配器。.首先,我创建了一个名为SMemoryAllocator的抽象类,其中只有一个派生类StackStackdAllocator。

我的问题是我的终端日志:

Undefined symbols for architecture x86_64:
"StackBasedMemory<float>::StackBasedMemory(unsigned long)", referenced from:
  _main in main.cpp.o
 "StackBasedMemory<float>::~StackBasedMemory()", referenced from:
  _main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see 
invocation)

还有我的抽象类SMemoryAllocator:

emplate<class Object>
class SMemoryAllocator
{
public:
  SMemoryAllocator(std::size_t size);

  virtual ~SMemoryAllocator();

  Object* create(std::size_t sizeOfBlock,std::size_t size);

  void free();

  virtual void add(Object*) = 0;

  virtual void remove() = 0;

protected:

  Object* _ptrOfmemory;
  size_t _sizeOfblock;
};

还有我的派生类StackBasedAllocator:

#include "SMemoryAllocator.h"

template<class Object>
class StackBasedMemory : public SMemoryAllocator<Object>
{
public:
  StackBasedMemory(std::size_t sizeOfBlock = 64);

  ~StackBasedMemory();

  virtual void add(Object* obj);

  virtual void remove();

private:
  Object* _currentMarker;
  unsigned int _iterOfblock;
};

和我的主要功能:

#include "FileLogger.h"
#include "StackBasedMemory.h"

int main(int argc,char** argv)
{
  std::cout << "hello S2Engine" << std::endl;
  //SLogger::setLog(new FileLogger());

  StackBasedMemory<float> test;

  //SLogger::kill();
  return 0;
}

我知道有些人会告诉我使用std的分配器,但我只是想学习如何自己做..谢谢您让我知道是否要查看我的.cpp文件。.

0 个答案:

没有答案