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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:34:38+00:00 2026-06-04T14:34:38+00:00

I’m reading about the initialized values by default of an array/struct and have this

  • 0

I’m reading about the initialized values by default of an array/struct and have this question:

is memset(&mystruct, 0, sizeof mystruct) same as mystruct = { 0 }; ?

if it’s not, what’s difference?

  • 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-04T14:34:41+00:00Added an answer on June 4, 2026 at 2:34 pm

    is memset(&mystruct, 0, sizeof mystruct) same as mystruct = { 0 }; ?

    No.

    memset(&mystruct, 0, sizeof mystruct) ;
    

    … will tell the compiler to call a function that we expect will set during execution the data in mystruct to zero.

    mystruct = { 0 };
    

    … will set tell the compiler set by itself the data to zero, which means it will:

    • if possible, set the data in mystruct to zero at compilation (e.g. for static variables, as tristopia and Oli Charlesworth remarked in the comments)
    • or if not (e.g. auto variables), to generate the assembly code that will set the data to zero when the variable is initialized (which is better than calling a function to do that).

    Note that perhaps the compiler could optimize the memset into a compile-time instruction (like replacing the first version with the second version), but I wouldn’t rely on that as memset is a function from the runtime library, not some language intrinsic (I’m not a compiler writer/language lawyer, though).

    Coming from C++, my own viewpoint is that the more you can do at compilation and the more the compiler knows at compile time, before the execution even starts, the better: It enables the compiler to possibly optimize the code and/or generate warning/errors.

    In the current case, using the mystruct = { 0 }; notation to initialize a struct is always safer than using the memset because it is very very easy write the wrong thing in C with a memset without the compiler complaining.

    The following examples show that it is easy for the code to do something different than it appears to do:

    // only the 1st byte will be set to 0
    memset(&mystruct, 0, sizeof(char)) ;          
    
    // will probably overrun the data, possibly corrupting
    // the data around it, and you hope, crashing the process.
    memset(&mystruct, 0, sizeof(myLARGEstruct)) ; 
    
    // will NOT set the data to 257. Instead it will truncate the
    // integer and set each byte to 1
    memset(&mystruct, 257, sizeof(mystruct)) ;    
    
    // will set each byte to the value of sizeof(mystruct) modulo 256
    memset(&mystruct, sizeof(mystruct), 0) ;      
    
    // will work. Always.
    mystruct = { 0 } ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am reading a book about Javascript and jQuery and using one of the
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text

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.