When should I use the new keyword in C++?
November 6, 2015
I started today solving some linked lists questions. After finishing some LeetCode problems, I went to HackerRank. I immediately stopped at Insert a node at the tail of a linked list
and spent one hour debugging why this does not work:
Node* Insert(Node *head,int data)
{
Node node;
node.data = data;
node.next = nullptr;
if (head == nullptr) {
return &node;
}
Node* p = head;
while (p->next != nullptr)
p = p->next;
p->next = &node;
return head;
}
I guess I didn’t RTFM:
Method 2 (not using new): Memory is no longer allocated when it goes out of scope. (i.e. you shouldn’t return a pointer to an object on the stack)