How can I zero just the padding bytes of a class?

Ben Hymers

I want to set the padding bytes of a class to 0, since I am saving/loading/comparing/hashing instances at a byte level, and garbage-initialised padding introduces non-determinism in each of those operations.

I know that this will achieve what I want (for trivially copyable types):

struct Example
{
    Example(char a_, int b_)
    {
        memset(this, 0, sizeof(*this));
        a = a_;
        b = b_;
    }
    char a;
    int b;
};

I don't like doing that though, for two reasons: I like constructor initialiser lists, and I know that setting the bits to 0 isn't always the same as zero-initialisation (e.g. pointers and floats don't necessarily have zero values that are all 0 bits).

As an aside, it's obviously limited to types that are trivially copyable, but that's not an issue for me since the operations I listed above (loading/saving/comparing/hashing at a byte level) require trivially copyable types anyway.

What I would like is something like this [magical] snippet:

struct Example
{
    Example(char a_, int b_) : a(a_), b(b_)
    {
        // Leaves all members alone, and sets all padding bytes to 0.
        memset_only_padding_bytes(this, 0);
    }
    char a;
    int b;
};

I doubt such a thing is possible, so if anyone can suggest a non-ugly alternative... I'm all ears :)

Sneftel

There's no way I know of to do this fully automatically in pure C++. We use a custom code generation system to accomplish this (among other things). You could potentially accomplish this with a macro to which you fed all your member variable names; it would simply look for holes between offsetof(memberA)+sizeof(memberA) and offsetof(memberB).

Alternatively, serialize/hash on a memberwise basis, rather than as a binary blob. That's ten kinds of cleaner.

Oh, one other option -- you could provide an operator new which explicitly cleared the memory before returning it. I'm not a fan of that approach, though..... it doesn't work for stack allocation.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

How can I add padding to the intrinsic content size of UILabel?

来自分类Dev

How can I *only* get the number of bytes available on a disk in bash?

来自分类Dev

How can I modify this countdown to not go below zero?

来自分类Dev

How can I determine the default encoding in a portable class library?

来自分类Dev

How can I copy a templated instance with a virtual base class?

来自分类Dev

In PHPUnit, how can I mock a function that is not part of a class?

来自分类Dev

How can I test a final class with private constructor?

来自分类Dev

Can I create a class which can be unpacked?

来自分类Dev

How can i enable true ToolStripMenuItem in form1 when backgroundworker completed his operation in a new class?

来自分类Dev

How can I define an UUID for a class, and use __uuidof, in the same way for g++ and Visual C++?

来自分类Dev

How can I apply a jQuery function to all elements with the same class.?

来自分类Dev

Can I pass a Class type as a procedure parameter

来自分类Dev

Why I can instantiate a static inner class?

来自分类Dev

Can I create a class [] operator in Typescript

来自分类Dev

why can I only read 2048 bytes at a time from an okhttp.Response InputStream?

来自分类Dev

Can I / Should I serialize a constant variable in a C# class?

来自分类Dev

I can "pickle local objects" if I use a derived class?

来自分类Dev

How to write a class that may accept a function pointer and/or functor just like a smart pointer does for custom deleter?

来自分类Dev

How can I cancel an ngEvent?

来自分类Dev

How can I return a function?

来自分类Dev

How can references to private class members be dangerouse

来自分类Dev

How do I parse a RestSharp response into a class?

来自分类Dev

Can I make virtual abstract class throw an exception

来自分类Dev

Do I need to set type face-manually to roboto-light if i am targeting api 11+ or can I just use android:fontFamily="sans-serif-light"

来自分类Dev

Why can't I use alias from a base class in a derived class with templates?

来自分类Dev

how to remove padding in decryption in c#

来自分类Dev

How can I create a histogram in R?

来自分类Dev

How can I select with column of lists

来自分类Dev

How can I extract text from images?

Related 相关文章

  1. 1

    How can I add padding to the intrinsic content size of UILabel?

  2. 2

    How can I *only* get the number of bytes available on a disk in bash?

  3. 3

    How can I modify this countdown to not go below zero?

  4. 4

    How can I determine the default encoding in a portable class library?

  5. 5

    How can I copy a templated instance with a virtual base class?

  6. 6

    In PHPUnit, how can I mock a function that is not part of a class?

  7. 7

    How can I test a final class with private constructor?

  8. 8

    Can I create a class which can be unpacked?

  9. 9

    How can i enable true ToolStripMenuItem in form1 when backgroundworker completed his operation in a new class?

  10. 10

    How can I define an UUID for a class, and use __uuidof, in the same way for g++ and Visual C++?

  11. 11

    How can I apply a jQuery function to all elements with the same class.?

  12. 12

    Can I pass a Class type as a procedure parameter

  13. 13

    Why I can instantiate a static inner class?

  14. 14

    Can I create a class [] operator in Typescript

  15. 15

    why can I only read 2048 bytes at a time from an okhttp.Response InputStream?

  16. 16

    Can I / Should I serialize a constant variable in a C# class?

  17. 17

    I can "pickle local objects" if I use a derived class?

  18. 18

    How to write a class that may accept a function pointer and/or functor just like a smart pointer does for custom deleter?

  19. 19

    How can I cancel an ngEvent?

  20. 20

    How can I return a function?

  21. 21

    How can references to private class members be dangerouse

  22. 22

    How do I parse a RestSharp response into a class?

  23. 23

    Can I make virtual abstract class throw an exception

  24. 24

    Do I need to set type face-manually to roboto-light if i am targeting api 11+ or can I just use android:fontFamily="sans-serif-light"

  25. 25

    Why can't I use alias from a base class in a derived class with templates?

  26. 26

    how to remove padding in decryption in c#

  27. 27

    How can I create a histogram in R?

  28. 28

    How can I select with column of lists

  29. 29

    How can I extract text from images?

热门标签

归档