title: C++ possible memory leak reading file
body: <p>I'm relatively new to C++ and I'm trying to read a proprietary file format. I know the header format, and I've created a struct <code>RTIHeader</code> with the necessary fields.</p>
  
  <p>My test code reads bytes from the file and copies them into the same memory space as the header struct, effectively reconstituting it. My problem is that every time I run the test code (just calling the constructor) I get a different value! My theory is that I do not fully understand <code>memcpy</code>.</p>
  
  <pre><code>struct RTIHeader decode(char* memblock){
      struct RTIHeader header;
      memcpy(&amp;header,&amp;memblock,sizeof(RTIHeader));
      return header;
  }
  
  RTIFile::RTIFile(const char* filename){
      // open the file in binary input mode with the pointer at the end
      std::ifstream file(filename,
                         std::ios::in |
                         std::ios::binary |
                         std::ios::ate);
  
      std::ifstream::pos_type size;
      char * memblock;
      RTIHeader header;
  
      // if the file didn't open, throw an error
      if(!file.is_open()){
          //TODO learn about C++ error handling
          return;
      }
      // use pointer position to determine file size
      size = file.tellg();
      // read file
      memblock = new char [sizeof(RTIHeader)];
      file.seekg(0,std::ios::beg);
      file.read(memblock,sizeof(RTIHeader));
  
      header = decode(memblock);
  
      std::cout &lt;&lt; (unsigned int)header.signature[0] &lt;&lt; "\n";
  
      // still need to read the rest of the file
      file.close();
  }
  </code></pre>