Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++:When creating a new objects inside a function and returning it as result, must I use the new operator to create the object?

I have two dummy questions which have confused me for a while. I did do some searching online and read through much c++ tutorials, however I cannot find concrete answers.

Say we have a class named Node which is a building block of a singly linked list.

class Node
{
   int data;
   Node* next;
}

Fact1: local variables(non static) will be destroyed upon the exit of the corresponding functions.

Question1: How about the situations blow:

Node* func()
{ 
    Node n; 
    Node* ptr=&n; 
    return n;
}

Will the node n be destroyed? Or we have to use the new operator to create the node and return a pointer to a heap memory. If both ways work, which one is a better approach?

Question2: How to write a destructor for the node class? (I find some similar questions on stackOverflow, but those answers focused on the destructor for the linked list. I already got that part. What I want is exactly a destructor for a Node class).

---------------------------------------EDIT------------------------------------

Thank all who gave me suggestions or pointed out my errors. I think I got my answers. Below is a note taken by me from your answers and that really knock out my confusions.

  1. It is not a good practice to return a stack memory address from a function since it will lead to undefined behaviors.
  2. Returning a heap memory is OK but we have to take care of the destruction of the objects.
  3. An alternative will be returning an object, benefiting from the copy constructor.
like image 338
user3367047 Avatar asked Oct 10 '14 07:10

user3367047


People also ask

Do you have to use new to return an object?

No, you don't have to use new to return an object from a function. You can return a copy of a local object. Q1: Will the node n be destroyed? The return type of the function is Node* but you're returning n which is a Node.

What is the new operator in C++?

The new operator assigns space in the memory to the object only during run time which means the allocation is dynamic. Note: The constructor can be a default constructor or a user-defined one.

How do you return an object in C++?

c++ - Returning object from function. A function can also return objects either by value or by reference. When an object is returned by value from a function, a temporary object is created within the function, which holds the return value. This value is further assigned to another object in the calling function.

How to create objects in C #programming?

A typical C# program creates many objects, which as you know, interact by invoking methods. We can Create objects in C# in the following ways: 1) Using the ‘new’ operator: A class is a reference type and at the run time, any object of the reference type is assigned a null value unless it is declared using the new operator.


3 Answers

Question 1

Node* func() { Node n; Node* ptr=&n; return n;}

Your code creates a local Node instance (on the stack), then returns its address. When the function returns, the Node instance, being a local variable, is destroyed. The address the function returned now points to some memory with undefined contents, and any attempts at dereferencing this pointer will lead to undefined behavior.

In order to create a node, you actually need to call a Node constructor. How you want to return the result is relevant to how you call the constructor.

  • You can either return a pointer as you were trying to do, in which case you need to use the new operator:

      Node* func() { 
        Node* n = new Node(10); 
        return n;
      }
    

    However, when you do this, you give func callers the responsibility to destroy the object in question. Since new and delete are symmetrical operations, it is considered better form to put them in symmetrical places in your code, e.g. like this:

      void cnuf(Node* p) { 
        delete p; 
      }
    

    A better alternative altogether may be to use std::shared_ptr which gives you reference counting, like this:

      std::shared_ptr<Node> func() {
        return std::make_shared<Node>(10);
      }
    

    Using this approach, the callers do not need to manually manage each node's lifecycle. Another alternative is using std::unique_ptr instead, which only allows single object ownership.

  • Or you can return the node by value, in which case you create it locally, and then let the function return mechanisms make a copy when you return it:

      Node func() { 
        Node n(10); 
        return n;
      }
    

Question 2

You can declare a destructor like this in your Node class declaration:

class Node {
  ...
  ~Node();
}

Then, you can define it like this:

Node::~Node() {
  ...
}

However, it is probably better to actually let the list managed the connection between its Node instances (next field), and only let the Node class manage the lifecycle of its member data (data field)

like image 196
Martin J. Avatar answered Oct 20 '22 00:10

Martin J.


You can return pointer to local object, but it will be pointed to stack memory, so results may be suprising. Look the following code:

#include <iostream>

using namespace std;

class Node { public: int n; };

Node* create(int n) {
    Node node = Node();
    node.n = n;
    cout << "Created " << node.n << endl;
    return &node;
}

int main() {
   Node* n1 = create(10);
   Node* n2 = create(20);
   cout << "Reading " << n1->n << endl;
   cout << "Reading " << n2->n << endl;
   return 0;
}

You won't get "10" "20" output. Instead

Created 10
Created 20
Reading 20
Reading 1891166112

First object was destructed (when first create function call ended). Second object was created on top of destructed n1, so n1 address was equal to n2 address.

Compiler will warn you when you return stack addresses:

main.cpp: In function Node* create(int):
main.cpp:8:10: warning: address of local variable node returned [-Wreturn-local-addr]
     Node node = Node();
like image 34
danbst Avatar answered Oct 19 '22 23:10

danbst


What you did probably mean is:

Node* func()
{ 
    Node n(10); 
    return &n;
}

But that would lead to undefined behavior, as the Node n would be on the stack and not under your control.

Node* func()
{ 
    Node* n = new Node(10); 
    return n;
}

That would work, but you would need to free the Node* in your destructor.

If you can use c++11 features I'd probably go with an std::unique_ptr<Node>:

class Node
{
   int data;
   std::unique_ptr<Node> next;
}
std::unique_ptr<Node> func()
{ 
    std::unique_ptr<Node> n(new Node(10)); 
    return n;
}

That way your Node would be freed by the destructor of the std::unique_ptr<>.

like image 27
Theolodis Avatar answered Oct 20 '22 00:10

Theolodis