Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8481805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:39:34+00:00 2026-06-10T19:39:34+00:00

It is a common pattern to use templates to enforce the compiler to initialize

  • 0

It is a common pattern to use templates to enforce the compiler to initialize primitive / POD types values (https://stackoverflow.com/a/11493744/16673 or http://www.codeproject.com/Articles/825/Using-templates-for-initialization).

Does a similar pattern exist that could be used to erase the value once it goes out of scope for security reasons, to make sure the value is not left on the stack once the variable is destructed? I am afraid a naive analogous implementation might not work, as the compiler is free to ignore any assignments to a value which is going out of scope, as the value can be trivially proven not to be used any more. Is there some consistent and reasonably portable solution e.g. using volatile?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T19:39:36+00:00Added an answer on June 10, 2026 at 7:39 pm

    You could use some c++11 features to make this more portable, but this may suffice as a starting point:

    Class

    template<typename T>
    class t_secure_destruct {
      static const size_t Size = sizeof(T);
      static const size_t Align = alignof(T);
    public:
      t_secure_destruct() : d_memory() {
        new(this->d_memory)T;
      }
    
      ~t_secure_destruct() {
        reinterpret_cast<T*>(this->d_memory)->~T();
        this->scribble();
      }
    
      // @todo implement or delete op-assign and remaining constructors
    
    public:
      T& get() {
        return *reinterpret_cast<T*>(this->d_memory);
      }
    
      const T& get() const {
        return *reinterpret_cast<const T*>(this->d_memory);
      }
    
    private:
      void scribble() {
        for (size_t idx(0); idx < Size; ++idx) {
          this->d_memory[idx] = random();
        }
      }
    
    private:
      __attribute__((aligned(Align))) char d_memory[Size];
    };
    

    Demo

    #include <iostream>
    
    class t_test {
    public:
      t_test() : a(-1) {
        std::cout << "construct\n";
      }
    
      ~t_test() {
        std::cout << "destruct\n";
      }
    
    public:
      void print() const {
        std::cout << "a = " << a << "\n";
      }
    
    public:
      int a;
    };
    
    int main(int argc, const char* argv[]) {
      t_secure_destruct<t_test>test;
      test.get().print();
      test.get().a = 100;
      test.get().print();
      return 0;
    }
    

    Of course, you could also back that allocation with a heap allocation, if you favor. If you need to outsmart an optimizer, you may need to put the scribbler out of its reach.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it a common pattern/idiom to use free functions as pseudo-constructors to avoid having
One common pattern I see and use frequently in C++ is to temporarily set
I just found out, that it seems a common pattern to use UpperFirstLetterPascalCase() for
It seems that common logging pattern in scala is to use a Logging trait
I wonder what folks use as a common data access pattern on Android? Content
One common pattern when caching with Django is to use the current site's ID
I've noticed that a common pattern I use is to assign SomeClass.__init__() arguments to
This is becoming a common pattern in my code, for when I need to
I'm working with a fairly common pattern - a page opens a child window
Data Access Objects (DAOs) are a common design pattern, and recommended by Sun. But

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.