<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Initializing structs with memset</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/5786/initializing-structs-with-memset</link><description>Why is it a standard in nordic-supplied code to initialize structs with memset like so? An example would be: 
 ble_gatts_char_md_t char_md;
memset(&amp;amp;char_md, 0, sizeof(char_md));
 
 Instead of a cleaner way: 
 ble_gatts_char_md_t char_md = {0};
</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 30 Jun 2015 14:08:28 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/5786/initializing-structs-with-memset" /><item><title>RE: Initializing structs with memset</title><link>https://devzone.nordicsemi.com/thread/20218?ContentTypeID=1</link><pubDate>Tue, 30 Jun 2015 14:08:28 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:df2c7216-c9ee-479d-88a6-02e8d28254f2</guid><dc:creator>Sensors</dc:creator><description>&lt;p&gt;Just did a little experiment with this. There was no change in code size between the two approaches using Keil uVision (armcc).&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Initializing structs with memset</title><link>https://devzone.nordicsemi.com/thread/20217?ContentTypeID=1</link><pubDate>Mon, 02 Mar 2015 02:50:54 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:fa48046e-f911-4d30-a1fe-071af4227f12</guid><dc:creator>Charles Manning</dc:creator><description>&lt;p&gt;I don&amp;#39;t think that is quite true.&lt;/p&gt;
&lt;p&gt;The {0} version will create a binary version of the structure and the = will cause a copy.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;xx = {0};
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;is eqivalent to&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;y = {0};memcpy(&amp;amp;x,&amp;amp;y, sizeof(y));
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;;&lt;/p&gt;
&lt;p&gt;But the result is the same: The code is larger (and slower) since making a large structure  needs to make the huge structure then also do a copy. A copy requires reading then writing each word in the structure. That is slower than just setting it to zero.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Initializing structs with memset</title><link>https://devzone.nordicsemi.com/thread/20216?ContentTypeID=1</link><pubDate>Sat, 28 Feb 2015 03:09:58 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:69af8e2b-c98a-4657-9b86-62bf0816d3b6</guid><dc:creator>RK</dc:creator><description>&lt;p&gt;Well there&amp;#39;s style, and space.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;ble_gatts_char_md_t char_md = {};
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;forces the compiler to emit code to explicitly set each element of the structure to zero when it&amp;#39;s created, for large structures that&amp;#39;s a fair amount of instructions. memset, which is usually linked in anyway, is just a function call, less code space.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>