title: How do you read from a memory buffer c++
body: <p>I am fairly new at C++ and am trying to understand how memory manipulation works. I am used to Java and Python and haven't really been exposed to this. </p>
  
  <p>I am working on a project that has the following structure that doesn't quite make sense to me. </p>
  
  <pre><code>typedef struct
  {
    size_t size;
    char *data;
  } data_buffer;
  </code></pre>
  
  <p>This structure basically acts as a buffer, with a pointer to the data stored within the buffer and the size of the buffer to allow the program to know how large the buffer is when reading from it.</p>
  
  <p>An example of how the program uses the buffer:</p>
  
  <pre><code>data_buffer buffer = {0};
  //Manipulate data here so it contains pertinent information
  CFile oFile;
  oFile.Write(buffer.data, buffer.size);
  </code></pre>
  
  <p>The program mostly uses 3rd party code to read the data found within the buffer, so I am having trouble finding an example of how this is done. My main question is <strong>how do I read the contents of the buffer, given only a pointer to a character and a size?</strong> However, I would also like to understand how this actually works. From what I understand, memory is written to, with a pointer to where it starts and the size of the memory, so I should be able to just iterate through the memory locations, grabbing each character from memory and tagging it onto whatever structure I choose to use, like a CString or a string. Yet, I don't understand how to iterate through memory. Can someone help me understand this better? Thanks.</p>