title: Can't seem to allocate vector properly
body: <p>I am attempting to write a program that takes hex string input and converts it to binary.  For the life of me, I can't seem to write this properly.  The main culprit so far is that I can't initialize the output vector in the hex_decode function properly.  See the gdb output.  I don't normally use c++ stl and string streams for this kind of thing but I thought I'd try something new.  This is biting me in the butt and looks too simple, but its clearly wrong.  What am I missing?</p>
  <p>code</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;sstream&gt;
  #include &lt;iostream&gt;
  #include &lt;iomanip&gt;
  
  #include &lt;vector&gt;
  #include &lt;cstdint&gt;
  
  std::vector&lt;uint8_t&gt; hex_decode(std::string input){
    std::vector&lt;uint8_t&gt; output;
  
    for (int i = 0; i &lt; input.length(); i+=2) {
      output.push_back(0);
    }
    
    for (int i = 0; i &lt; input.length(); i+=2) {
      uint8_t n = 0;
      std::stringstream ss;
      ss &lt;&lt; input[i];
      ss &gt;&gt; std::hex &gt;&gt; n;
      n = n &lt;&lt;4;
      output[i/2] += n;
      ss &gt;&gt; std::hex &gt;&gt; n;
      output[i/2] += n;
    }
  }
  
  
  int main(int argc, char** argv) {
    std::string input;
    std::vector&lt;uint8_t&gt; decode;
    std::cin &gt;&gt; input;
  
    decode = hex_decode(input);
    for (auto&amp;&amp; b : decode) {
      std::cout &lt;&lt; b;
    }
  }
  </code></pre>
  <p>gdb output</p>
  <pre><code>Program received signal SIGSEGV, Segmentation fault.
  0x00000000004015e1 in main (argc=1, argv=0x7fffffffdc48) at mapConverterMain.cc:37
  (gdb) info locals
  b = &lt;error reading variable&gt;
  __for_range = @0x7fffffffdac0: {&lt;std::_Vector_base&lt;unsigned char, std::allocator&lt;unsigned char&gt; &gt;&gt; = {
      _M_impl = {&lt;std::allocator&lt;unsigned char&gt;&gt; = {&lt;__gnu_cxx::new_allocator&lt;unsigned char&gt;&gt; = {&lt;No data fields&gt;}, &lt;No data fields&gt;}, _M_start = 0x1 &lt;error: Cannot access memory at address 0x1&gt;, 
        _M_finish = 0x7fffffffdb10 &quot;\300\003&quot;, 
        _M_end_of_storage = 0x603d88 &quot;@\022@&quot;}}, &lt;No data fields&gt;}
  __for_begin = {_M_current = 0x1 &lt;error: Cannot access memory at address 0x1&gt;}
  __for_end = {_M_current = 0x7fffffffdb10 &quot;\300\003&quot;}
  input = {static npos = 18446744073709551615, 
    _M_dataplus = {&lt;std::allocator&lt;char&gt;&gt; = {&lt;__gnu_cxx::new_allocator&lt;char&gt;&gt; = {&lt;No data fields&gt;}, &lt;No data fields&gt;}, 
      _M_p = 0x618030 &quot;0012811738003000213106014c4d941e229016220f7e02dc0e2002240000000388a40654a626c890013ece229031bb989b0df2050512011200000000622901a28989b24d062290329f989b108249058000448190002241d00010400cc80000000311480d&quot;...}, _M_string_length = 566, {
      _M_local_buf = &quot;\300\003\000\000\000\000\000\000\063\070\060\060\063\060\060&quot;, 
      _M_allocated_capacity = 960}}
  decode = {&lt;std::_Vector_base&lt;unsigned char, std::allocator&lt;unsigned char&gt; &gt;&gt; = {
      _M_impl = {&lt;std::allocator&lt;unsigned char&gt;&gt; = {&lt;__gnu_cxx::new_allocator&lt;unsigned char&gt;&gt; = {&lt;No data fields&gt;}, &lt;No data fields&gt;}, _M_start = 0x1 &lt;error: Cannot access memory at address 0x1&gt;, 
        _M_finish = 0x7fffffffdb10 &quot;\300\003&quot;, 
        _M_end_of_storage = 0x603d88 &quot;@\022@&quot;}}, &lt;No data fields&gt;}
  (gdb) bt
  #0  0x00000000004015e1 in main (argc=1, argv=0x7fffffffdc48) at mapConverterMain.cc:37
  (gdb) print decode.size()
  $3 = 140737488345871
  (gdb) print input.length()
  $4 = 566
  (gdb) 
  </code></pre>
  <p>input data file</p>
  <pre><code>0012811738003000213106014c4d941e229016220f7e02dc0e2002240000000388a40654a626c890013ece229031bb989b0df2050512011200000000622901a28989b24d062290329f989b108249058000448190002241d00010400cc80000000311480dd04c4d990d7114810e74c4da202028289011100000000311480cbd4c4d99e67114810d9cc4da5fd02828920f0000c90520006480a00030802a900000000622901599989b305e6229003e4989b3fe4480cc80000000188a4050f6626cb5c388a40065a626cf6d013ec490180008481a0004241c800209022100000000711480aff4c4d9130027d8c452020ad3136253892070000490540002480900010803b900000000e2290144a989b239404fb188a4041de626c66000
  </code></pre>