javiJ
Deno3y ago
3 replies
javi

Update value by reference without duplicating the data

Im exploring the world of FFI, porting some C applications I have to TypeScript, with the help of Deno. Currently, I cannot think of an efficient way to update the value that a pointer is pointing to. For instance, in C I could do something like:
char **array = calloc(2, sizeof(char*));
if (array == NULL) return 1;
*(array) = "hello";
*(array + 1) = "world";

Nonetheless, with Deno I have to first either allocate memory with an allocator or create a TypedArray myself, then create a new unsafe pointer of that. To update it, I’d have to create an unsafe pointer view of that pointer, get the array buffer, create a new Uint8Array from that array buffer, use the <Uint8Array>.set function to update the chunk of memory, and then create a new unsafe pointer of that, duplicating the memory and adding extra complexity.
Is there another way to do it? Thanks!
Was this page helpful?