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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:01:02+00:00 2026-05-11T01:01:02+00:00

I have a structure which I create a custom constructor to initialize the members

  • 0

I have a structure which I create a custom constructor to initialize the members to 0’s. I’ve seen in older compilers that when in release mode, without doing a memset to 0, the values are not initialized.

I now want to use this structure in a union, but get errors because it has a non-trivial constructor.

So, question 1. Does the default compiler implemented constructor guarantee that all members of a structure will be null initialized? The non-trivial constructor just does a memset of all the members to ‘0’ to ensure a clean structure.

Question 2: If a constructor must be specified on the base structure, how can a union be implemented to contain that element and ensure a 0 initialized base element?

  • 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. 2026-05-11T01:01:03+00:00Added an answer on May 11, 2026 at 1:01 am

    Question 1: Default constructors do initialize POD members to 0 according to the C++ standard. See the quoted text below.

    Question 2: If a constructor must be specified in a base class, then that class cannot be part of a union.

    Finally, you can provide a constructor for your union:

    union U  {    A a;    B b;     U() { memset( this, 0, sizeof( U ) ); } }; 

    For Q1:

    From C++03, 12.1 Constructors, pg 190

    The implicitly-defined default constructor performs the set of initializations of the class that would be performed by a user-written default constructor for that class with an empty mem-initializer-list (12.6.2) and an empty function body.

    From C++03, 8.5 Initializers, pg 145

    To default-initialize an object of type T means:

    • if T is a non-POD class type (clause 9), the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
    • if T is an array type, each element is default-initialized;
    • otherwise, the object is zero-initialized.

    To zero-initialize an object of type T means:

    • if T is a scalar type (3.9), the object is set to the value of 0 (zero) converted to T;
    • if T is a non-union class type, each non static data member and each base-class subobject is zero-initialized;
    • if T is a union type, the object’s first named data member is zero-initialized;
    • if T is an array type, each element is zero-initialized;
    • if T is a reference type, no initialization is performed.

    For Q2:

    From C++03, 12.1 Constructors, pg 190

    A constructor is trivial if it is an implicitly-declared default constructor and if:

    • its class has no virtual functions (10.3) and no virtual base classes (10.1), and
    • all the direct base classes of its class have trivial constructors, and
    • for all the nonstatic data members of its class that are of class type (or array thereof), each such class has a trivial constructor

    From C++03, 9.5 Unions, pg 162

    A union can have member functions (including constructors and destructors), but not virtual (10.3) functions. A union shall not have base classes. A union shall not be used as a base class.An object of a class with a non-trivial constructor (12.1), a non-trivial copy constructor (12.8), a non-trivial destructor (12.4), or a non-trivial copy assignment operator (13.5.3, 12.8) cannot be a member of a union, nor can an array of such objects

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

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Better is a bit subjective / depends on your goals… May 11, 2026 at 11:44 pm
  • Editorial Team
    Editorial Team added an answer In SQL Plus scripts you can use a substitution variable… May 11, 2026 at 11:44 pm
  • Editorial Team
    Editorial Team added an answer It depends on how you issue it. If you just… May 11, 2026 at 11:44 pm

Related Questions

I am implementing a tree think of it as a folder structure so I
I have a java class which fires custom java events. The structure of the
So like most new .NET developers you start of passing DataSets everywhere and although
I have an NSDocument which has the following structure: @interface MyDocument : NSDocument {

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.