DenoDDeno
Powered by
javiJ
Deno•4y 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";
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
new Uint8Array
from that array buffer, use the
<Uint8Array>.set
<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!
Deno banner
DenoJoin
Chat about Deno, a modern runtime for JavaScript and TypeScript.
20,934Members
Resources
Recent Announcements

Similar Threads

Was this page helpful?

Similar Threads

Deno bundle duplicating variables
SorikairoSSorikairo / help
4y ago
`deno vendor` + `/// <reference types=...>` ?
Carlos ScheideggerCCarlos Scheidegger / help
2y ago