1

I was shown many ways of using calloc and malloc: with casting, without, etc So here I have two option how I can use calloc. I am curious which one is right for x86 isa.

If I have the following:

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

NodePtr* temp = &root;

How would be correct to allocate memory using calloc.

  1. Option 1

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

    temp -> children[currChar].children = calloc(27, sizeof(children[currChar].children));
    
Oliver Charlesworth
  • 260,367
  • 30
  • 546
  • 667
YohanRoth
  • 2,933
  • 3
  • 21
  • 46

1 Answers1

0

There are several correct ways to write this. I would prefer the following:

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

(i.e. use the type name instead of the long expression, and no cast.)

Apart from that, I am not terribly keen on the magic number (27).

Community
  • 1
  • 1
NPE
  • 464,258
  • 100
  • 912
  • 987