git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [RFC PATCH 1/2] Replace memset(0) with static initialization where possible

From
Nicolas Pitre <nico@cam.org>
Date
Oct 9, 2008, 19:47 UTC
Message-ID
<alpine.LFD.2.00.0810091534430.26244@xanadu.home>
In-Reply-To
<NveF6_7LIvvEmRZEvLeTO5lw7EzzmOQkz1WGEMYGSFKDWqSwAeLwBw@cipher.nrlssc.navy.mil>
On Thu, 9 Oct 2008, Brandon Casey wrote:
> "Possible" benefits:
> 
>   1) more concise, so it improves readability in most cases
>   2) gives compiler more flexibility when optimizing

Beware beware. At some point this wasn't a gain at all with some gcc versions as it was stupid enough to construct a temporary object on the stack corresponding to "{ 0, }" and afterward do the assignment by *copying* this object to a different stack slot corresponding to the actual variable instead of constructing the initial value in place.

Also note that, on the other hand, gcc is smart enough to optimize a memset() using a constant size and value already by doing appropriate code replacement inline. I've also seen cases where gcc did the opposite and replaced an explicit assignment like your patch does with an actual call to memset() when optimizing for size.

So when claiming flexibility for the compiler to better optimize things, please make sure this is really what is happening through assembly dump inspection.

Nicolas
Previous: Brandon CaseyNext: Brandon Casey
Message 4 of 6 in “Replace memset(0) with static initialization where possible”
  1. 1/2 Replace memset(0) with static initialization where possibleBrandon Casey, Oct 9, 2008
  2. Shawn O. PearceOct 9, 2008
  3. Brandon CaseyOct 9, 2008
  4. Nicolas PitreOct 9, 2008
  5. Brandon CaseyOct 9, 2008
  6. Nicolas PitreOct 9, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.