+ 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.
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