在C ++中实现链接列表时出现未处理的异常访问错误读取位置错误 [英] Unhandled exception access voilation reading location error while implementing linked list in c++

查看:66
本文介绍了在C ++中实现链接列表时出现未处理的异常访问错误读取位置错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下功能:

I have the following functions :

void each_user_specifications :: algorithm_dequeue_stage (single_connection_per_user_queue *q, int tot_queues)
{
	packet_with_attributes *temp1, *temp2;
	unsigned int temp_tot_size = 0;
	int counter = 0, queue_counter = 0;
	int pkt_size = 0, temp_pkt_size = 0;
	static int run_count = 0;
	int ready_to_dequeue_flag = 0, pkt_count = 0, complete_pkt_sent_flag = 0;

	run_count ++;

	for (int i=0; i < tot_queues; i++)
	{
		cout <<(q+i)->tokens_generated<<" are generated for this queue\n";
		temp1 = (q+i)->head;

		while (temp1 != NULL)
		{
			temp_tot_size += temp1->new_packet_size;
			temp1 = temp1->new_packet;
		}

		temp1 = (q+i)->head;

		while (temp1 != NULL)
		{
			pkt_size = temp1->new_packet_size;
			temp_pkt_size = pkt_size;

			while (pkt_size != 0)
			{
				if (temp_tot_size <= (q+i)->tokens_generated)
				{
					//temp2 = temp1;
					ready_to_dequeue_flag = 1;
					break;
				}

				pkt_size --;
				temp_tot_size --;
			}

			if (ready_to_dequeue_flag == 1)
			{
				(q+i)->tokens_generated = (q+i)->tokens_generated - temp_tot_size;
				(q+i)->current_size -= temp_tot_size;
				(q+i)->sum_tokens += temp_tot_size;
				(q+i)->avg_tokens_lead_or_lagged = (q+i)->sum_tokens/run_count;

				if (pkt_size == temp_pkt_size)
				{
					complete_pkt_sent_flag = 1;
					pkt_count ++;
				}

				while (temp1->new_packet != NULL)
				{
					temp1 = temp1->new_packet;
					pkt_count ++;
				}

				temp2 = temp1;
				ready_to_dequeue_flag = 0;
				break;
			}

			temp1 = temp1->new_packet;
		}

		temp_tot_size = 0;

		while (pkt_count > 0)
		{
			(q+i)->head = (q+i)->dequeue_the_packet ((q+i)->head);

			if ((q+i)->head == NULL)
				break;

			pkt_count --;
		}

		/*temp1 = (q+i)->head;

		while (temp1 != NULL)
		{
			temp2 = temp1;

			if (temp1->new_packet == NULL)
				break;

			temp1 = temp1->new_packet;
		}

		temp2 = temp1;*/

		if (complete_pkt_sent_flag == 0)
			temp2->new_packet_size = temp_pkt_size - pkt_size;

		temp1 = (q+i)->head;

		pkt_count = 0;

		queue_counter ++;

		cout <<"Dequeuing done for queue number "<<queue_counter<<" having class "<<(q+i)->cos<<"\n";

		while (temp1 != NULL)
		{
			cout <<temp1->new_packet_size<<"'"<<temp1->new_packet_arrival_time<<"\n";
			temp1 = temp1->new_packet;
		}

		system ("PAUSE");
	}
}




packet_with_attributes* single_connection_per_user_queue :: dequeue_the_packet (packet_with_attributes* temp_head)
{
	packet_with_attributes *old_temp_head1, *old_temp_head2;
	old_temp_head1 = new packet_with_attributes;
	old_temp_head2 = new packet_with_attributes;

	old_temp_head1 = temp_head;
	
	while (old_temp_head1->new_packet != NULL)
	{
		old_temp_head2 = old_temp_head1;
		old_temp_head1 = old_temp_head1->new_packet;
	}

	old_temp_head2->new_packet = NULL;

	if (temp_head == NULL)
		return temp_head;

	else 
	{
		delete old_temp_head1;
		return temp_head;
	}
}



