Situation: I am implementing a container in the form of a list that supports the Pop () function, which should return to the user the const char * stored in the front of the container. However, I'm not sure as a developer whether I should return the original const char * (by removing the node pointer from the container, but not calling delete for the const char * itself), or should I allocate new memory and return a copy of the element.
From the class and projects, I came across those who support constant copying, so that no previously returned links (from recipients, etc.) And pointers to const char * cannot change the advanced version, but since this approach requires additional highlighting and strcpy, I thought I might ask if it's really just returning the original unrealized pointer to const char without deleting it. Here is a code snippet of the approach With selection and copying (and subsequent removal of the original link):
const char* LinkedList::PopHeadString()
{
node* deletehead = head_;
char* output = NULL;
if (head_ != NULL && GetHeadType() == STRING) {
output = new char[strlen(head_->en.data.str) + 1];
strcpy(output, head_->en.data.str);
head_ = head_->next;
delete deletehead->en.data.str;
delete deletehead;
--nEntries_;
}
return output;
}
Since Pop () is a normal container operation, I thought to ask what the general approach is.
source
share