title: Undefined Behavior to take Address of End of Array/Vector?
body: <p>I'd like to write a template function which can pass data to a C-style API (in my use-case specifically, OpenGL), using a <code>std::vector</code>. The code I came up with looks like this:</p>
  
  <pre><code>template&lt;typename T&gt;
  void pass_data(GLuint buffer, std::vector&lt;T&gt; const&amp; data) {
      glBindBuffer(GL_ARRAY_BUFFER, buffer);
      glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(T), data.data(), GL_STATIC_DRAW);
  }
  </code></pre>
  
  <p>This seemed like a simple solution, until it occurred to me that <code>T</code> might have alignment restrictions that would cause <code>&amp;t[1] - &amp;t[0] != sizeof(T)</code> to be true, which would mean that this function would not accurately pass the whole array. So I rewrote the function to do this instead:</p>
  
  <pre><code>template&lt;typename T&gt;
  void pass_data(GLuint buffer, std::vector&lt;T&gt; const&amp; data) {
      glBindBuffer(GL_ARRAY_BUFFER, buffer);
      glBufferData(GL_ARRAY_BUFFER, static_cast&lt;size_t&gt;(&amp;data[data.size()] - &amp;data[0]), data.data(), GL_STATIC_DRAW);
  }
  </code></pre>
  
  <p>However, it's not clear to me that this kind of one-past-the-end access is safe (I'm pretty sure it's undefined behavior). What's the best way to ensure that my function accurately assesses the byte-size of my vector, and passes the correct parameters to the underlying C-API call?</p>
  
  <p>Note: I do not want discussion about how I'm [mis-]using OpenGL. I'm just focused on the desire to accurately model a pointer + size of a vector.</p>