windows triggered breakpoint due to corruption of heap

Hi,
I was implementing queue in some pre-written program.
There I get this heap corruption error. I predict the error might be
of array index overflow..
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
// global declaration
#define MAX_LIST 20
int front=0, rear=0;
typedef struct byteslog
{
	ULONG serverCode;
	long totalBytes;
}byteslog;
byteslog ser[MAX_LIST];

void
pushToQ(ULONG code, long sentbytes, long recvbytes)
{
	if(rear >= MAX_LIST)
	{
		//debug("Queue overflow");
		return;
	}

	for(int i=0; i<MAX_LIST; i++)
	{
		// if server is already added just update bytes
		if(ser[i].serverCode == code) 
		{
			ser[i].totalBytes=(sentbytes+recvbytes)/512;
			return;
		}
	}
	// add server and update bytes
	ser[rear].serverCode=code;
	ser[rear].totalBytes=(sentbytes+recvbytes)/512;
	rear++;
}

Any corrections ??
If (rear >= MAX_LIST) and there is no element with that code in the list, the last lines are executed.
Possible solution: put the second part of the code in a else {...}.
Last edited on
The if statement returns from the function, so that isn't it. I don't see anything that could result in array index problems, but...

EDIT: Also, it seems like maybe a map would be better for what you seem to be doing here; let the key be the code.
Last edited on
@Zhuge: You are correct.
@laxman: Maybe the function is called from different threads?
Topic archived. No new replies allowed.