I have this portion of c++ code, below there are two lines using malloc
and I want to change it to new
notation.
struct CodingTree* createCodingTree(unsigned int capacity){
struct CodingTree* cTree = (struct CodingTree*)malloc(sizeof(struct cTree));
cTree->size = 0;
cTree->capacity = capacity;
cTree->array = (struct BinaryNode**)malloc(cTree->
capacity * sizeof(struct BinaryNode*));
return cTree;
}
For the first one, I've changed :
struct CodingTree* cTree = (struct CodingTree*)malloc(sizeof(struct cTree));
to
CodingTree* cTree = new CodingTree();
And seems like it's working, but for the second one, I have no idea what would be the equivalent new
.
For reference, adding the CodingTree
and BinaryNode
structures.
struct BinaryNode{
char symbol;
unsigned int count;
struct BinaryNode *left, *right;
BinaryNode(char symbol, unsigned int count){
this->symbol = symbol;
this->count = count;
left =0;
right =0;
}
};
struct CodingTree {
unsigned int size;
unsigned int capacity;
BinaryNode** array;
};