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 1092111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:38:01+00:00 2026-05-16T23:38:01+00:00

This question follows a suggestion made by @sharptooth in this related question . Can

  • 0

This question follows a suggestion made by @sharptooth in this related question.

Can std::string be tweaked so that it becomes password-safe ?

If not, what would be the guidelines to write a password-handling class (thus a class that takes big care about what it writes to memory and clears it before destruction) ?

  • 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-05-16T23:38:02+00:00Added an answer on May 16, 2026 at 11:38 pm

    Yes, first define a custom allocator:

    template <class T> class SecureAllocator : public std::allocator<T>
    {
    public:
        template<class U> struct rebind { typedef SecureAllocator<U> other; };
    
        SecureAllocator() throw() {}
        SecureAllocator(const SecureAllocator&) throw() {}
        template <class U> SecureAllocator(const SecureAllocator<U>&) throw() {}
    
        void deallocate(pointer p, size_type n)
        {
            std::fill_n((volatile char*)p, n*sizeof(T), 0);
            std::allocator<T>::deallocate(p, n);
        }
    };
    

    This allocator zeros the memory before deallocating. Now you typedef:

    typedef std::basic_string<char, std::char_traits<char>, SecureAllocator<char>> SecureString;
    

    However there is a small problem, std::string may use small string optimization and store some data inside itself, without dynamic allocation. So you must explicitly clear it on destruction or allocate on the heap with our custom allocator:

    int main(int, char**)
    {
        using boost::shared_ptr;
        using boost::allocate_shared;
        shared_ptr<SecureString> str = allocate_shared<SecureString>(SecureAllocator<SecureString>(), "aaa");
    
    }
    

    This guarantees that all the data is zeroed before deallocation, including the size of the string, for example.

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

Sidebar

Related Questions

This question follows on from a previous question, that has raised a further issue.
This question follows on from a previous question. However stackoverflow presents me from commenting
This problem follows on from a previous question . When I run the following
this follows on from my last question which I thought was answered but for
G'day, This is related to my question on star developers and to this question
This question is a follow-up to the one at Can I use a language
This is again, I guess, a 'best practices' question because i can think of
I have to implement a strange requirement that goes as follows: Users can hover
FOLLOW UP TO MARC GRAVELLS SUGGESTION IN THIS QUESTION I now have something like
Edited this question entirely, not sure if it made sense previously. Ok, I am

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.