title: C++ Random Vector/Stream Errors
body: <p>I have created a structure that contains a vector of unsigned integers for bitstream handling. Essentially, values are written to this vector in sets that are no more than 64 bits long.</p>
  <p>The strange behavior comes into play with different images used. Running the entire code on various images yields strange results. Some images (no correlation to size) will properly encode and write to the ofstream while others will cause a vector emplace_back error at the line &quot;ints.emplace_back(field())&quot;.</p>
  <p>As I am sure most reading this will suggest, I have added a .reserve line to increase the capacity. This does fix this problem, but then (for the same images) a new problem occurs when writing the vector to the file. Now an error occurs in fstream at line
  <code>_Count -= _CSTD fwrite(_Ptr, sizeof(_Elem), static_cast&lt;size_t&gt;(_Count), _Myfile);</code>
  But again, this error does not happen for most of the other images that are both the same dimensions or larger or smaller. Any reason for this strange behavior? Or perhaps suggestions on a way to fix the fwrite error? I tried shrink_to_fit() but that causes the same malloc error when shrinking to fit.</p>
  <p>Here is the relevant code:
  (structures)</p>
  <pre><code>  struct field {
      private:
          uint32_t bits;
  
      public:
          field() : bits(0) {}
          operator uint32_t() { return bits; }
          inline uint32_t pack() { return bits; }
          field&amp; set_bits(uint32_t val, uint8_t pos) {
              bits |= (val &lt;&lt; pos);
              return *this;
          }
          uint32_t get_bits(uint8_t a, uint8_t b) {
              return (bits &gt;&gt; a) &amp; ~(0xFFFFFFFF &lt;&lt; (b - a));
          }
      };
      struct bstream {
      private:
          std::vector&lt;field&gt; ints;
          size_t position;
          size_t length;
      public:
          bstream() : position(0), ints(0), length(0) {}
          bstream(const bstream&amp; p1) { position = p1.position; ints = p1.ints; length = p1.length; }
  
          field* data() { return ints.data(); }
          int get_size() { return (ceil(position / 32.0)); } // returns how many bytes there are
          int max_size() { return ints.max_size(); }
          int size() { return ints.size(); }
          int capacity() { return ints.capacity(); }
          bstream&amp; resize(int size) { ints.resize(floor(size / 4)); length = floor(size / 4); return *this; }
          bstream&amp; reserve(int size) { ints.reserve(size); return *this; }
          bstream&amp; shrink_to_fit() { ints.shrink_to_fit(); return *this; }
  
          bstream&amp; push_chunk(uint32_t C, int Q) {
              const int a = (position % 32);
              const int b = a + Q;
              if (a == 0)
                  ints.emplace_back(field());
              if (b &gt; 32) {
                  ints.back().set_bits(C, a);
                  position += (32 - a);
                  this-&gt;push_chunk(C &gt;&gt; (32 - a), b - 32);
              }
              else {
                  ints.back().set_bits(C, a);
                  position += Q;
              }
              return *this;
          }
          uint32_t pop_chunk(int Q) {}
  
          bstream&amp; write_int(int C, int Q, const int qmin, bool is_signed) {
              this-&gt;push_chunk(abs(C) &gt;&gt; qmin, Q - qmin);
              if (is_signed &amp;&amp; (abs(C) &gt;&gt; qmin) != 0)
                  this-&gt;push_chunk(int(0) &lt; C, 1);
              return *this;
          }
          int read_int(int Q, const int qmin, bool is_signed) {}
  
          bstream&amp; write_descent(int A, int B, const int qmin) {}
          int read_descent(int A, const int qmin) {}
  
          bstream&amp; encode_group(int* Q, int* C, int index, int qmin, int sizeQ, int sizeC) {}
          bstream&amp; encode_coeffs(int Qg, int Q, int* C, int index, int qmin) {}
  
          void decode_group(int* Q, int* C, int index, int qmin, int sizeQ, int sizeC) {}
          void decode_coeffs(int Qg, int&amp; Q, int* C, int index, int qmin) {}
  
          void print() {}
      };
  </code></pre>
  <p>(Line used to write to ofstream)
  <code>outFile-&gt;write(reinterpret_cast&lt;char*&gt;(STREAM.data()), 4.0 * STREAM.get_size());</code></p>