Wizard Sultan
Wizard Sultan

Reputation: 918

issues in understanding pointers

The following codes are from the book Programming Interviews Exposed I am having problem to understand the concept of pointers. Why can't we use the code no. 1.

Code no. 1

bool insertInFront( IntElement *head, int data )
{
    IntElement *newElem = new IntElement;
    if( !newElem ) return false;
    newElem->data = data;
    head = newElem; // Incorrect!
    return true;
}

Code no. 2

bool insertInFront( IntElement **head, int data )
{
    IntElement *newElem = new IntElement;
    if( !newElem ) return false;
    newElen->data = data;
    *head = newElem; // Correctly updates head
    return true;
}

Upvotes: 0

Views: 193

Answers (4)

Unsigned
Unsigned

Reputation: 9916

  • In code 1, you are assigning the pointer location of newElem to the local copy of head. When the function returns, head is destroyed, and you will have a memory leak (you lose the pointer to newElem, leaving you with no way to delete it.) You've only changed the function's local copy of the pointer, the caller's copy is unaffected.

  • In code 2, head is a pointer-to-a-pointer. You don't have just a pointer, you actually have a pointer to the caller's pointer. This allows you to alter the caller's pointer, storing the pointer to newElem When the function returns, head is destroyed, but it's only the pointer-to-a-pointer. The original pointer is intact in the caller's scope.

Upvotes: 4

Manas
Manas

Reputation: 608

Say you are calling function 1 as:

insertInFront(H, data);

Upon calling of a function, the computer makes duplication of arguments, then release them when the function returns. So in code No.1, head=newElem assigned the address of newElem to head(which is a duplicate of H), then released head, and the address of newElem is lost forever. In code No.2, however, the function should be called as:

insertInFront(&H, data);

This means the ADDRESS of H is duplicated to head, and the address of newElem is assigned to *head, i.e. where head is pointing, which results in H. In this way you get the address of newElem after the function returns.

Upvotes: 1

Lochemage
Lochemage

Reputation: 3974

The concept of pointers are very complicated. Basically the way you need to think about pointers are that they are 'values' themselves. so:

*head = some_memory_address_that_points_to_head;

could be thought of as:

int im_a_pointer_to_head = some_memory_address_value_to_head;

So if we apply this same concept to your first function:

bool insertInFront(int im_a_pointer_to_head, int data)...

You are essentially passing in a copy of the value of your head pointer, changing it within the function only changes the temporary copy made for this function and does not actually change where the original pointer is pointing to.

The second function solves this because you are actually passing in the copy of the pointer that is pointing to the pointer that is pointing to the head (try saying that 3 times fast!). In this case, you are not changing the copied value, but instead are changing where the actual pointer is pointing to.

Upvotes: 1

Paul Dardeau
Paul Dardeau

Reputation: 2619

To change the value of anything passed via a function argument (and have that change persisted) outside of the function call, you MUST have the memory address of whatever it is whose value you want to change. This is another way of saying that you must "pass by reference" instead of "pass by value" -- otherwise pass by value will cause the function to just alter the COPY.

Upvotes: 0

Related Questions