我想要一个带有N个零的数组,比如
#define STRIDE 3
struct MtNode{
/* nodes stores pointers to its 2^stride child nodes.*/
/* you are using C, so struct is needed here */
struct MtNode* nodes; // 2^stride = 2^3 = 8
int nexthop;
};
/* Initialize binary trie node */
/* in C, struct is needed beefure MtNode */
struct MtNode* init_mtnode(){
struct MtNode *ret = malloc(sizeof(struct MtNode));
int size = (int)pow(2,STRIDE);
ret->nodes = malloc( sizeof(struct MtNode) * size );
for (int i=0; i<(int)pow(2,STRIDE); ++i)
/* ret->nodes[i] has type struct MtNode, not struct MtNode* */
/* (ret->nodes[i]) = NULL; */
{
ret->nodes[i].nodes = NULL;
ret->nodes[i].nexthop = -1;
}
ret->nexthop = -1;
return ret;
}
我可以做一个for-loop并将它们全部推出,但有更好的方法吗?
[0,0,0,0...0]
答案 0 :(得分:2)
使用Int8Array
:
var x = new Int8Array(100)
# x now is filled with 0s and is of size 100
Int8Array 类型数组表示二进制补码
8
- 有符号整数的数组。内容初始化为0
。
另请参阅:http://www.ecma-international.org/ecma-262/6.0/#table-49