Weird errors, please help.

This is a discussion on Weird errors, please help. within the C++ Programming forums, part of the General Programming Boards category; Thanks for everyone's help so far but now I have a new error when I try to run my program. ...

  1. #16
    Registered User
    Join Date
    Feb 2008
    Posts
    43
    Thanks for everyone's help so far but now I have a new error when I try to run my program.

    18 [main] Driver2 5868 _cygtls::handle_exceptions: Exception: STATUS_ACCESS_VIOLATION

    623 [main] Driver2 5868 open_stackdumpfile: Dumping stack trace to Driver2.exe.stackdump

    Code:
    #include "compTree.h"
    #include <iostream>
    
    
    int main(){
    	CompTree t;
    	t.add(10);
    	t.add(9);
    	t.add(8);
    	
    	cout<< t.remove();
    	
    	return 0;
    }
    Code:
    #include "compTree.h"
    
    
    
    
    CompTree::CompTree(){
    	v[0] = -1;  //set rank 0 to null because the root of the tree starts at rank 1
    }
    
    CompTree::Position CompTree::add(int x){
    	v.push_back(x);
    	return last();
    }
    
    int CompTree::remove(){
    	int temp = v.back();
    	v.pop_back();
    	return temp;
    }
    
    int CompTree::removeRoot(){
    	v[1]=last().element();
    	return remove();
    }
    
    CompTree::Position CompTree::last(){
    	if(!isEmpty())
    		return Position(v.size()-1,&v);
    }
    
    CompTree::Position CompTree::root(){
    	if(!isEmpty())
    		return Position(1,&v);
    }
    
    CompTree::Position CompTree::leftChild(Position &p){
    	if(!isEmpty())
    		return Position(p.rank*2 ,&v);
    }
    
    CompTree::Position CompTree::rightChild(Position &p){
    	if(!isEmpty())
    			return Position(p.rank*2+1 ,&v);
    }
    
    CompTree::Position CompTree::sibling(Position &p){
    	int temp = parent(p).rank;
    	if(v.at((int)floor(temp/2))== p.element())
    		return Position((int)floor(temp/2+1),&v);
    	else
    		return Position((int)floor(temp/2),&v);
    }
    
    CompTree::Position CompTree::parent(Position &p){
    	return Position((int)floor(p.rank/2) ,&v);
    }
    
    bool CompTree::isInternal(Position &p){
    	int temp = (int)floor(p.rank*2);
    		if ((int)v.size()>= temp)
    			return true;
    		else
    			return false;
    }
    
    bool CompTree::isExternal(Position &p){
    	int temp = (int)floor(p.rank*2);
    	if ((int)v.size()>= temp)
    		return false;
    	else
    		return true;
    }	
    
    bool CompTree::isEmpty(){
    	if(v.at(1) != -1)
    		return false;
    	else
    		return true;
    }
    
    void CompTree::swap(CompTree::Position &p1, CompTree::Position &p2){
    	int temp = p1.element();
    	p1.ptr->at(p1.rank) = p2.element();
    	p2.ptr->at(p2.rank) = temp;
    }
    
    int CompTree::size(){
    	return v.size()-1; //because v[0] is not considered part of the tree.
    }
    
    CompTree::~CompTree(){};
    
    CompTree::Position::Position(int r, vector<int> *vp){
    	rank = r; 
    	ptr = vp;
    }
    
    int CompTree::Position::element(){
    	return ptr->at(rank);
    }
    
    bool CompTree::Position::isNull(){
    	if(rank == -1)
    		return true;
    	else
    		return false;
    }
    CompTree::Position::~Position(){}
    Code:
    #ifndef COMPTREE_H_
    #define COMPTREE_H_
    
    #include <vector>
    #include <math.h>
    
    using namespace std;
    
    
    class CompTree {
    public:
    	class Position;
    	
    	vector<int> v;
    	
    	CompTree();
    	Position add(int x);
    	Position last();
    	Position root();
    	Position leftChild(Position &p);
    	Position rightChild(Position &p);
    	Position sibling(Position &p);
    	Position parent(Position &p);
    	int remove();
    	int removeRoot();
    	bool isInternal(Position &p);
    	bool isExternal(Position &p);
    	bool isEmpty();
    	void swap(Position &p1, Position &p2);
    	int size();
    	~CompTree();
    	
    	
    	
    	class Position {
    	public:
    		Position(int r, vector<int> *vp);
    		int element();
    		bool isNull();
    		int rank;
    		vector<int> *ptr;
    		~Position();
    	};
    		
    };
    #endif /*COMPTREE2_H_*/

  2. #17
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Your constructor accesses the vector out-of-bounds. You cannot access any item through indices in an empty vector.
    You can use push_back or construct the vector with one item with value -1 to begin with:
    Code:
    CompTree::CompTree():
        v(1, -1)
    {
    	//v[0] = -1;  //was out of bounds
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP with DX9 Errors!!!
    By Tommaso in forum Game Programming
    Replies: 7
    Last Post: 06-28-2006, 03:51 PM
  2. Errors with header files in OpenGL using VisualC++
    By wile_spice in forum Game Programming
    Replies: 3
    Last Post: 06-22-2006, 09:56 AM
  3. Weird Errors in VS 2003
    By Devil Panther in forum Windows Programming
    Replies: 1
    Last Post: 10-01-2005, 07:16 AM
  4. executing errors
    By s0ul2squeeze in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2002, 01:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21