In a linked list we declare node like this:
struct node
{
int data; // Data part
struct node* next; // pointer to next node
node(int key)
{
data = key;
next = NULL;
}
};
Our insert function looks something like this
void insert(int key)
{
struct node* go = head; // Head is global.
while(go != NULL)
{
go = go -> next;
}
go -> next = new node(key);
}
If the function insert is actually returning void, then how is it able to make changes to the linked list?
Does memory allocated by operator new (from the free store) act like a global variable?
new is not like global variable. In C++, Global Variable (assuming you not talking about global pointer) is allocated before your application entry point which is "main" is called and deallocate when your application is shutdown.
new in other hand allocate new memory in when it called and deallocate when you call delete
MyClass* c = new MyClass(); // Allocate
// ..
delete c; // Deallocate, MyClass c is deleted
So if you make some object with new, and never delete it. It will always exist but you may lost the pointer to the data allocated and causing memory leak.
Related
I have created a function to append a node at the front of a linked list in c++. If I don't comment the last line i.e. the delete temp line, the program runs into an infinite loop but if I comment it, everything runs fine. I was hoping to free the memory allocated to the pointer temp to avoid memory leak. Why is it causing a problem?
void addFront(Node **head, int item)
{
Node *temp = new Node(); // Allocating new memory.
temp->data = item; //Storing data in the node.
temp->next = *head; //Linking temp pointer to head pointer.
*head = temp; //Resetting the head pointer as the new first node.
//delete temp; //Deallocating memory.
}
You are calling 'new' once, so you only allocated node once. There is no need to "avoid leak" since you never copied the node.
Don't confuse pointers with the pointee. delete temp deletes the object pointed to by temp, but that is the node you just inserted in your tree.
You do not need to manually manage the memory of the pointer itself, because it uses automatic storage.
new Node(); does more than allocating memory. It allocates memory and creates a Node by calling its constructor. Instead of two phase construction, members should be initiliazed by calling the constructor.
Slightly modified comments:
void addFront(Node **head, int item)
{
Node *temp = new Node(item,*head); // create new node. temp is a pointer to it
*head = temp; // head points to the new node
//delete temp; // delete the new node ?!?
}
There is no memory leak, because now *head does point to the newly created node.
This is a memory leak:
void foo() {
int* x = new int;
}
because after the function returns you have no way to access x and have no way to release its memory. While this is not a memory leak:
void bar() {
int * x;
}
As mentioned above, x itself uses automatic storage and its memory is released automatically when the function returns. Colloquially we say: x is allocated on the stack.
Why is it causing a problem?
If the purpose of the function is to add a new node, then it should follow that there would be more nodes after the function than there were before calling the function.
If you create one node, and delete it, then there would be no more nodes after the function than there was before. This contradicts with the described postcodition that there should be more nodes than there was before.
You haven't shown the part of the program where you do this, but presumably you somehow use the list after you've created it. You cannot use nodes that you've already deleted.
If I don't comment the last line i.e. the delete temp line, the program runs into an infinite loop
Deleting the node there is the wrong way to fix the infinite loop that you haven't shown.
I was hoping to free the memory allocated to the pointer temp to avoid memory leak.
You will have to follow the following order of operations:
Create node
Use node
Free node
You cannot do the following. It won't work:
Create node
Free node
Use node (can't do this after freeing the memory)
void add_first(int e) {
Node* newest = new Node(e, first);
first = newest;
n++;
}
On this code i declared a node pointer "newest".
list->add_first(4);
Let's say i call that function for the second time.
list->add_first(8)
Is it still that same *newest from the first time i called the function pointing at a new node OR did the previous *newest die after the function and on the second function call a new *newest was generated?
//Full code in case you need extra information
template <typename T>
class List {
struct Node {
T elem;
Node* next;
Node(T elem = 0, Node* next = nullptr) : elem(elem), next(next) {}
};
Node* first;
size_t n;
public:
List () : n(0), first(nullptr) {}
~List () {
while (first != nullptr) {
Node* aux = first;
first = first->next;
delete aux;
}
}
bool addFirst(T e) {
Node* newest = new Node(e, first);
first = newest;
n++;
return true;
}
bool add(T e, size_t p) {
if (p > n) return false;
if (p == 0) return addFirst(e);
Node* newest = new Node(e);
Node* aux = first;
size_t i = 1;
while (i++ < p) {
aux = aux->next;
}
newest->next = aux->next;
aux->next = newest;
n++;
return true;
}
bool addLast(T e) {
return add(e, n);
}
};
new Node(e, first)
will allocate new memory on the heap for an object of type Node, where it will remain until you manually deallocate it using delete. If you don't retain a reference to it, then you can't deallocate it and you have a memory leak.
Node* newest = ...;
will create a pointer of type Node on the stack until it goes out of scope (in this case, when the function returns), initializing it to reference the object you just created.
It's important to note that newest is not the object; it is merely referencing it for the time being.
first = newest;
The type of first is not provided here, but presumably in order for it to compile it is defined at a higher scope, e.g. a member variable, and accepts an assignment of type Node*...so it's probably a Node* itself.
In which case this will by default set first to reference the same object pointed to by newest. It is not pointing to newest in this scenario, but both are now referencing the same object.
When the function returns, newest(the pointer) will be destroyed because it is defined within the scope of the function invocation, but the Node object you created will live on the heap, as well as first which references it because it's defined outside this scope.
If you immediately call this function again, e.g. in a loop, you will allocate another object of type Node, set first to that object, and the object previously pointed to by first is now orphaned on the heap without a reference.
EDIT: I did just notice that you pass first into the constructor for Node, so it's possible you're making a separate connection therein.
newest (i.e. the pointer) dies when the function returns (because it is a local variable). The next time the function is called, a new newest variable is created and destroyed again.
*newest (i.e. the object it's pointing to) remains alive until you explicitly delete it (because you created it with new)
Pointers are just types which hold a memory address of an instance of a specific type. In your example you dynamically allocate memory to store an instance of Node using new and you store its address to a pointer (notice that new returns with a pointer which points to the allocated memory). When the function returns, the pointer "dies", but the allocated memory remains allocated.
what's the difference between these two methods?
Method(CustomClass t)
{
CustomClass *temp = &t;
}
called like this
Method(CustomClass(1,2,3));
and
Method(CustomClass* t)
{
CustomClass *temp = t;
}
called like this
Method(new CustomClass(1,2,3));
i haven't coded in c++ for a few years now and am having some trouble remembering. I'm coming from c# where every class need to be dynamically allocated with 'new'. The thing is that i don't undertand the difference between alocating an object dynamically with 'new' and calling it normally. How does temp differ in the 2 examples?
More specific example: I was creating a linked list and for my list i had this method:
void List::AddNew(TestClass node)
{
if (!first)
{
first = &node;
}
else
{
bool setFirst = false;
if (!last)
setFirst = true;
TestClass *temp;
temp = last;
last = &node;
if (temp)
temp->next = last;
if (setFirst)
first->next = last;
}
}
where first and last are TestClass *; the list wasn't initialized correctly (first and last pointed to the same value) and i had no idea why, so i changed the method to this:
void List::AddNew(TestClass* node)
{
if (!first)
{
first = node;
}
else
{
bool setFirst = false;
if (!last)
setFirst = true;
TestClass *temp;
temp = last;
last = node;
if (temp)
temp->next = last;
if (setFirst)
first->next = last;
}
}
and now it works. I'm missing a basic principle for pointers and i can't seem to guess that it is.
Here is TestClass also:
class TestClass
{
public:
int x, y;
TestClass *next;
TestClass *prev;
TestClass();
TestClass(int,int);
~TestClass();
};
what's the difference between these two methods?
One of them is using stack (automatic) allocation, while the other is using heap (dynamic) allocation.
Method(CustomClass(1,2,3)); // stack based
Method(new CustomClass(1,2,3)); // heap based
If you're going to use new you need to make sure you delete that reference also. Otherwise, you'll have a memory leak.
yes, i know this but what's the real defference between the
allocations and when should i use one or the other?
Object lifetime. If you put something on the stack, you will only be able to use it within a particular function, and any functions below it will need it passed as a parameter. If you put it on the heap, you can return a reference to it and use it wherever you want. You can't return a reference / address to a stack variable since it's life ends when the function returns.
I think you're most confused about when to use dynamic-allocation vs when to use stack-allocation. The only thing to know is that you should use stack-based allocation when there is no need for dynamic-based allocation. When is dynamic- (or heap) allocation needed you ask? Well, normally you would use it when you need an object to exist beyond the scope in which it was created or when the contents of that dynamically-allocated object rely on a strictly runtime mechanism (like the addition of elements to a vector (the size may not be known at compile-time)). Also, this...
T* t = &t;
Is not heap-allocation. This is simply a pointer with automatic-storage duration (on the stack) pointing to another object on the stack (if t is itself stack-based). Allocation on the heap happens only when new is used.
CustomClass *temp;
temp is only a pointer. It points to NULL(ok, actually it can point everywhere, but you should set it to NULL if no object exists) or to a already existing class.
CustomClass tmp;
Creates a object.
Method(CustomClass* t):
Demands for a already existing Object (t = pointer to existing object).
CustomClass *temp = t;
Assigns the pointer to a new local pointer.
Method(CustomClass t):
Should create a copy of the class you pass (not sure...)
CustomClass *temp = &t;
& = Adress-Operator. Retrieves address of t and saves it to the local pointer temp.
mfg
I'm creating something similar to structure list. At the beginning of main I declare a null pointer. Then I call insert() function a couple of times, passing reference to that pointer, to add new elements.
However, something seems to be wrong. I can't display the list's element, std::cout just breaks the program, even though it compiler without a warning.
#include <iostream>
struct node {
node *p, *left, *right;
int key;
};
void insert(node *&root, const int key)
{
node newElement = {};
newElement.key = key;
node *y = NULL;
std::cout << root->key; // this line
while(root)
{
if(key == root->key) exit(EXIT_FAILURE);
y = root;
root = (key < root->key) ? root->left : root->right;
}
newElement.p = y;
if(!y) root = &newElement;
else if(key < y->key) y->left = &newElement;
else y->right = &newElement;
}
int main()
{
node *root = NULL;
insert(root, 5);
std::cout << root->key; // works perfectly if I delete cout in insert()
insert(root, 2);
std::cout << root->key; // program breaks before this line
return 0;
}
As you can see, I create new structure element in insert function and save it inside the root pointer. In the first call, while loop isn't even initiated so it works, and I'm able to display root's element in the main function.
But in the second call, while loop already works, and I get the problem I described.
There's something wrong with root->key syntax because it doesn't work even if I place this in the first call.
What's wrong, and what's the reason?
Also, I've always seen inserting new list's elements through pointers like this:
node newElement = new node();
newElement->key = 5;
root->next = newElement;
Is this code equal to:
node newElement = {};
newElement.key = 5;
root->next = &newElement;
? It would be a bit cleaner, and there wouldn't be need to delete memory.
The problem is because you are passing a pointer to a local variable out of a function. Dereferencing such pointers is undefined behavior. You should allocate newElement with new.
This code
node newElement = {};
creates a local variable newElement. Once the function is over, the scope of newElement ends, and its memory gets destroyed. However, you are passing the pointer to that destroyed memory to outside the function. All references to that memory become invalid as soon as the function exits.
This code, on the other hand
node *newElement = new node(); // Don't forget the asterisk
allocates an object on free store. Such objects remain available until you delete them explicitly. That's why you can use them after the function creating them has exited. Of course since newElement is a pointer, you need to use -> to access its members.
The key thing you need to learn here is the difference between stack allocated objects and heap allocated objects. In your insert function your node newElement = {} is stack allocated, which means that its life time is determined by the enclosing scope. In this case that means that when the function exits your object is destroyed. That's not what you want. You want the root of your tree to stored in your node *root pointer. To do that you need to allocate memory from the heap. In C++ that is normally done with the new operator. That allows you to pass the pointer from one function to another without having its life time determined by the scope that it's in. This also means you need to be careful about managing the life time of heap allocated objects.
Well you have got one problem with your Also comment. The second may be cleaner but it is wrong. You have to new memory and delete it. Otherwise you end up with pointers to objects which no longer exist. That's exactly the problem that new solves.
Another problem
void insert(node *&root, const int key)
{
node newElement = {};
newElement.key = key;
node *y = NULL;
std::cout << root->key; // this line
On the first insert root is still NULL, so this code will crash the program.
It's already been explained that you would have to allocate objects dynamically (with new), however doing so is fraught with perils (memory leaks).
There are two (simple) solutions:
Have an ownership scheme.
Use an arena to put your nodes, and keep references to them.
1 Ownership scheme
In C and C++, there are two forms of obtaining memory where to store an object: automatic storage and dynamic storage. Automatic is what you use when you declare a variable within your function, for example, however such objects only live for the duration of the function (and thus you have issues when using them afterward because the memory is probably overwritten by something else). Therefore you often must use dynamic memory allocation.
The issue with dynamic memory allocation is that you have to explicitly give it back to the system, lest it leaks. In C this is pretty difficult and requires rigor. In C++ though it's made easier by the use of smart pointers. So let's use those!
struct Node {
Node(Node* p, int k): parent(p), key(k) {}
Node* parent;
std::unique_ptr<Node> left, right;
int key;
};
// Note: I added a *constructor* to the type to initialize `parent` and `key`
// without proper initialization they would have some garbage value.
Note the different declaration of parent and left ? A parent owns its children (unique_ptr) whereas a child just refers to its parent.
void insert(std::unique_ptr<Node>& root, const int key)
{
if (root.get() == nullptr) {
root.reset(new Node{nullptr, key});
return;
}
Node* parent = root.get();
Node* y = nullptr;
while(parent)
{
if(key == parent->key) exit(EXIT_FAILURE);
y = parent;
parent = (key < parent->key) ? parent->left.get() : parent->right.get();
}
if (key < y->key) { y->left.reset(new Node{y, key}); }
else { y->right.reset(new Node{y, key}); }
}
In case you don't know what unique_ptr is, the get() it just contains an object allocated with new and the get() method returns a pointer to that object. You can also reset its content (in which case it properly disposes of the object it already contained, if any).
I would note I am not too sure about your algorithm, but hey, it's yours :)
2 Arena
If this dealing with memory got your head all mushy, that's pretty normal at first, and that's why sometimes arenas might be easier to use. The idea of using an arena is pretty general; instead of bothering with memory ownership on a piece by piece basis you use "something" to hold onto the memory and then only manipulate references (or pointers) to the pieces. You just have to keep in mind that those references/pointers are only ever alive as long as the arena is.
struct Node {
Node(): parent(nullptr), left(nullptr), right(nullptr), key(0) {}
Node* parent;
Node* left;
Node* right;
int key;
};
void insert(std::list<Node>& arena, Node *&root, const int key)
{
arena.push_back(Node{}); // add a new node
Node& newElement = arena.back(); // get a reference to it.
newElement.key = key;
Node *y = NULL;
while(root)
{
if(key == root->key) exit(EXIT_FAILURE);
y = root;
root = (key < root->key) ? root->left : root->right;
}
newElement.p = y;
if(!y) root = &newElement;
else if(key < y->key) y->left = &newElement;
else y->right = &newElement;
}
Just remember two things:
as soon as your arena dies, all your references/pointers are pointing into the ether, and bad things happen should you try to use them
if you ever only push things into the arena, it'll grow until it consumes all available memory and your program crashes; at some point you need cleanup!
I'm attempting to craft my own basic singly linked list in C++ as a learning exercise, and I'm encountering some difficulty in the memory management department. As it stands I have...
A 'Node' class:
class Node
{
public:
char *value;
Node *next;
Node();
~Node();
};
Node::Node()
{
}
Node::~Node()
{
delete[] value;
}
And then my list (I've omitted certain method calls for brevity):
class LinkedList
{
private:
Node *head;
public:
LinkedList();
~LinkedList();
void Add(char **x);
};
LinkedList::LinkedList()
{
head = 0;
}
LinkedList::~LinkedList()
{
Node *temp;
Node *current = head;
while(current)
{
temp = current;
current = current->next;
delete temp;
}
}
void LinkedList::Add(char **x)
{
Node *nodeToAdd = new Node();
nodeToAdd->value = *x;
nodeToAdd->next = NULL;
Node *current = head;
if(!head)
{
head = nodeToAdd;
return;
}
while(current->next)
{
current = current->next;
}
current->next = nodeToAdd;
}
I'm attempting to use this code as follows (again I've omitted things for brevity):
int main()
{
LinkedList *list = new LinkedList();
char *alpha = "alpha";
char *beta = "beta";
char *charlie = "charlie";
char *delta = "delta";
char *echo = "echo";
list->Add(&alpha);
list->Add(&beta);
list->Add(&charlie);
list->Add(&delta);
list->Add(&echo);
delete list;
}
The last call in main to delete the list produces an error:
Debug Assertion Failed! Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
What am I doing wrong here?
The data pointed to by the various Node::value aren't dynamically allocated, so you shouldn't delete them. Applying the concept of "ownership", nodes should either make their own copies of data, which they own and can delete, or nodes don't own data, so they shouldn't be responsible for deleting it.
You can also implement multiple ownership using reference counting, like Objective-C does (see Objective-C Memory Management Rules for more info) but you have to be careful to avoid ownership cycles. You often find some type of reference counting in third-party smart pointers, such as Boost's smart_ptr library. Since you're doing this for the learning experience, it may make more sense to roll your own than use a library. Of course, you could also use a library for now, letting you focus on whatever you're trying to learn.
One day a student came to Moon and said: “I understand how to make a better garbage collector. We must keep a reference count of the pointers to each cons.”
Moon patiently told the student the following story:
“One day a student came to Moon and said: ‘I understand how to make a better garbage collector...
you are trying to release the memory which is not allocated on heap.
char *alpha = "alpha"; --- not allocated on heap
calling delete[]in Node destructor would lead to heap corruption.
Some points:
1) initialize pointers properly in the constructor:
Node::Node():value(NULL),next(NULL)
{
}
2) Take a ownership of value.
Allocate the memory on heap and copy
the contents
You shouldn't release a pointer use delete[]/delete if it's not created by new operator. There are some actions under the hood for the delete[] operation, like releasing/reclaiming marked memory from a managed pool. Since your pointer doesn't belong to these stuff, there will be a problem. IMHO, the underlying delete[] code is the _BLOCK_TYPE_IS_VALID(pHead->nBlockUse) stuff.
The problem is that you're assuming that you can delete the data inside node, but you're passing in pointers to string literals instead, which you can't delete.
If you're assuming that the Node object controls the lifetime of the data inside it, your Node constructor or the Add function in LinkedList will have to make a copy of the data that it is being passed.
In your destructor, you are trying to array delete (delete [ ]) a static string. You have change your Add function to reserve the string and copy it first. See the code below.
However, if I were you and fairly new to memory management, I'd really use something like CString instead of a raw "char *" as it's much easier to deal with.
void LinkedList::Add(const char *x)
{
Node *nodeToAdd = new Node();
int len=strlen(x);
nodeToAdd->value = new char [len+1]; // room for string + terminating 0
strcpy(nodeToAdd->value,x);
nodeToAdd->next = NULL;
Node *current = head;
if(!head)
{
head = nodeToAdd;
return;
}
while(current->next)
{
current = current->next;
}
current->next = nodeToAdd;
}
value and next in Node class doesn't have memory allocated. You should allocate memory in Node's constructor.