Repository navigation
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 bIn 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! |
Answered by
bnoordhuis
Jan 31, 2025
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. |
0 replies
|
A new |
6 replies
Answer selected by
harumazzz
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.