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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T16:45:31+00:00 2026-05-10T16:45:31+00:00

Instead of having to remember to initialize a simple ‘C’ structure, I might derive

  • 0

Instead of having to remember to initialize a simple ‘C’ structure, I might derive from it and zero it in the constructor like this:

struct MY_STRUCT {     int n1;     int n2; };  class CMyStruct : public MY_STRUCT { public:     CMyStruct()     {         memset(this, 0, sizeof(MY_STRUCT));     } }; 

This trick is often used to initialize Win32 structures and can sometimes set the ubiquitous cbSize member.

Now, as long as there isn’t a virtual function table for the memset call to destroy, is this a safe practice?

  • 1 1 Answer
  • 1 View
  • 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. 2026-05-10T16:45:32+00:00Added an answer on May 10, 2026 at 4:45 pm

    PREAMBLE:

    While my answer is still Ok, I find litb’s answer quite superior to mine because:

    1. It teaches me a trick that I did not know (litb’s answers usually have this effect, but this is the first time I write it down)
    2. It answers exactly the question (that is, initializing the original struct’s part to zero)

    So please, consider litb’s answer before mine. In fact, I suggest the question’s author to consider litb’s answer as the right one.

    Original answer

    Putting a true object (i.e. std::string) etc. inside will break, because the true object will be initialized before the memset, and then, overwritten by zeroes.

    Using the initialization list doesn’t work for g++ (I’m surprised…). Initialize it instead in the CMyStruct constructor body. It will be C++ friendly:

    class CMyStruct : public MY_STRUCT { public:     CMyStruct() { n1 = 0 ; n2 = 0 ; } }; 

    P.S.: I assumed you did have no control over MY_STRUCT, of course. With control, you would have added the constructor directly inside MY_STRUCT and forgotten about inheritance. Note that you can add non-virtual methods to a C-like struct, and still have it behave as a struct.

    EDIT: Added missing parenthesis, after Lou Franco’s comment. Thanks!

    EDIT 2 : I tried the code on g++, and for some reason, using the initialization list does not work. I corrected the code using the body constructor. The solution is still valid, though.

    Please reevaluate my post, as the original code was changed (see changelog for more info).

    EDIT 3 : After reading Rob’s comment, I guess he has a point worthy of discussion: "Agreed, but this could be an enormous Win32 structure which may change with a new SDK, so a memset is future proof."

    I disagree: Knowing Microsoft, it won’t change because of their need for perfect backward compatibility. They will create instead an extended MY_STRUCTEx struct with the same initial layout as MY_STRUCT, with additionnal members at the end, and recognizable through a "size" member variable like the struct used for a RegisterWindow, IIRC.

    So the only valid point remaining from Rob’s comment is the "enormous" struct. In this case, perhaps a memset is more convenient, but you will have to make MY_STRUCT a variable member of CMyStruct instead of inheriting from it.

    I see another hack, but I guess this would break because of possible struct alignment problem.

    EDIT 4: Please take a look at Frank Krueger’s solution. I can’t promise it’s portable (I guess it is), but it is still interesting from a technical viewpoint because it shows one case where, in C++, the "this" pointer "address" moves from its base class to its inherited class.

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

Sidebar

Related Questions

The problem is this, when I add two or more doubles from a table
How can I do this? open FILE, $somefile; foreach (<FILE>) { if (/some_regex/) {
A little background: I'm in the process of learning Python through O'Reilly's, Learning Python
My company develops and sells a SaaS application that has hundreds of customers. Some
I'm working on some code that uses a lot of after_save callbacks, and I
As a general rule, I prefer using value rather than pointer semantics in C++
I'm working on a large Django app, the vast majority of which requires a
I'm trying to write a quick little java application to read the contents of
In VisualStudio2010 Ultimate RC I cannot figure out how to suppress {CollectionAssert.AreEqual failed. (Element

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.