【发布时间】:2022-11-20 08:58:35
【问题描述】:
我正在将 C 源代码转换为 javascript 但我遇到了一些问题,我不知道如何在 javascript 中编写此 c 方法 `
void create_board(int r, int c, int w) {
board = malloc((r * c) * sizeof(int));
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
board[i * c + j] = -1;
}
}
num_rows = r;
num_cols = c;
num_win = w;
}
` 有人可以帮我用 javascript 编写这段代码吗?
我尝试了很多解决方案,但对我没有任何用处。
create_board(r,c,w) {
// this.#board = [];
this.#board = this.#num_rows * this.#num_cols * ;
for (let i = 0; i < r; i++) {
for (let j = 0; j < c; j++) {
this.#board[i * c + j] = -1;
}
}
this.num_rows = r;
this.#num_cols = c;
this.num_win = w;
}
【问题讨论】:
-
JavaScript 没有“malloc”,它不需要直接分配内存。如果您想要特定大小的数组,请参阅the docs。
-
您必须展示
malloc()的用途,才能知道如何最好地实现 Javascript 的最终目标。 nodejs 有Buffer.alloc(),但一般来说,您只需将数组或字符串用于动态数据,语言会为您管理空间,而不需要像malloc()这样的东西。
标签: javascript node.js c malloc