小编Dav*_*Woo的帖子

使用自定义分配器但没有其他args的std :: function构造函数有什么意义?

我正在使用std :: function和自定义分配器,但是当我没有为初始仿函数提供函数时,它的行为不像我预期的那样.

当我向构造函数提供自定义分配器但没有初始函子时,分配器从未使用过或者看起来如此.

这是我的代码.

//Simple functor class that is big to force allocations
struct Functor128
{
    Functor128()
    {}

    char someBytes[128];

    void operator()(int something)
    {
        cout << "Functor128 Called with value " << something << endl;
    }
};

int main(int argc, char* argv[])
{
Allocator<char, 1> myAllocator1;
Allocator<char, 2> myAllocator2;
Allocator<char, 3> myAllocator3;
Functor128 myFunctor;

cout << "setting up function1" << endl;
function<void(int)> myFunction1(allocator_arg, myAllocator1, myFunctor);
myFunction1(7);

cout << "setting up function2" << endl;
function<void(int)> myFunction2(allocator_arg, myAllocator2);
myFunction2 …
Run Code Online (Sandbox Code Playgroud)

c++ c++11 std-function

9
推荐指数
1
解决办法
764
查看次数

C++使用放置新的未定义行为构造对象两次?

我遇到了一些让我感到恐惧的代码.基本上它遵循这种模式:

class Foo
{
  public:
    //default constructor
    Foo(): x(0), ptr(nullptr)  
    {
      //do nothing
    }

    //more interesting constructor
    Foo( FooInitialiser& init): x(0), ptr(nullptr) 
    {
      x = init.getX();
      ptr = new int;
    }
    ~Foo()
    {
      delete ptr;
    }

  private:
    int x;
    int* ptr;
};


void someFunction( FooInitialiser initialiser )
{
   int numFoos = MAGIC_NUMBER;
   Foo* fooArray = new Foo[numFoos];   //allocate an array of default constructed Foo's

   for(int i = 0; i < numFoos; ++i)
   {
       new( fooArray+ i) Foo( initialiser );    //use …
Run Code Online (Sandbox Code Playgroud)

c++ placement-new new-operator language-lawyer

6
推荐指数
1
解决办法
761
查看次数

在编译时捕获std :: function分配

我想只允许在我的代码库中使用std :: function,如果它不进行任何分配.

为此我可以编写类似下面的函数的东西,只用它来创建我的函数实例:

template< typename Functor>
std::function<Functor> makeFunction( Functor f)
{
    return std::function<Functor>(std::allocator_arg, DummyAllocator(), f);
}
Run Code Online (Sandbox Code Playgroud)

如果DummyAllocator在运行时被使用,它将断言或抛出.

理想情况下,我想在编译时捕获分配用例.

template< typename Functor>
std::function<Functor> makeFunction( Functor f)
{
   static_assert( size needed for function to wrap f < space available in function, 
   "error - function will need to allocate memory");

   return std::function<Functor>(f);
 }
Run Code Online (Sandbox Code Playgroud)

这样的事情可能吗?

c++ dynamic-memory-allocation c++11 std-function

6
推荐指数
1
解决办法
397
查看次数

反调试 - 用于int 3断点检测的calc函数内存占用

我正在研究本文中列出的一些简单的反调试措施 http://www.codeproject.com/Articles/30815/An-Anti-Reverse-Engineering-Guide#BpMem

我已经在给定函数中实现了对int 3断点的简单检查,以便如果在thisIsADummyFunction中的任何位置设置断点,则函数testForInt3Breakpoints返回true .

int thisIsADummyFunction()
{
    int i = rand();
    ++i;  
    return i;
}

bool testForInt3Breakpoints()
{  
    bool breakPointPresent = false;
    unsigned char* memPtr = reinterpret_cast<unsigned char*>( thisIsADummyFunction );

    auto size = 0x16; //this value determined by manual inspection of compiled code

    for ( size_t i = 0; i < size; i++ ) {
        if ( memPtr[ i ] == 0xCC ) {           //see if byte equals int 3 instruction
            breakPointPresent = true;
            break; …
Run Code Online (Sandbox Code Playgroud)

c++ windows visual-studio

5
推荐指数
1
解决办法
470
查看次数