Thread: A question about the difference between Call-By-Reference and Call-by-Value

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    17

    A question about the difference between Call-By-Reference and Call-by-Value

    Hi all, I am still having troubles with distinguishing call by reference and call by value. Let me ask my question on an example.

    Code:
    struct Node{
    int data;
    Node *next;
    Node *previous;
    };
    struct CircularDoublyLinkedList{
    Node *final;
    void create();
    void empty();
    void add(Node *newptr);
    };
    void CircularDoublyLinkedList::create(){
    final=NULL;
    }
    int main(){
    Node newnode={9,NULL,NULL};
    CircularDoublyLinkedList cdll;
    cdll.create();
    cdll.add(&newnode);
    for(int i=8;i>=0;i--){
    newnode.data=i;
    cdll.add(&newnode);
    }
    cdll.empty();
    }
    Here is my add method:

    Code:
    void CircularDoublyLinkedList::add(Node *newNode)
    {
        if(final==NULL)
        {
            newNode->next=newNode;
            newNode->previous=newNode;
            final = newNode;
            return;
        }
    
    
        newNode->next=final->next;
        final->next->previous = newNode;
        final->next=newNode;
        newNode->previous=final;
    
    
    }
    Now, after all these things, I realized that we were sending the address of a Node-type variable. And we didn't allocate the memory dynamically for it. My questions are:

    1- As far as I understood, there is only 1 node, and we're changing its data continuously in main. We are not creating another node to add to the cdll, are we?
    2- Since we didn't allocate the memory dynamically, how the heck can we deallocate it ?

    Your helps are greatly appreciated here Thanks.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Warning! Indent style not found!

    1)What the create() function do? The add()?
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by blackolick_ View Post
    1- As far as I understood, there is only 1 node, and we're changing its data continuously in main. We are not creating another node to add to the cdll, are we?
    2- Since we didn't allocate the memory dynamically, how the heck can we deallocate it ?

    Your helps are greatly appreciated here Thanks.
    1. That is correct. (And of course that's not what you want -- you want a bunch of nodes, so you need to be creating some dynamically.)
    2. You don't, in the same way you don't deallocate i in your example either. It is destroyed when it goes out of scope.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    17
    Here is create:
    Code:
    void CircularDoublyLinkedList::create(){
    final=NULL;
    }
    And I meant CircularDoublyLinkedList::add method.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    17
    Quote Originally Posted by tabstop View Post
    1. That is correct. (And of course that's not what you want -- you want a bunch of nodes, so you need to be creating some dynamically.)
    2. You don't, in the same way you don't deallocate i in your example either. It is destroyed when it goes out of scope.
    First of all thanks for your reply, but that was my midterm question. Structures and main was given, and I was asked to write add and empty methods...

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If main was given, then your add function is obligated to create a new node to add to the linked list (via new).

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    17
    Quote Originally Posted by tabstop View Post
    If main was given, then your add function is obligated to create a new node to add to the linked list (via new).
    You are the man Thanks a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [Newbie] C++ call by reference question
    By learn in forum C++ Programming
    Replies: 3
    Last Post: 10-31-2013, 02:44 PM
  2. Call by reference question
    By badtwistofate in forum C Programming
    Replies: 4
    Last Post: 03-24-2009, 02:40 PM
  3. call by reference question
    By staticalloc in forum C Programming
    Replies: 2
    Last Post: 09-09-2004, 12:58 PM
  4. Call-by-value Vs. Call-by-reference
    By Wiz_Nil in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2002, 09:06 AM

Tags for this Thread