Allocating JS Array Size #868
-
When initialize an Array in C, you do this: int* a = (int*)malloc(sizeof(int) * 1000);
// An array of 1000 elements
// some work
free(a); or this int a[1000]; // same b In quickjs-ng, we have to define an Array, iterate through the list, set the property uint32, and quickjs manage the index manually so that the Array is never out of bounds and there is no undefined behavior. I want to ask is there anyway to allocate the new array with 1000 elements directly (so that quickjs don't have to reallocate too much). Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
You could allocate an array and then set its length to 1000. That's 2 steps still, but an improvement. |
Beta Was this translation helpful? Give feedback.
-
A new |
Beta Was this translation helpful? Give feedback.
A new
JSValue JS_NewArrayFrom(JSContext *ctx, int count, JSValue *values)
isn't out of the question, IMO. Most JS engines have a similar API.