Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have the following functions :

C++
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;
	}
}


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.
Posted
Updated 2-Oct-12 21:52pm
v6
Comments
Jochen Arndt 21-Sep-12 8:37am    
How is init_satq allocated by the initialization function? And what is the value of tot_queues (assuming that it is passed by reference to the initialization function and set their)?
wirelesscomm2k12 21-Sep-12 9:29am    
The function prototype of initialization function is:

single_connection_per_user_queue *each_user_specifications :: algorithm_each_user_parameters_initialization_stage (each_user_specifications u[], int obj_array_size, double sched_cycle_system_bandwidth, unsigned int &token_bank_capacity, int &tot_queues)

and this function is returning an array of objects belonging to the same class - i.e. single_connection_per_user_queue.
Jochen Arndt 21-Sep-12 10:04am    
Because the exception does not occur when called from inside the init function, possible error sources are the involved variables: the returned array pointer and the dimension value tot_queues. To exclude these error sources, you should show some of the code of the init function (or check it yourself). If you use something like *init_satq = new single_connection_per_user_queue[tot_queues] their and did not change tot_queues later, it should be OK.

You may add code to your question using the 'Improve question' link rather than trying to post it in comments.
wirelesscomm2k12 21-Sep-12 9:30am    
And yes, you are right, the variable tot_queues is passed by reference.
pasztorpisti 21-Sep-12 9:31am    
Providing more code might help if the other parts are not top secret.

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.
 
Share this answer
 
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:
C++
old_temp_head1 = new packet_with_attributes;
old_temp_head2 = new packet_with_attributes;

and this line:
C++
delete old_temp_head1;

within dequeue_the_packet() function.
Basically, forget about this. The function body should look like this (if I understand it correctly):
C++
{
	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. Now, in the algorithm_dequeue_stage() function you need to change this line:
C++
(q+i)->head = (q+i)->dequeue_the_packet ((q+i)->head);

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


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.
 
Share this answer
 
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900