Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deal with dynamic allocation when implementing list of objects?

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.

like image 612
Eduardo macedo Avatar asked Aug 30 '17 16:08

Eduardo macedo


People also ask

How to allocate memory for an object dynamically?

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

What is dynamic memory allocation on the heap?

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).

What is the limit of dynamic memory allocation in Java?

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?

How is memory allocated to an array of structures?

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).


1 Answers

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.

like image 123
gsamaras Avatar answered Nov 15 '22 00:11

gsamaras