When creating a node in c++, when do I need to use the reserved word new to insure that memory is available to allocate.
For instance, this function, does not use new when declaring the node pointer NewNode used to find the end of an existing linked list for purposes of adding a node to the end of the list, however it does use new when actually adding the node to the end of the list. Is this because the node pointer is being used as a non-dynamic memory allocation that points to other memory that has already been allocated dynamically or for some other reason? Would it be incorrect syntax or logic wise to use the new (nothrow) in front of the NewNode declaration?
typedef Node* NodePtr; /*a type definition for NodePtr for ease of use in `the larger program this function is a part of*/`
void AddNode(char NewChar, NodePtr List)
{
NodePtr NewNode = List; //make NewNode point to List
while (NewNode->Link != NULL) //find end of linked list
NewNode = NewNode->Link;
NewNode->Link = new (nothrow) Node; //create a new Node at the end of the list
if (NewNode->Link == NULL) //make sure Node was created
retu;
NewNode = NewNode->Link; //make NewNode point to the Node just created
NewNode->Ch = NewChar; //fill Ch part of NewNode
NewNode->Link = NULL; //make sure the list ends with NULL
}