我在第一个函数的最后一个while循环中遇到错误,尽管有时它不会引发错误并自由运行并使数据包出队.但有时,它会碰到断点并引发访问冲突读取位置的错误.

我不知道我在做什么错?我已经尝试过答案中发布的所有内容,请提供帮助.



I am getting error in the last while loop of the first function, although sometimes it''s not kicking an error and running freely and dequeuing the packets. But sometimes, it hits the breakpoint and kicks an error of access voilation reading location.

I don''t know, what I am doing wrong ? I have tried everything posted in answers, please help.

推荐答案

我怀疑函数dequeue_the_packet.

你这样做...

old_temp_head1 = temp_head;

while(old_temp_head1-> new_packet!= NULL)

及以后..

如果(temp_head == NULL)
返回temp_head;

现在,当您执行old_temp_head1-> new_packet
时,如果temp_head == NULL,您应该已经有访问冲突.
因此,也许此函数有错别字或其他错误,这会导致您的数据损坏.
I am suspicous of the function dequeue_the_packet.

You do this...

old_temp_head1 = temp_head;

while (old_temp_head1->new_packet != NULL)

and later..

if (temp_head == NULL)
return temp_head;

Now you should already have had an access violation if temp_head == NULL when you executed old_temp_head1->new_packet

So maybe this function has a typo in it or an other bug, and that is leading to your data corruption.


我在这里可以看到一些问题.请进行以下更正并重新运行:

1.使packet_with_attributes * single_connection_per_user_queue :: dequeue_the_packet(packet_with_attributes * temp_head)静态功能; (我将在下面解释其原因)

2.如前所述,摆脱这两行:
I can see a few problems here. Please make these corrections and re-run:

1. Make packet_with_attributes* single_connection_per_user_queue :: dequeue_the_packet (packet_with_attributes* temp_head) static function; (I will explain the reason for this below)

2. As suggested before, get rid of these two lines:
old_temp_head1 = new packet_with_attributes;
old_temp_head2 = new packet_with_attributes;


这行:


and this line:

delete old_temp_head1;


在dequeue_the_packet()函数中.
基本上,忘记这一点.函数主体应如下所示(如果我理解正确的话):


within dequeue_the_packet() function.
Basically, forget about this. The function body should look like this (if I understand it correctly):

{
	while (temp_head && temp_head->new_packet != NULL)
	{
		temp_head = temp_head->new_packet;
	}
 
	if(temp_head) temp_head->new_packet = NULL;
 
	return temp_head;
}



3.现在,在algorithm_dequeue_stage()函数中,您需要更改以下行:



3. Now, in the algorithm_dequeue_stage() function you need to change this line:

(q+i)->head = (q+i)->dequeue_the_packet ((q+i)->head);


至:


to:

(q+i)->head = packet_with_attributes::dequeue_the_packet ((q+i)->head);



现在,让我解释一下使函数静态化的原因.看,如何摆弄要传递给函数的temp_head对象.在某些阶段,对象可能会变为NULL.这意味着它将是无效的,并且如果在此对象内调用该函数将导致崩溃.



Now, let me explain the reason for making the function static. See, how you fiddling with the temp_head object you are passing to the function. At some stage the object may become NULL. This means that it will be invalid and if the function is called within this object there will be a crash.


感谢您抽出宝贵的时间并以如此简短的方式发布解决方案.虽然,我还没有尝试过两种解决方案中的任何一种.但是,我将尝试这些,而且我很确定他们将解决此问题,因为它们看起来完全不同.我的意思是,这一次,解决此问题的方法看起来完全不同.

再次感谢您发布如此简短的解决方案.
Thanks for taking your time and posting solutions in such a brief manner. Although, I haven''t tried any one of the two solutions. But, I am going to try these, and I am pretty sure that they are going to solve this problem because they look completely different. I mean, this time, the way to solve this problem looks completely different.

Once again, thanks a lot for posting such a brief solutions.


这篇关于在C ++中实现链接列表时出现未处理的异常访问错误读取位置错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