Link lost problem

It's been a year since I did programming. I'm into C programming again but got stuck on link list.

The program runs correctly but after I input all 5 strings the program gets an error.

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
#include <stdio.h>
#include <stdlib.h>

typedef struct TestNode
{
    int Num;
    char *Test;
    float Deci;
    TestNode *Next;
}TestNode;

int main()
{
    TestNode *x, *p;
    x = (TestNode*)malloc(sizeof(TestNode));
    p = x;
    unsigned const int Zero = 0;

    for(int i=0;i<=5;i++)
    {
        p->Next = (TestNode*)malloc(sizeof(TestNode));
        p->Next->Test = (char*)malloc(sizeof(char)*10);
        gets(p->Next->Test);
        p->Next->Num = 100 * 2 + 6 / 3 % 10 <= 10 * (16 - 6 / 3) && Zero;
        p->Next->Deci = 167.34 - 10.354 * (9.77 + p->Next->Num);
        p = p->Next;
    }
    p->Next = NULL;

    p = x;

    printf("%d, %.2f\n", p->Next->Num, p->Next->Deci);

    while(p->Next != NULL)
    {
        puts(p->Next->Test);
        p = p->Next;
    }

    free(x);
    free(p);  //not sure about this since it is also pointing to an allocated variable.
    free(p->Next->Test);
    free(p->Next);
    return 0;
}
closed account (zb0S216C)
Untrue wrote:
 
&& Zero

What's that doing there?

Untrue wrote:
 
free(p);

If you do this, don't free() x (if p is equal to x).
Wazzak
Last edited on
Just being sure if it's really 0.
Last edited on
You should call

free(p->Next->Test);

before calling

free(p->Next);

and before calling

free(p);

As Framework stated, you should call either free(x); or free(p);, not both.
I've traced every pointers though or it could be memory leak. Gaaagh still getting a program error. :(

Maybe somebody can trace the exception code?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Problem signature:
  Problem Event Name:	APPCRASH
  Application Name:	blank.exe
  Application Version:	0.0.0.0
  Application Timestamp:	500160b2
  Fault Module Name:	blank.exe
  Fault Module Version:	0.0.0.0
  Fault Module Timestamp:	500160b2
  Exception Code:	c0000005
  Exception Offset:	000014f1
  OS Version:	6.1.7601.2.1.0.256.48
  Locale ID:	1033
  Additional Information 1:	0a9e
  Additional Information 2:	0a9e372d3b4ad19135b953a78882e789
  Additional Information 3:	0a9e
  Additional Information 4:	0a9e372d3b4ad19135b953a78882e789
1
2
3
while(p->Next != NULL)
   //...
free(p->Next->Test);
Also, you allocated 6 7 nodes, so you need to deallocate 6 7 nodes.
Last edited on
Untrue wrote:
got stuck on link list.


You and me both!! I hate Link Lists!! I hope you do better than I'm doing. :)
I know it's been a day, maybe you've changed your code.

The loop at line 34 makes p == NULL, so you can't free it at line 41.

I think you want to return p to x, and then free while x:
1
2
3
4
5
6
7
8
9
10
//@ line 40:
p = x;
while (x != NULL)
{
  if (x->Test != NULL) // Which you should initialize these pointers to
    free(x->Test);
  p = x;
  x = x->Next;
  free(p);
}


Of course one thing to do is to let yourself have memory leaks until you can isolate the cause of the crash.
Last edited on
Just a correction
The loop at line 34 makes p->Next == NULL

Also line 2 has no purpose.
And just as with delete, free(NULL); make nothing.
THANKS guys! The program worked after I changed free(p), free(p->Next->Test), free(p->Next) to free(x), free(x->Next->Test), free(x->Next).
Topic archived. No new replies allowed.