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

  • Home
  • SEARCH
  • 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 531107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:16:00+00:00 2026-05-13T09:16:00+00:00

In Win32 API programming it’s typical to use C struct s with multiple fields.

  • 0

In Win32 API programming it’s typical to use C structs with multiple fields. Usually only a couple of them have meaningful values and all others have to be zeroed out. This can be achieved in either of the two ways:

STRUCT theStruct;
memset( &theStruct, 0, sizeof( STRUCT ) );

or

STRUCT theStruct = {};

The second variant looks cleaner – it’s a one-liner, it doesn’t have any parameters that could be mistyped and lead to an error being planted.

Does it have any drawbacks compared to the first variant? Which variant to use and why?

  • 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-13T09:16:00+00:00Added an answer on May 13, 2026 at 9:16 am

    Those two constructs a very different in their meaning. The first one uses a memset function, which is intended to set a buffer of memory to certain value. The second to initialize an object. Let me explain it with a bit of code:

    Lets assume you have a structure that has members only of POD types ("Plain Old Data" – see What are POD types in C++?)

    struct POD_OnlyStruct
    {
        int a;
        char b;
    };
    
    POD_OnlyStruct t = {};  // OK
    
    POD_OnlyStruct t;
    memset(&t, 0, sizeof t);  // OK as well
    

    In this case writing a POD_OnlyStruct t = {} or POD_OnlyStruct t; memset(&t, 0, sizeof t) doesn’t make much difference, as the only difference we have here is the alignment bytes being set to zero-value in case of memset used. Since you don’t have access to those bytes normally, there’s no difference for you.

    On the other hand, since you’ve tagged your question as C++, let’s try another example, with member types different from POD:

    struct TestStruct
    {
        int a;
        std::string b;
    };
    
    TestStruct t = {};  // OK
    
    {
        TestStruct t1;
        memset(&t1, 0, sizeof t1);  // ruins member 'b' of our struct
    }  // Application crashes here
    

    In this case using an expression like TestStruct t = {} is good, and using a memset on it will lead to crash. Here’s what happens if you use memset – an object of type TestStruct is created, thus creating an object of type std::string, since it’s a member of our structure. Next, memset sets the memory where the object b was located to certain value, say zero. Now, once our TestStruct object goes out of scope, it is going to be destroyed and when the turn comes to it’s member std::string b you’ll see a crash, as all of that object’s internal structures were ruined by the memset.

    So, the reality is, those things are very different, and although you sometimes need to memset a whole structure to zeroes in certain cases, it’s always important to make sure you understand what you’re doing, and not make a mistake as in our second example.

    My vote – use memset on objects only if it is required, and use the default initialization x = {} in all other cases.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can see the definition of a Guid yourself: public… May 13, 2026 at 8:50 pm
  • Editorial Team
    Editorial Team added an answer I don't believe that add_definitions() adds its arguments to CMAKE_CXX_FLAGS.… May 13, 2026 at 8:50 pm
  • Editorial Team
    Editorial Team added an answer Short answer: yes. More specifically it depends on your regex… May 13, 2026 at 8:50 pm

Related Questions

I need to port a screen capture utility to Linux. I'm not familiar with
As a big fan of Charles Petzold's books Code and The Annotated Turing I
I learned windows programming using Visual C++, and the Win32 API. Nowadays, it seems
C++ was the first programming language I really got into, but the majority of
I need to write a few c header wrappers for a new programming language

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.