title: How to return []byte from internal void * in C function by CGO?
body: <p>I am wrapping an C function with the following definition:</p>
  
  <pre><code>int parser_shift(parser* parser, void* buffer, int length);
  </code></pre>
  
  <p>It removes up to length bytes from internal buffer of unparsed bytes, storing them in the given buffer.</p>
  
  <p>Now I wish to wrap it into a Go function with the following definition:</p>
  
  <pre><code>func (p *Parser) Shift() []byte {
      var buffer []byte
      // TODO:
  
      return buffer
  }
  </code></pre>
  
  <p>What is the right way of writing to finish the TODO in above with CGO? </p>
  
  <p>I tried the following way, however it crashed with error: Error in "/path/to/my/program': free(): invalid next size (fast): 0x00007f8fe0000aa0:</p>
  
  <pre><code>var buffer []byte
  bufStr := C.CString(string(buffer))
  defer C.free(unsafe.Pointer(bufStr))
  C.parser_shift(p.Cparser, unsafe.Pointer(bufStr), C.int(8192))
  buffer = []byte(C.GoString(bufStr))
  return buffer
  </code></pre>