C++ Access Violation Exception?

Hi all

I am getting an access violation expection error in the function code below though everything has been intilaized proporely.

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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
struct queue
{
	
		long start;
		long duration;
		struct queue *next;
		struct queue *prev;
};

queue *H_queue_H;
queue *H_queue_L;
queue *H_queue_H_V;
queue *N_queue_H;
queue *N_queue_L;
queue *H_queue_head_H;
queue *H_queue_tail_H;
queue *H_queue_head_L;
queue *H_queue_tail_L;
queue *H_queue_head_H_V;
queue *H_queue_tail_H_V;
queue *N_queue_head_H;
queue *N_queue_tail_H;
queue *N_queue_head_L;
queue *N_queue_tail_L;
queue *H_queue_empty_H;
queue *H_queue_empty_L;
queue *H_queue_empty_H_V;
queue *N_queue_empty_H;
queue *N_queue_empty_L;


int Buffer_Call(int i)
{
	queue *b;
		if(i==0)
		{
			b=H_queue_empty_H;
			H_queue_empty_H=H_queue_empty_H->next; 
			b->start=currentTime;
			b->duration= currentTime +GenExpRV(mewHwait_H);
			if(H_queue_tail_H!=(queue *)NULL)
			{
				H_queue_tail_H->next=b;
				b->prev=H_queue_tail_H;
				b->next = (queue *)NULL;
				H_queue_tail_H=b;
			}
			else
			{
				H_queue_head_H=H_queue_tail_H=b;
				b->next = (queue *)NULL;
				b->prev = (queue *)NULL;
			

			}
			QH_H++;
		}
		else if(i==1)
		{
			b=H_queue_empty_L;
			H_queue_empty_L=H_queue_empty_L->next; 
			b->start=currentTime;
			b->duration= currentTime +GenExpRV(mewHwait_L);
			if(H_queue_tail_L!=(queue *)NULL)
			{
				H_queue_tail_L->next=b;
				b->prev=H_queue_tail_L;
				b->next = (queue *)NULL;
				H_queue_tail_L=b;
			}
			else
			{
				H_queue_head_L=H_queue_tail_L=b;
				b->next = (queue *)NULL;
				b->prev = (queue *)NULL;
			

			}
			QH_L++;
		}
		if(i==2)
		{
			b=H_queue_empty_H_V;
			H_queue_empty_H_V=H_queue_empty_H_V->next; 
			b->start=currentTime;
			b->duration= currentTime +GenExpRV(mewHwait_H_V);
			if(H_queue_tail_H_V!=(queue *)NULL)
			{
				H_queue_tail_H_V->next=b;
				b->prev=H_queue_tail_H_V;
				b->next = (queue *)NULL;
				H_queue_tail_H_V=b;
			}
			else
			{
				H_queue_head_H_V=H_queue_tail_H_V=b;
				b->next = (queue *)NULL;
				b->prev = (queue *)NULL;
			

			}
			QH_H_V++;
		}



		else if(i==3)
		{
			b=N_queue_empty_H;
			N_queue_empty_H=N_queue_empty_H->next;
			b->start=currentTime;
			b->duration=currentTime + GenExpRV(mewNwait_H);
			if(N_queue_tail_H!=(queue *)NULL)
			{
				N_queue_tail_H->next=b;
				b->prev=N_queue_tail_H;
				b->next = (queue *)NULL;
				N_queue_tail_H=b;

			}
			else
			{
				N_queue_head_H=N_queue_tail_H=b;
				b->next = (queue *)NULL;
				b->prev = (queue *)NULL;

			}
			QN_H++;
		}
		else if(i==4)
		{
			b=N_queue_empty_L;
			N_queue_empty_L=N_queue_empty_L->next;
			b->start=currentTime;
			b->duration=currentTime + GenExpRV(mewNwait_L);
			if(N_queue_tail_L!=(queue *)NULL)
			{
				N_queue_tail_L->next=b;
				b->prev=N_queue_tail_L;
				b->next = (queue *)NULL;
				N_queue_tail_L=b;

			}
			else
			{
				N_queue_head_L=N_queue_tail_L=b;
				b->next = (queue *)NULL;
				b->prev = (queue *)NULL;

			}
			QN_L++;
		}


		return 0;
}


The error seems to be in the following lines:

H_queue_empty_H=H_queue_empty_H->next;

H_queue_empty_L=H_queue_empty_L->next;

H_queue_empty_H_V=H_queue_empty_H_V->next;

N_queue_empty_H=N_queue_empty_H->next;

N_queue_empty_L=N_queue_empty_L->next;

but I don't understand since everything has been initialized earlier. What do you think guys? What seems to be the problem?

Any help/suggestions will be greatly appreciated :)

Thanks





Nothing has been initialized. Not in the code you posted anyway..
This is the initialization part for H_queue (H_queue being a double linked list). Other queues has a similar initialization process

H_queue_H= (struct queue *)malloc(max_QH_H*sizeof (struct queue));
memset(H_queue_H, 0, max_QH_H*sizeof(struct queue));
H_queue_head_H= H_queue_H;
for (i=0; i< max_QH_H-1; i++)
H_queue_H[i].next=H_queue_H+i+1;
H_queue_H[max_QH_H-i-1].next=(struct queue *) NULL;
H_queue_head_H = H_queue_tail_H=(struct queue *) NULL;
H_queue_empty_H=H_queue_H;
Still, this sounds like uninitialized pointer.
Try making line 25, queue *H_queue_empty_H = 0;, and adding printf("H_queue_empty_H is at %p\n", H_queue_empty_H); immediately after initialization and before line 38, to see if that pointer is initialized and if it isn't corrupted.
Last edited on
It turned out that the linked list was not intialized properly
The main error is this line:

H_queue_H[max_QH_H-i-1].next=(struct queue *) NULL;

When I exit for loop, I was setting first element's next field to NULL while I should have set the last one instead!

Thanks hamsterman :)
Topic archived. No new replies allowed.