How to access WebAssembly linear memory from C/C++ : <p>I'm writing a small C program intended to be compiled to wasm w/ <code>emcc</code> and run in a web browser. Because wasm exported functions can only accept simple number values as parameter inputs and return values, I need to share memory between the JavaScript API and the compiled WebAssembly code in order to access more complex data types like strings or <code>char</code> arrays. The problem is that I can't for the life of me figure out how to access <a href="https://developer.mozilla.org/en-US/docs/WebAssembly/Using_the_JavaScript_API" rel="noreferrer">WebAssembly linear memory</a> from inside of my C program.</p>

<p>My ultimate goal is to be able to read strings initialized in JavaScript inside of my C program, and then also read strings that are modified/initialized in my C program back in the web browser's JavaScript code.</p>

<p>Here is a basic example of what I'm trying to do:</p>

<p>main.js
</p>

<pre><code>const importObject = {
  'env': {
    'memoryBase': 0,
    'tableBase': 0,
    'memory': new WebAssembly.Memory({initial: 256}),
    'table': new WebAssembly.Table({initial: 0, element: 'anyfunc'})
  }
}

// using the fetchAndInstantiate util function from
// https://github.com/mdn/webassembly-examples/blob/master/wasm-utils.js
fetchAndInstantiate('example.wasm', importObject).then(instance =&gt; {

      // call the compiled webassembly main function
      instance.exports._main()
      console.log(importObject.env.memory)
})
</code></pre>

<p>example.c</p>

<pre class="lang-js prettyprint-override"><code>int main() {
    // somehow access importObject.env.memory 
    // so that I can write a string to it
    return 0;
}
</code></pre>

<p><a href="https://stackoverflow.com/questions/41353389/how-can-i-return-a-javascript-string-from-a-webassembly-function">This question</a> gets me part of the way there, however, I still don't understand how to read/write from the WebAssembly memory buffer in my C code.</p>
