Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I was writing a simple commandline C++ program , but I am running into Heap Corruption errors . The following is my program's code.
please can anyone help me ??????????

C++
void evenValue()
{

	cout<<endl<<"                  * * * WELCOME * * *"<<endl<<endl;
	cout<<"How many elements do you want to enter to queue: ";

	int length1,i,element1; //=====================  // My queue 
	queues q1;
	cin>>length1;
	cout<<"Enter "<<length1<<" elements: ";
	for(i=0;i<length1;i++)
	{
		cin>>element1;
		q1.insert_rear(element1);
	}
	//==========================================================================================================
int element;
int count = 0;
   		for(int ptr=q1.front;ptr<=q1.rear;ptr++)
			{	
				if(q1.queue[ptr]%2==0)
					count++;
	        }
	      //=====================  // My stack 
//	int length,element;
  //  cout<<"Enter many item do you want to fill the stack"<<endl;
    //cin>> length;
	
    My_Stack s1(count); 
  //  int * x;
  //  x = new int [count];

   cout << "Pushing elements " << endl;
    for (int i = 0; i < count; i++)
	 {  
		 for(int ptr=0; ptr < q1.Size();ptr++)
			{	
				if(q1.queue[ptr]%2==0)
				{
				element = q1.queue[ptr];
				s1.push(element);
			//	cout<<s.items[ptr]<<"    ";
				}
				}
	}
    	s1.display(s1);}
Posted

1 solution

Hard to tell without seeing your classes queues and My_Stack. But just from guessing:

a) count is set to the number of even elements in your queue.

b) then your stack is dimensioned to hold this count of elements.

c) in the second loop over the queue elements you use a different kind of iteration, namely 0 --- ql.Size(), instead of ql.front ... ql.read -- was that intentional

d) you push count*count elements onto your stack that was dimensioned for holding only count elements -- was that intentional?

The last statement looks somewhat redundant: sl.display(sl). Either it should be sl.display() or display (sl).

I hope that one of these observations gets you back on track.
 
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