Jack Cheng
Jack Cheng

Reputation: 121

c++ passing a pointer as a parameter, and its counterpart in java

If we are given a list and each ListNode is defined as a struct:

Struct ListNode
{
    int val;
    ListNode *next;
    //and some constructor
}

and we are given the head pointer of the list, denoted by ListNode *head. Now if I pass ListNode *head as a parameter into a function such as

int sum(ListNode *head){}

and then I traverse the list without making a copy of *head. For example, I do

for(; head!=null; head=head->next)

Will *head still point to the head node of the list after the function is called?

And in Java, if we define ListNode as a class and do the same traversal with ListNode head (without making a copy), what will happen then?

Can someone illustrate this from a memory-allocation perspective? Thanks

Notes: I get this confused because of the List interface in Java. I think the value of an ArrayList can be changed even if we pass it as a parameter into the function. But in C++ this does not happen?

Upvotes: 1

Views: 225

Answers (4)

Arjun
Arjun

Reputation: 44

In C++, You are passing the Address of the first node From main to a function.So the value in head(in main) will not change.That is,the variables head(in main) and head(in function) are two different variables.head(in function) is only changing.

Upvotes: 0

Jobs
Jobs

Reputation: 3377

In Java, a copy of the head reference will be passed into the function. However, since a copy of the head reference is still a reference to the ListNode, you are still successfully iterating through your original linkedList.

You mentioned:

if I pass ListNode *head as a parameter into a function such as

int sum(ListNode *head){}

and then I traverse the list without making a copy of *head.

Yes that's correct. And in Java, you can do the same (traverse the list without making a copy of head) because you are traversing using a copy of head.

Upvotes: 0

Dima
Dima

Reputation: 40500

You said "without making a copy", and this is in fact, the key to your answer: when the function is called, a copy of head value is passed to it as a parameter. So, whatever changes the function does to it inside of it, do not affect the caller's copy in any way.

This is called "pass-by-value". In C++, there is a way to pass parameters by reference: int sum((ListNode *)& head){}, a function declared this way gets the same head as the caller has, not a copy, and whatever changes it makes to it, will also be visible to the caller.

Java and C do not have this ability, the functions always get a copy of the caller's value as a parameter, and cannot make any changes to it that the caller would see.

Upvotes: 1

Brett Kail
Brett Kail

Reputation: 33946

Will *head still point to the head node of the list after the function is called?

Yes. By assigning to head, you're only modifying the pointer, not the memory pointed to. If you assign to *head, head->val, etc., then that update would be observed in the caller.

And in Java, if we define ListNode as a class and do the same traversal with ListNode head (without making a copy), what will happen then?

The same as in C. By assigning to head, you only modify the reference, not the memory referenced.

Upvotes: 1

Related Questions