trouble with linked list

So, I'm using allegro game programming library. Before I move on to bigger and better things I wnat to get a handle on pointers. Specifically with things like structs, linked lists and maybe binary trees in the future. I posted a program that uses a linked list to keep tracks of a variable number of objects on the screen. GDB tells me it fails in the main

also, the new style of posting is kinda buggy, for example I can move the cursor past the program code.

gdb:
1
2
3
4
5
6
7
8
9
10
11
12
    Program received signal EXC_BAD_ACCESS, Could not access memory.
    Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000006
    [Switching to process 84322]
    0x0000000100001a60 in _al_mangled_main ()
    (gdb) bt
    #0 0x0000000100001a60 in _al_mangled_main ()
    #1 0x000000010006c697 in call_user_main [inlined] () at /Users/me/Desktop/stuff/allegro/src/macosx/osx_app_delegate.m:214
    #2 0x000000010006c697 in +[AllegroAppDelegate app_main:] (self=<value temporarily unavailable, due to optimizations>, _cmd=<value temporarily unavailable, due to optimizations>, arg=<value temporarily unavailable, due to optimizations>) at /Users/me/Desktop/stuff/allegro/src/macosx/osx_app_delegate.m:225
    #3 0x00007fff8029a114 in __NSThread__main__ ()
    #4 0x00007fff8210afd6 in _pthread_start ()
    #5 0x00007fff8210ae89 in thread_start ()

code:
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
    #include <cstdlib>
    #include <stdlib.h>
    #include <allegro5/allegro.h>
    #include <allegro5/allegro_primitives.h>
    const float FPS = 60;
    const int SCREEN_W = 640;
    const int SCREEN_H = 480;
    const int BOUNCER_SIZE = 32;
    float startx = 570;
    float starty = 200;
    int max = 11;
    float dx = -10;
    struct circle
    {
    float x;
    float y;
    float dy;
    struct circle *c;
    } *top;
    struct circle* deepcopy(struct circle*& a);
    void add(struct circle*& a);
    void pop(struct circle*& a);
    void renew(struct circle*& a);
    void push(struct circle*& a, struct circle* b);
    void move(struct circle*& a);
    int count(struct circle* a);
    int count(struct circle* a)
    {
    int c = 0;
    for(struct circle* b = a;b!=NULL;b=b->c)
    {
    c++;
    }
    return c;
    }
    struct circle* deepcopy(struct circle*& a)
    {
    struct circle* b = new circle;
    b->x=a->x;
    b->y=a->y;
    b->dy=a->dy;
    return b;
    }
    void add(struct circle*& a)
    {
    if(a!=NULL)
    {
    struct circle *nnew;
    nnew->x=startx;
    nnew->y=starty;
    nnew->dy= (float)((rand() % (2*max)) - max);
    nnew->c = a;
    a = nnew;
    }
    else {
    a = new struct circle;
    a->x=startx;
    a->y=starty;
    a->dy= (float)((rand() % (2*max)) - max);
    }
    }
    void pop(struct circle*& a)
    {
    struct circle *c = a;
    a=a->c;
    free(c);
    }
    void renew(struct circle*& a)
    {
    struct circle *nnew;
    while(a!=NULL)
    {
    if(!((a->x<=0)||(a->x>=SCREEN_W)||(a->y<=0)||(a->y>=SCREEN_H)))
    {
    struct circle *b = deepcopy(a);
    push(nnew,b);
    }
    pop(a);
    a=a->c;
    }
    a = nnew;
    }
    void push(struct circle*& a, struct circle* b)
    {
    b->c=a;
    a = b;
    }
    void move(struct circle*& a)
    {
    struct circle *b = a;
    while(b!=NULL)
    {
    b->x+=dx;
    b->y+=b->dy;
    b=b->c;
    }
    }
    int main(int argc, char **argv)
    {
    ...
    while(1)
    {
    ...
    if(redraw && al_is_event_queue_empty(event_queue)) {
    redraw = false;
    al_clear_to_color(al_map_rgb(0,0,0));
    //al_draw_bitmap(bouncer, bouncer_x, bouncer_y, 0);
    if(count(top)<=max)
    {
    add(top);
    }
    for(struct circle *a=top;a!=NULL;a=a->c)
    {
    al_draw_bitmap(bouncer,a->x,a->y,0);
    }
    move(top);
    renew(top);
    al_flip_display();
    }
    }
    al_destroy_bitmap(bouncer);
    al_destroy_timer(timer);
    al_destroy_display(display);
    al_destroy_event_queue(event_queue);
    return 0;
    }


