简体   繁体   中英

Pointer address does not change in a link list

My problem is q->next always prints the same address, but I assigned q = &x; . Why it is not printing different addresses?

#include <stdio.h>


class Node
{
public:
    int val;
    Node *next;
    Node(int v,Node *p) { val=v, next=p; }
};


int main()
{
    Node head(0, NULL);
    Node *q = &head;

    int i = 5;
    while (i>0)
    {
        Node x(i * 10, q);
        q = &x;
        printf("# %d %p\n", q->val, q->next);
        i--;
    }
}

In the first iteration of the loop, q contains the address of head . On each subsequent iteration, q contains the address of x .

This means that on the first iteration, q->next yields the address of head and on each subsequent iteration, q->next yields the address of x . However, x is created inside the loop, on the stack. Since there is no change to the stack inbetween, the x object always appears at the same place on the stack.

So I'd expect the program to print first the address of head and then four times the address of the four x objects (which all happen to be allocated at the same position of the stack).

I think the reason is that, within the while loop, you declare x on the stack. Then after the end of the while loop has been reached, the variable gets "destroyed". In the subsequent iteration, however, x gets reserved on the stack again using the exact same (stack) memory place.

Note that you won't get a linked list with valid pointers. You need to create Node instances on the heap using 'new' operator.

EDIT:

If you don't want to allocate memory on the heap you can use the "Linked lists using arrays of nodes" approach descriped here . The drawback is, however, that you need to know the maximum number of nodes in advance.

您正在堆栈上创建节点-尝试使用new。

This has to do with the way x is allocated: It is a local variable inside the main function. That means it is allocated on the stack, at a specific position. You are reusing the same piece of memory all the time. Instead, try allocating memory for new nodes ( new ).

x is a local variable in the while loop. Its lifetime is only one iteration of the loop.

You should dynamically allocate the Node objects like so :

Node* x = new Node(value, next);

so their lifetime lasts until you de-allocate the object :

delete x;

Node x is being created on the stack, each time you go round your loop it will be getting created and then destroyed again at the end of the block. And each time round the loop it will be being created in the same location.

You probably want:

Node *x = new Node( i*10, q );
q = x;

您继续设置q next

Node x(i * 10, q);

Your x node is allocated on the local stack, not on the heap, so as your variable gets recycled on each loop iteration it recieves the same local address. To create i = 5 uique nodes you need to allocate object on heap using new() operator. You would also to add code to destoy your allocated objects afterwards.

example:


Node * px = new  Node(i*10, 1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM