title: Strict aliasing rule
body: <p>I'm reading notes about reinterpret_cast and it's aliasing rules ( <a href="http://en.cppreference.com/w/cpp/language/reinterpret_cast" rel="nofollow">http://en.cppreference.com/w/cpp/language/reinterpret_cast</a> ).</p>
  
  <p>I wrote that code:</p>
  
  <pre><code>struct A
  {
    int t;
  };
  
  char *buf = new char[sizeof(A)];
  
  A *ptr = reinterpret_cast&lt;A*&gt;(buf);
  ptr-&gt;t = 1;
  
  A *ptr2 = reinterpret_cast&lt;A*&gt;(buf);
  cout &lt;&lt; ptr2-&gt;t;
  </code></pre>
  
  <p>I think these rules doesn't apply here:</p>
  
  <ul>
  <li>T2 is the (possibly cv-qualified) dynamic type of the object </li>
  <li>T2 and T1 are both (possibly multi-level, possibly cv-qualified at each level) pointers to the same type T3 (since C++11) </li>
  <li>T2 is an aggregate type or a union type which holds one of the aforementioned types as an element or non-static member (including, recursively, elements of subaggregates and non-static data members of the contained unions): this makes it safe to cast from the first member of a struct and from an element of a union to the struct/union that contains it. </li>
  <li>T2 is the (possibly cv-qualified) signed or unsigned variant of the dynamic type of the object </li>
  <li>T2 is a (possibly cv-qualified) base class of the dynamic type of the object</li>
  <li>T2 is char or unsigned char</li>
  </ul>
  
  <p>In my opinion this code is incorrect. Am I right? Is code correct or not?</p>
  
  <p>On the other hand what about connect function (man 2 connect) and struct sockaddr?</p>
  
  <pre><code>   int connect(int sockfd, const struct sockaddr *addr,
                 socklen_t addrlen);
  </code></pre>
  
  <p>Eg. we have struct sockaddr_in and we have to cast it to struct sockaddr. Above rules also doesn't apply, so is this cast incorrect?</p>