0

I am writing a code to make a linked list. one of the function I wrote in order to make a linked list easier in the main function was[node is the name of a struct conatains a.data b.pointer name next) :

    node& create_node(int value)
    {
    node newitem;
    newitem.data=value;
    newitem.next=NULL;
    return (newitem);
    };

When I write the function like this everything is ok but I want to write the function header as :

    node * create_node(int value)

But when I write it that way and I write

  return *newitem;

I get an error. I wanted to ask why the VS shows me an error in the 2nd way and what is the difference between using * and & [I already read here about references but I don't understand why one should use it in functions as , from what I understood using references takes additional space and not contributing ] .

edit :thank you for help, when I posted this it was before I even ran a test on the main function only tried to avoid mistakes before compilation . It took me some time but now I see the fundamental mistake I did .

Man in Black
  • 89
  • 2
  • 2
  • 12
  • 5
    Your first function is not correct. You are returning a reference to a local object that will be destroyed once the function ends. You should also take a look at this: http://codereview.stackexchange.com/questions/42958/linkedlist-with-node-implementation – NathanOliver Mar 27 '15 at 20:15
  • @NathanOliver I should have duped it: http://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope – πάντα ῥεῖ Mar 27 '15 at 20:18
  • 'When I write the function like this everything is ok ' - besides a warning you should not ignore - your code is fundamental broken. –  Mar 27 '15 at 20:18
  • I think you should read [What are the differences between a pointer variable and a reference variable in C++?](http://stackoverflow.com/questions/57483/what-are-the-differences-between-a-pointer-variable-and-a-reference-variable-in). – R Sahu Mar 27 '15 at 20:18

3 Answers3

3

Your code returns a reference to a variable.

Unfortunately you return a reference to a local variable. This will fail, because the local variable will be destroyed uppont returning, but the caller will still try to reference it (that's UB) !

So if you want to return a reference, you shall make sure the object still exist:

node& create_node(int value) { 
    node* newitem = new node;
    newitem->data=value;
    newitem->next=NULL;
    return (*newitem);  // return the objet: this will be then be converted to a reference
}

You could also work with pointers as suggested by another answer. However in this case, I'd opt for shared_ptr:

shared_ptr<node> create_node(int value) { 
    node* newitem = new node;
    newitem->data=value;
    newitem->next=NULL;
    return (shared_ptr<node>(newitem)); 
}
Christophe
  • 68,716
  • 7
  • 72
  • 138
2

If you want to return a pointer you should use a pointer:

node* create_node(int value)
{
    node *newitem = new node;
    newitem->data = value;
    newitem->next = NULL;
    return newitem;
};

Also please consider who'll delete the object then.

1

You should return &newitem.

But given the fact that your newitem is only available in this function's scope, the returned pointer will point to a destroyed object, so "nothing", or rather it will result in undefined behavior.

I guess you want your newitem to be created dynamically.

node * create_node(int value)
{
    node * newitem = new node;
    newitem->data=value;
    newitem->next=NULL;
    return newitem;
};
jfouquet
  • 11
  • 2