2014-10-04 72 views
1

我看到了很多使用calloc和malloc的方式:使用cast,without等 所以在这里我有两个选项我可以使用calloc。我很好奇哪一个适合x86 isa。什么是使用calloc for x86 ISA的正确方法

如果我有以下几点:

typedef struct node{ 
    int numOfOccur; 
    int numOfSuperWords; 
    struct node *children; 
}NodePtr; 

NodePtr* temp = &root; 

如何将正确的使用calloc分配存储空间。

  1. 选项1个

    temp -> children[currChar].children = (NodePtr *)calloc(27, sizeof(struct node)); 
    
  2. 选项2

    temp -> children[currChar].children = calloc(27, sizeof(children[currChar].children)); 
    
+2

调用'calloc'的正确方法是独立于ISA。 – 2014-10-04 18:34:12

+0

那么为什么在旧书中我们需要所有这些铸造..但今天人们使用它,而无需铸造和其他东西。有些东西改变了 – YohanRoth 2014-10-04 18:35:35

+0

语言可能已经改变。这与ISA无关。 – 2014-10-04 18:36:01

回答

0

有写这几个是正确的方法。我宁愿以下几点:

temp->children[currChar].children = calloc(27, sizeof(struct node)); 

(即使用类型名称,而不是长期的表达,并no cast。)

除此之外,我并不十分热衷于幻数(27)。