When passing pointers into a C function, remember they have to point to a place in the emscripten heap. This means you'll need to allocate some space on the emscripten heap for your pointer and your integer, then pass the pointers to *that* pointer and that integer into the function... then extract the final values back out of the heap.
Something like: var pArrayPointer = Module._malloc(8); // room for 2 pointers var pLength = pArrayPointer + 4; AllocateArray(pArrayPointer, pLength); var arrayPointer = Module.getValue(pArrayPointer, 'u32'); var length = Module.getValue(pLength, 'u32'); Module._free(pArrayPointer); // free your temp vars I would recommend wrapping all that into a function that exposes a more JS-friendly interface (such as returning a Float32Array as a single return value). -- brion On Tue, Nov 3, 2015 at 4:27 PM, larour <[email protected]> wrote: > Dear All, > > I have an issue that I can't seem to see addressed fully, apologies in > advance if this has already been posted. > Here is the issue: > > given a C function that I compile with emcc: > > int allocatearray(float** array,int* parray_size){ > > array_size=rand(10); > float* array= (float*) malloc(array_size); > *parray_size=array_size; > *parray=array; > } > > > this function allocates an array for which I do not initially know the > size, hence cannot be done on the > js side, only the c side knows how to handle the allocation. > > I would like to be able to create a js array that is allocated insize the > "c" function allocatearray: > > i.e: I would like to write the following code: > > var allocated_array; > var allocated_array_size; > AllocateArray = > Module.cwrap('allocate_array','number',['number','number']); > > but I don't seem to understand how using the module heap I could retrieve > the allocated_array > and its size, how would the call even look like? > AllocateArray(&allocate_array, & allocated_array_size); ? //that's not > javascript! but that's what I would like to write > > I could try and build a pointer in the module heap, and pass that on to > the AllocateArray module, but I need to pass > a pointer to a pointer if I want to be able to allocate it. How do you > create a NULL pointer in javascript, and get > it allocated within the c function? > > I have seen a lot of documentation on how to pass a pointer from js to C, > but here, it's different, I want to pass > a NULL pointer from js to C, have C allocate it, and return it to js. > > I would appreciate any help on the issue, thanks a lot in advance! > > Eric > > > -- > You received this message because you are subscribed to the Google Groups > "emscripten-discuss" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "emscripten-discuss" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
