Link List Getting Invalid(garbage value)

]Hi there i started working on Link List but i got the invalid value in my programe..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<iostream.h>
#include<conio.h>
struct std
{
 int marks;
 std *next;
};
   void trave(std *start)
  {
  std * ptr;
  ptr=start;
  while(ptr!=NULL)
	  {
	   cout<<(ptr->marks);
	   ptr=ptr->next;
	   cout<<endl;
	   }

    }
void main()
{
 clrscr();
 
 std *s, *start,*ptr;
 
 start=s;
 s=new std;
 
 s->marks=300;
 s->next=ptr;

 ptr =new std;
 
 ptr->marks=450;
 ptr->next=ptr;

 ptr=new std;
 
ptr->marks=444;
 ptr->next=NULL;
 s->next->next=ptr;

  trave(start);
 getch();


}
Last edited on
Well, it's not a good idea to use "std" as a name, since there's a C++ namespace already called that. I took the liberty of renaming your struct as "node".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include<conio.h>

struct node
{
    int marks;
    node *next;
};

void trave(node *start)
{
    node * ptr;
    ptr=start;

    while (ptr!=NULL)
	{
        std::cout << (ptr->marks);
	    ptr = ptr->next;
	    std::cout << std::endl;
	}
}

int main()
{
    clrscr();

    node *s, *start,*ptr;

    start = s;

    s = new node;

    s->marks = 300;
    s->next  = ptr;

    ptr = new node;

    ptr->marks = 450;
    ptr->next  = ptr;

    ptr = new node;

    ptr->marks = 444;
    ptr->next = NULL;
    s->next->next = ptr;

    trave(start);

    getch();
}


There are uninitialised and incorrectly-used pointers here.
Line 29: start = s; - none of the variables have been initialised at this point, so this line is not useful, it just replaces one garbage value with a different garbage value.

Line 34: s->next = ptr; s is a good (valid) pointer. but ptr still is uninitialised, so this is not a good move. It means s->next now contains garbage.

Line 41: ptr = new node; ptr has already contained the address of a new node. It should have been deleted before this line. Now the previous value is lost and there is a memory leak.

Line 45: s->next->next = ptr; Remember line 34? And the fact that s->next contains garbage. Well, attempting to access memory using that pointer is not allowed, so this instruction must fail.

I didn't get any further than that. But the main thing is to make sure all pointers are valid before trying to use them. And always pair the use of the new operator with a corresponding delete of the same pointer to de-allocate the memory when finished.
Last edited on
Thanks for Help but i still not getting
How to Pass the Address of starting point as i did trave(start);
then how can do this ....
As I said, I didn't get that far, the program failed before I reached line 47.

Just make sure that your pointers *s, *start,*ptr have been properly initialised, either by using new, or by setting the pointer to some valid address.

Consider this:
1
2
3
4
int x, y, z;
z = x + y;
cout << x << endl;
cout << z << endl;

this code is not sensible because none of the integers have been initialised, so the results are unpredictable.

If you do similar things with uninitialised pointers, the results are just as meaningless, but far more dangerous, and guaranteed to do something bad.
Last edited on
Thanks dear i got the point now i modify my program. Now look as


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include<iostream.h>
#include<conio.h>
struct node
{
 int marks;
 node *next;
};
 void trave(node *start)
 {
  node * ptr;
  ptr=start;
  while(ptr!=NULL)
	{
	 cout<<(ptr->marks);
	 ptr=ptr->next;
	 cout<<endl;
	}

 }
void main()
{
 clrscr();
 node *s,*su, *start,*ptr;


 s=new node; //New element of node by pointer s

 s->marks=300;


 ptr =new node; //New element of node by pointer ptr

 s->next=ptr;   //Addres of ptr contanis s' next portition
 ptr->marks=450;

 su=new node;   //New element of node by pointer su
 ptr->next=su;   //Addres of su contanis ptr next portition 

 su-> marks=333; 

 su->next=NULL;  //last element of  list 

 s->next->next=su;
 start=s;
 trave(start);
 getch();


}
closed account (3qX21hU5)
It is very important to remember like chervil said to pair the use of new with delete. If you just create new stuff on the stack it is eventually going to cause problems so always remember to delete them when you are finished with them.
That looks better.

A small point, line 43: s->next->next=su; is not needed. This line can be removed. s->next contains ptr, and line 37 already did this: ptr->next=su;

It would be good to clean up at the end like this:
1
2
3
4
5
6
7
8
9
10
    start=s;
    trave(start);

    delete s;
    delete ptr;
    delete su;

    getch();

}
Last edited on
My version of the code. It is identical, but I altered the style. I hope it becomes easier to read. That might be a matter of opinion :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
void main()
{
    clrscr();

    node *node_a, *node_b, *node_c;

    node_a  = new node;
    node_b  = new node;
    node_c  = new node;

    node_a->marks = 300;        // First element of list
    node_a->next  = node_b;

    node_b->marks = 450;
    node_b->next  = node_c;

    node_c->marks = 333;
    node_c->next  = NULL;         // Last element of  list

    trave(node_a);

    delete node_a;
    delete node_b;
    delete node_c;

    getch();

}
Last edited on
Thanks all of u for ur kind attention on my problem..
with this error i learned more and more


But who i can make a list of till finite numbers..eg this code contains a list of 3 suppose if the requirement demands 100 number of list then ..how it can be control
Thanks again..
Last edited on
It will probably be easier to use separate functions to control the adding of new elements to the list. You don't have to use a proper C++ class to do that, but it's one way of keeping everything under control.

I played around with this idea, my example here is just an experiment, it may not be the best answer. One problem here is there is no way to directly reach the last element. Instead, it is a matter of stepping from the start, one-by-one until the last element is found.

You could probably come up with a better solution where a separate pointer to the last element is maintained somewhere.

If the comment // is removed from line 28, you can see each element is properly destroyed at the end. Notice the new on line 40 is matched by the delete on line 30.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include<iostream.h>
#include<conio.h>

class node
{
public:
    node(int);
    ~node();

    void insert(int);
    int getMarks() { return marks; };
    node * next() { return nxt; };

private:
    int marks;
    node *nxt;
};

node::node(int n)
{
    marks = n;
    nxt = NULL;
}

node::~node()
{
// Next line is for information/debugging purposes only
//    cout << "Destructor: " << marks << " " << nxt << endl;

    delete nxt; // ok with NULL pointer
}

void node::insert(int n)
{
    node *p = this;             // p points to current node

    while (p->nxt)              // Find empty node ( = last node).
        p = p->nxt;

    p->nxt = new node(n);
}

//------------------------------------

void trave(node *start)
{
    node * ptr = start;

    while (ptr != NULL)
    {
        cout << ptr->getMarks();
        ptr = ptr->next();
        cout << endl;
    }

}

//------------------------------------

int main()
{
    clrscr();

    node *start = new node(300);   // First element of list

    start->insert(450);

    start->insert(333);


    for (int i=1; i<=10; i++)
    {
        int number = i*i;          // this can be anything you like
        start->insert(number);
    }

    trave(start);                  // Display the list

    delete start;                  // free the entire list

    getch();

    return 0;

}
Last edited on
Topic archived. No new replies allowed.