On Tue, Nov 3, 2015 at 6:41 PM, larour <[email protected]> wrote:

> Brion,
>
> thanks for answering so promptly. I have been working with your code
> snipped, and something
> is not working as anticipated. The length does return the right value ( if
> I change from u32 (which is undefined apparently) to i8).
>

Looks like should be 'i32'. 'i8' will only happen to work if the length of
the array is < 256.


> However, the arrayPointer does not act as an array, i.e.
> arrayPointer.length is undefined. I've tried
> to display arrayPointer[0], but it's garbage. Do I need to create an array
> dynamically (like
> new Float32Array, and make it point to the pArrayPointer?)
>

Yes, a C pointer is just a number -- it has no special array powers in
JavaScript. :) You'll need to create a Float32Array pointing at the correct
portion of heap memory, which is the index arrayPointer in this example
(not pArrayPointer, which is the pointer *to* the pointer!)

Something like:

  var floatArray = Module.HEAPF32.slice(arrayPointer / 4, arrayPointer / 4
+ arrayLength)

should do it. Note that you're still responsible for free()ing the original
array's memory from the emscripten heap in this case, but it should be safe
to do so immediately -- slice() produces a copy -- so also add:

  Module._free(arrayPointer);

-- brion



>
> Thanks for any pointers,
>
> Eric L.
>
>
>
> On Tuesday, November 3, 2015 at 4:59:17 PM UTC-8, Brion Vibber wrote:
>>
>> 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.
>

-- 
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.

Reply via email to