I had to implement a function that looked like this:
MyList * sum (MyList * l1, MyList * l2) {
MyList * newlist = new MyList();
//Adds two objects and place the result in a third new list
return newlist;
}
The function took two lists and placed the sum of each object into a new list. The MyList
class had nodes with pointers to the next
variable and the objects inside the list were user defined.
And that got me thinking - how should I deal with the dynamic allocation of memory from the objects and the list itself? Since I had to create memory for each of the objects of the new list.
Is there any way to place the values of the sum of objects in the new list without having to rely on dynamic allocation? Maybe by doing something like this:
Object result(node1->content + node2->content);
Node->content = &result; // will this object be erased when the function ends?
instead of this:
Node->content = new Object(node1->content + node2->content);
How should I deal with the lifetime of the new list created inside of the function in relation to the variable that will hold the memory after the function ends? Can I do something like this when returning the new list?
MyList & sum (MyList * l1, MyList * l2) {
//Create variable without allocating memory and return it's reference
}
In short, my main doubt is how to deal with the lifetime of an object that is created inside a function and will be held by other object.
In C# programs, memory for objects needs to be allocated dynamically. Dynamic memory allocation for objects or other types of data is implemented using the ‘new’ operator. The general form of the ‘new’ operator
Example of dynamic memory allocation on the heap is: While allocating memory on heap we need to delete the memory manually as memory is not freed (deallocated) by the compiler itself even if the scope of allocated memory finishes (as in case of stack).
Programmatically, there is no limit for Dynamic Memory Allocation in Java. The only limit has to do with the actual physical memory on the machine that the java code executes. By saying “physical memory” we mean both RAM and virtual memory. When does Dynamic Memory Allocation Occur in Java?
An example of memory allocation for an array of structures Memory allocation for an array of structures is implemented in two stages. First, memory is allocated for an array of variables (references). Then, memory is allocated for each item of the array (each structural variable).
Node->content = &result; // will this object be erased when the function ends?
Yes, since it's a local variable. As soon as its function terminates, so does result
's lifetime.
MyList & sum (MyList * l1, MyList * l2) {
//Create variable without allocating memory and return it's reference
}
This will fail too, for similar reasons as the above.
I suggest you use std::shared_ptr
or std::unique_ptr
. If you wish, read Differences between unique_ptr and shared_ptr.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With