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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:33:28+00:00 2026-06-09T04:33:28+00:00

Consider the following code: struct T { int a; union { struct { int

  • 0

Consider the following code:

struct T {
    int a;
    union {
        struct {
            int a;
        } s1;
        struct {
            char b[1024];
        } s2;
    };
};

int main() {
    T x = T();
}

Since an explicit constructor is called, the above code ends-up zero-initializing all the data members in x.

But I would like to have x zero-initialized even if an explicit is not called. To do that one idea would be to initialize the data members in their declaration, which seems to be okay for T::a. But how can I zero-initialize all the memory occupied by the union by using
the same criteria?

struct T {
    int a = 0;
    union {
        struct {
            int a;
        } s1;
        struct {
            char b[1024];
        } s2;
    };
};

int main() {
    T x; // I want x to be zero-initialized
}
  • 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-09T04:33:29+00:00Added an answer on June 9, 2026 at 4:33 am

    For a union without a user-defined default constructor, value initialization is zero initialization.

    However, zero-initialization of a union may not zero all memory, but only the padding and the first member. If the first member isn’t the largest, you could be left with non-zero content.

    Since you know that s2 is largest, you can make a default constructor that zeros it:

    struct T
    {
        int a;
        union {
            int s1a;
            char s2b[1024];
        };
        T() : a(), s2b() {}
    };
    

    And now

    T x;
    

    will be zeroed.

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

Sidebar

Related Questions

Consider the following code struct foo { const int txt_len; const int num_len; char
Consider the following code: #include <vector> struct A { explicit A(int i_) : i(i_)
Java noob question: Consider the following C array and initializer code: struct { int
Consider the following code: template <class x1, class x2 = int*> struct CoreTemplate {
Consider the following code: struct Foo { mutable int m; template<int Foo::* member> void
Consider the following code. #include <stdio.h> #include <vector> #include <iostream> struct XYZ { int
Consider the following code: struct Foo { }; struct Bar { explicit Bar(const Foo&)
Consider the following code template<typename T, int N> struct A { typedef T value_type;
consider the following code:- struct mystruct { int data; struct mystruct *next; }; void
Consider the following C++ code: struct X { int a; int b; }; X

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.