+ 1

What is the difference between these two?

class node{ public: int data; node* next; }; 1. struct node* new_node = (struct *) malloc(sizeof(struct node)); 2. node* new_node = new node(); I have been seen tutorials using the first one to allocate memory, but I'm using the second one without any problem.

15th Oct 2021, 2:27 PM
Kashyap Kumar
Kashyap Kumar - avatar
1 Odpowiedź
+ 3
As I understand it, (1) is dynamic memory allocation in C way, and (2) is the C++ way. The (2) calls for a constructor, one that matches given argument(s), or the default constructor where no argument was given, e.g. `new node()` Hth, cmiiw
15th Oct 2021, 2:52 PM
Ipang