Join GitHub today
Stack overflow error when large string passed #6860
I uploaded some basic code on my gist to replicate my situation.
However, when I pass a string of 1.3 MB or more to the C function, "Stack overflow" error will be risen.
How can I pass a large data to the C function?
How to replicate
You can easily change the length of input string by editing
I self-solved this issue. I'll write down what I did.
// sample.c void sample(const char*);
There are many ways to pass an argument to this
// NG const sample = Module.cwrap('sample', null, ['string']); sample(veryLargeString); // stack overflow error!
In this case, passed string will be on Emscripten's stack.
// OK const bufferSize = Module.lengthBytesUTF8(veryLargeString); const bufferPtr = Module._malloc(bufferSize + 1); Module.stringToUTF8(veryLargeString, bufferPtr, bufferSize + 1);
The heap can automatically grow when built with
// OK const sample = Module.cwrap('sample', null, ['number']); // not 'string', pointer is a number sample(bufferPtr); Module._free(bufferPtr);