Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
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 ??????????
 
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 6-May-12 10:26am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  

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

  Print Answers RSS
0 Nirav Prabtani 268
1 OriginalGriff 267
2 _Amy 170
3 Mika Wendelius 165
4 Sergey Alexandrovich Kryukov 162
0 Nirav Prabtani 284
1 OriginalGriff 207
2 _Amy 170
3 Mika Wendelius 165
4 Sergey Alexandrovich Kryukov 119


Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 6 May 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100