<p style="white-space: pre-wrap; word-wrap: break-word;">can i has +2?</p><p><a href="https://gerrit.osmocom.org/12039">View Change</a></p><p>1 comment:</p><ul style="list-style: none; padding: 0;"><li style="margin: 0; padding: 0;"><p><a href="https://gerrit.osmocom.org/#/c/12039/3/src/libmsc/gsm_04_08.c">File src/libmsc/gsm_04_08.c:</a></p><ul style="list-style: none; padding: 0;"><li style="margin: 0; padding: 0 0 0 16px;"><p style="margin-bottom: 4px;"><a href="https://gerrit.osmocom.org/#/c/12039/3/src/libmsc/gsm_04_08.c@1666">Patch Set #3, Line 1666:</a> <code style="font-family:monospace,monospace">       conn->geran_encr = (struct geran_encr){};</code></p><p><blockquote style="border-left: 1px solid #aaa; margin: 10px 0; padding: 0 10px;">Or you can only do that at same time you declare the struct variable?</blockquote></p><p style="white-space: pre-wrap; word-wrap: break-word;">sorry guys, it's going to stay this way. I'm already using this scheme throughout all code I write, and it is the single best method. Here is my reasoning:</p><ul><li>memset() is the byte hacking way and I don't like it. In practice, clearing out is always 0, and even '\0' is 0, and NULL is 0, and false is 0, but that's just incidental. Semantically, each type has its own constructor default value. Using memset() totally brushes over that concept; which works out fine in practice, but I will use compiler provided default values wherever I can and avoid memset.</li></ul><ul><li>just 'var = {0}' doesn't work, you need to supply the type name to the compiler (unless it is a new variable declaration init, which this is not; see examples below).</li></ul><ul><li>the struct variable is part of ran_conn, I need to here clean out one part of that. It does not make sense to declare a new local variable for that. (see examples below)</li></ul><ul><li>to write "{0}" means that I indicate the first item's value to be zero. Doing "{}" is the more general way that achieves to zero all items, no matter what type they are. Let me illustrate...</li></ul><p style="white-space: pre-wrap; word-wrap: break-word;">With this, writing {0} is fine, since the first element is an int:</p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">  struct foo {<br>    int a;<br>    int b[3];<br>  }</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">  struct foo my_foo = {0}; // initialize a = 0, the rest as zero</pre><p style="white-space: pre-wrap; word-wrap: break-word;">But what about this:</p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">  struct bar {<br>     struct {<br>        int a;<br>     } sub;<br>     char c[3];<br>  }</pre><p style="white-space: pre-wrap; word-wrap: break-word;">Doesn't work:</p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">  struct bar my_bar = {0};</pre><p style="white-space: pre-wrap; word-wrap: break-word;">Would have to be one of:</p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">  struct bar my_bar = { {0} };<br>  struct bar my_bar = { .sub = {0} };<br>  struct bar my_bar = { .c = {0, 0, 0} };<br>  struct bar my_bar = { .c = {} };</pre><p style="white-space: pre-wrap; word-wrap: break-word;">But I don't WANT any specific value! Just zero all!<br>The Generally Always Working Best (TM) way to approach this is:</p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">  struct bar my_bar = {};</pre><p style="white-space: pre-wrap; word-wrap: break-word;">All items are zeroed without needing to know anything about its structure.</p><p style="white-space: pre-wrap; word-wrap: break-word;">Why this (struct bar){} notation here? because I have</p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">  struct moo {<br>     struct bar item;<br>  };</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">  void my_func(struct moo *m)<br>  {<br>      m->item = (struct bar){};<br>  }</pre><p style="white-space: pre-wrap; word-wrap: break-word;">This is the way it should be done, and you will not convince me otherwise, sorry</p></li></ul></li></ul><p>To view, visit <a href="https://gerrit.osmocom.org/12039">change 12039</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.osmocom.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.osmocom.org/12039"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: osmo-msc </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-MessageType: comment </div>
<div style="display:none"> Gerrit-Change-Id: Idc7ca9da1aa13ae16f5db2cb1024676cbc770820 </div>
<div style="display:none"> Gerrit-Change-Number: 12039 </div>
<div style="display:none"> Gerrit-PatchSet: 3 </div>
<div style="display:none"> Gerrit-Owner: Neels Hofmeyr <nhofmeyr@sysmocom.de> </div>
<div style="display:none"> Gerrit-Reviewer: Jenkins Builder (1000002) </div>
<div style="display:none"> Gerrit-Reviewer: Neels Hofmeyr <nhofmeyr@sysmocom.de> </div>
<div style="display:none"> Gerrit-Reviewer: Pau Espin Pedrol <pespin@sysmocom.de> </div>
<div style="display:none"> Gerrit-Reviewer: Vadim Yanitskiy <axilirator@gmail.com> </div>
<div style="display:none"> Gerrit-Comment-Date: Fri, 30 Nov 2018 18:23:43 +0000 </div>
<div style="display:none"> Gerrit-HasComments: Yes </div>
<div style="display:none"> Gerrit-HasLabels: No </div>