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
  • 267,707
  • 33
  • 569
  • 680
YohanRoth
  • 3,153
  • 4
  • 31
  • 58

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
  • 486,780
  • 108
  • 951
  • 1,012