使用幻数初始化内存块的简明方法

时间:2011-09-27 21:09:02

标签: c++ initialization

我所指的几个例子:

typedef struct SOME_STRUCT {
  unsigned int x1;
  unsigned int x2;
  unsigned int x3;
  unsigned int x4;

  // What I expected would work, but doesn't; the 2nd parameter gets
  // turned into an 8-bit quantity at some point within memset
  SOME_STRUCT() { memset( this, 0xFEEDFACE, sizeof( *this ) ); }

  // Something that worked, but seems hokey/hackish
  SOME_STRUCT() {
    unsigned int *me = (unsigned int *)this;
    for( int ii = 0; ii < sizeof(*this)/sizeof(*me); ++ii ) {
      me[ii] = 0xFEEDFACE;
    }
  }

  // The far-more-verbose-but-C++-way-of-doing-it
  // This works, but doesn't lend itself very well
  // to being a drop-in way to pull this off on
  // any struct.
  SOME_STRUCT() :  x1( 0xFEEDFACE )
                 , x2( 0XFEEDFACE )
                 , x3( 0XFEEDFACE )
                 , x4( 0XFEEDFACE ) {}

  // This would work, but I figured there would be a standard
  // function that would alleviate the need to do it myself
  SOME_STRUCT() { my_memset( this, 0xFEEDFACE, sizeof(*this) ); }
}

我不能在这里使用valgrind,并且我的选项仅限于我可以访问的各种调试库 - 这就是我为这个一次性案例自己做的原因。

7 个答案:

答案 0 :(得分:4)

以下是安全使用std::generate()的部分示例:

#include <algorithm>

struct Wizard {
    size_t i;
    static unsigned char magic[4];
    Wizard() : i(0) {}
    unsigned char operator()() {
        size_t j = i++;
        i %= sizeof(magic); // Not strictly necessary due to wrapping.
        return magic[j];
    }
};

unsigned char Wizard::magic[4] = {0xDE,0xAD,0xBE,0xEF};

std::generate(reinterpret_cast<unsigned char*>(this),
              reinterpret_cast<unsigned char*>(this) + sizeof(*this),
              Wizard());

(当然,结尾可能是也可能不对,取决于你的看法以及你期望看到的内容!)

答案 1 :(得分:2)

我会声明这个构造函数:

SOME_STRUCT( unsigned int magic) : x1 (magic), x2 (magic), x3 (magic), x4 (magic) {}

这与您的第三个选项非常相似,似乎是C ++的自然方式。

答案 2 :(得分:2)

其他人没有提出的一点是:

我认为对非POD类型执行此操作是不安全的。具有讽刺意味的是,将初始化添加到构造函数中会使其成为非pod。因此,我提出了一个独立的函数来静态检查POD-ness(样本使用c ++ 0x type_traits,但你也可以使用Boost)

#include <iostream>
#include <type_traits>

template <typename T>
    typename std::enable_if<std::is_pod<T>::value>::type* FeedFace(T& v)
{
    static const unsigned char MAGIC[] = { 0xFE, 0xED, 0xFA, 0xCE };
    unsigned char *me = reinterpret_cast<unsigned char *>(&v);
    for( size_t ii = 0; ii < sizeof(T)/sizeof(unsigned char); ++ii ) 
        me[ii] = MAGIC[ii % sizeof(MAGIC)/sizeof(unsigned char)];
}

struct Pod { char data[37]; };
struct NonPod : Pod { virtual ~NonPod() { } };

int main()
{
    Pod pod;
    FeedFace(pod);

    NonPod nonpod;
    // FeedFace(nonpod); // fails to compile (no matching function call)

    return 0;
}

答案 3 :(得分:1)

我认为这允许讨厌的hacky东西,像这样:

#include <iomanip>
#include <iostream>
#include <algorithm>
using namespace std;
int main(void)
{
    struct SOME_STRUCT {
        unsigned int x1;
        unsigned int x2;
        unsigned int x3;
        unsigned int x4;
    } foo;
    fill(reinterpret_cast<unsigned int *>(&foo),
         reinterpret_cast<unsigned int *>(&foo) + sizeof(foo) / sizeof(unsigned int),
         (unsigned int)0xDEADBEEF);
    cout << foo.x1 << endl;
    cout << foo.x2 << endl;
    cout << foo.x3 << endl;
    cout << foo.x4 << endl;
    return (0);
}

基本上滥用带指针强制转换的std :: fill()。

答案 4 :(得分:1)

您可以reinterpret_cast this作为char*,然后将std::generate与谓词一起使用,该谓词可以旋转您关注的值。如果我稍后有时间,我会尝试草拟代码。

你还考虑过一个LD_PRELOAD内存检查malloc库吗?

答案 5 :(得分:0)

这是另一种hacky方法。

SOME_STRUCT() {
    x1 = 0xFEEDFACE;
    memmove(&(this->x2), this, sizeof(*this)-sizeof(x1));
}

答案 6 :(得分:0)

即使您的memset()尝试确实有效,它也会对结构包装做出假设,因此无法保证其正确无误。没有编程方法来迭代结构的成员并用C或C ++分配它们。因此,您需要满足于单独分配成员。话虽如此,如果您觉得您对结构的内存布局感到满意并且不需要担心可移植代码,您可以使用for循环轻松初始化它。

unsigned int i, *ar = (unsigned int *)&my_struct;
for (i = 0; i < sizeof(my_struct) / sizeof(unsigned int); i++) {
  ar[i] = 0xdeadbeef;
}