0

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--;
    }
}
Matt
  • 22,721
  • 17
  • 71
  • 112
russell
  • 3,668
  • 9
  • 43
  • 56
  • 3
    Perhaps the local object `x` is always created at the same address, what were you expecting to see? – CB Bailey Aug 19 '11 at 06:41
  • Ok, i want to iterate over the whole list. But if local object x created in same address then it is replacing the old rather than adding new node. So what is the solution? – russell Aug 19 '11 at 06:45
  • In fact , i know using pointer i can easily insert new node, but i was trying to insert new node without using pointer. Is it not possible?? – russell Aug 19 '11 at 06:48
  • Perhaps you should expand the detail of your question to say what you are trying to achieve. From the code it is not obvious what you are trying to do. If you want to implement a linked list you almost certainly need some sort of dynamic memory management. – CB Bailey Aug 19 '11 at 06:50
  • Ok,guys thanks to everyone. I upvoted all the answer. It's tough to select accepted answer since everyone is correct. But since @Daren Thomas first exactly answer that i want to know, i accept his answer. – russell Aug 19 '11 at 07:02

8 Answers8

2

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).

Frerich Raabe
  • 90,689
  • 19
  • 115
  • 207
2

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.

Jonny Dee
  • 837
  • 4
  • 12
1

Your are creating the Node on the stack - try using new.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
1

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).

Daren Thomas
  • 67,947
  • 40
  • 154
  • 200
1

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;
Sander De Dycker
  • 16,053
  • 1
  • 35
  • 40
1

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;
Paul Mitchell
  • 3,241
  • 1
  • 19
  • 22
1

You keep setting next to q:

Node x(i * 10, q);
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
1

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);
alexm
  • 6,854
  • 20
  • 24