for the newbies who are curious I figured it out on my own. I posted this question in 3 different places, and finally posted the solution at Daniweb (same nick).

I'll post it here as well:

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
    #include <cstdlib>
    #include <stdlib.h>
    #include <allegro5/allegro.h>
    #include <allegro5/allegro_primitives.h>
    const float FPS = 60;
    const int SCREEN_W = 640;
    const int SCREEN_H = 480;
    const int BOUNCER_SIZE = 32;
    float startx = 570;
    float starty = 200;
    int max = 11;
    float dx = -10;
    struct circle
    {
    float x;
    float y;
    float dy;
    struct circle *c;
    } *top;
    void deepcopy(struct circle*& a, struct circle*& b);
    void add(struct circle*& a);
    void pop(struct circle*& a);
    void renew(struct circle*& a);
    void push(struct circle*& a, struct circle* b);
    void move(struct circle*& a);
    int count(struct circle* a);
    int count(struct circle* a)
    {
    int c = 0;
    for(struct circle* b = a;b!=0;b=b->c)
    {
    c++;
    }
    return c;
    }
    void deepcopy(struct circle*& a, struct circle*& b)
    {
    b->x=a->x;
    b->y=a->y;
    b->dy=a->dy;
    }
    void add(struct circle*& a)
    {
    if(a!=0)
    {
    struct circle* nnew = new circle;
    nnew->x=startx;
    nnew->y=starty;
    nnew->dy= (float)((rand() % (2*max)) - max);
    nnew->c = a;
    a = nnew;
    }
    else {
    a = new circle;
    if(a==0)
    {
    throw "In add, new operator failed";
    }
    a->x=startx;
    a->y=starty;
    a->dy=(float)((rand() % (2*max)) - max);
    a->c=0;
    }
    }
    void pop(struct circle*& a)
    {
    struct circle *c = a;
    a=a->c;
    free(c);
    }
    void renew(struct circle*& a)
    {
    struct circle *nnew;
    while(a!=0)
    {
    if(!((a->x<=0)||(a->x>=SCREEN_W)||(a->y<=0)||(a->y>=SCREEN_H)))
    {
    struct circle *b = new circle;
    deepcopy(a,b);
    push(nnew,b);
    }
    pop(a);
    }
    a = nnew;
    }
    void push(struct circle*& a, struct circle* b)
    {
    b->c=a;
    a = b;
    }
    void move(struct circle*& a)
    {
    struct circle *b = a;
    while(b!=0)
    {
    b->x+=dx;
    b->y+=b->dy;
    b=b->c;
    }
    }
    int main(int argc, char **argv)
    {
    ...
    while(1)
    {
    ...
    if(redraw && al_is_event_queue_empty(event_queue)) {
    redraw = false;
    al_clear_to_color(al_map_rgb(0,0,0));
    //al_draw_bitmap(bouncer, bouncer_x, bouncer_y, 0);
    if(count(top)<=max)
    {
    add(top);
    }
    if(top==0)
    {
    throw "top is still 0";
    }
    // al_draw_bitmap(bouncer,a->x,a->y,0);
    struct circle *a = top;
    while (a->c!=0)
    {
    al_draw_bitmap(bouncer,a->x,a->y,0);
    if(a->c!=0)
    {
    a=a->c;
    }
    }
    move(top);
    renew(top);
    al_flip_display();
    }
    }
    ...
    return 0;
    }


I would appreciate seasoned veterans dishing on this. Maybe there is a better way of going about it? Just remember I used a linked list because I have a variable amount of objects on the screen
Topic archived. No new replies allowed.