Like Tree2Likes
  • 2 Post By Salem

Bus error

This is a discussion on Bus error within the C++ Programming forums, part of the General Programming Boards category; I am writing a double hashing program and I seem to have a problem here. I get always bus error. ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    102

    Bus error

    I am writing a double hashing program and I seem to have a problem here. I get always bus error. I have used std:: cout to print out where exactly the problem occurs and it seems to occur at E * newvalues = new T [newtavleLength]; Here is the part of the code. It is the part of the code where I rehash all the values in a new larger table (double old tbaleSize) after the number of values exceed 70% of tableLength.

    Code:
    template <typename T>
    void ContDynArray<T>::rehash() {	
    	size_t newtableLength = tableLength * 2;
    	
    			size_t newmaxvalues = 0.7 * newtableLength;
    			
    			T * newvalues = 0;
    			T * newprintvalues = 0;			
    			bool * newoccupied = false;
    
    			newvalues = new T[newtableLength];
    			
    			newprintvalues = new T[newmaxvalues];
    			
    			newoccupied = new bool [newtableLength];			
    			for (size_t i = 0; i < newtableLength; ++i) newoccupied[i] = false;			
    			for (size_t i = 0; i < tableSize; ++i) {
    				
    				unsigned long hashvalue = hashValue(printvalues[i]);
    				size_t newplace = hashvalue% newtableLength;
    				while(newoccupied[newplace]) newplace = collisionfunction (hashvalue, newplace);
    				newvalues[newplace] = printvalues[i];
    	    		newoccupied[newplace] = true;
    	    		newprintvalues[i] = printvalues[i];
    			}
    			
    			delete[] values;
    	    	delete[] occupied;
    	    	delete[] printvalues;
    	    	values = newvalues;
    	    	occupied = newoccupied;
    	    	printvalues = newprintvalues;
    	    	tableLength = newtableLength;
    }
    Last edited by Dontgiveup; 05-26-2012 at 05:48 PM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    2,540
    what operating system, compiler, and libraries are you using?

    you don't say whether this is the part of the code that actually causes the error. I honestly don't see how it could cause anything like a "bus error"

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,742
    > I have used std:: cout to print out where exactly the problem occurs and it seems to occur at E * newvalues = new T [newtavleLength];
    I suggest you use a debugger instead.
    cout is buffered, so you're not guaranteed to see text which is queued to cout immediately before a crash.

    In addition to telling you the exact line where it crashes, a debugger allows you to
    - examine any variables within any stack frame of the current call stack
    - examine global variables

    Not to mention
    - setting breakpoints before (and during) a test run
    - changing variables which seem "wrong" to you just to see if the code works OK if prior code were fixed.

    Once you get past the typical student homework which can be completed in a couple of hours, a debugger is a vital tool you should be familiar with.
    rags_to_riches and antred like this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    255
    Quote Originally Posted by Dontgiveup View Post
    I am writing a double hashing program and I seem to have a problem here. I get always bus error. I have used std:: cout to print out where exactly the problem occurs and it seems to occur at E * newvalues = new T [newtavleLength]; Here is the part of the code. It is the part of the code where I rehash all the values in a new larger table (double old tbaleSize) after the number of values exceed 70% of tableLength.
    Can you post the entire ContDynArray class? If you've corrupted your heap, the cause of the problem may very well be in another part of your code. Also, in addition to using a debugger you should also make liberal use of assertions and make as many of your variables const as possible (general rule: It it can be const it SHOULD be const). It is much easier to reason about a piece of code if you know for sure that most variables will never have their values changed after their initialization. I've gone ahead and slightly rewritten and reformatted your code.

    Code:
    template <typename T>
    void ContDynArray<T>::rehash()
    {
        const size_t newtableLength = tableLength * 2;
    
        const size_t newmaxvalues = 0.7 * newtableLength;
    
        T* const newvalues = new T[ newtableLength ];
    
        T* const newprintvalues = new T[ newmaxvalues ];
    
        bool* const newoccupied = new bool [ newtableLength ];
    
        for ( size_t i = 0; i < newtableLength; ++i )
        {
            newoccupied[ i ] = false;
        }
    
        for ( size_t i = 0; i < tableSize; ++i )
        {
            const unsigned long hashvalue = hashValue( printvalues[ i ] );
            size_t newplace = hashvalue % newtableLength;
    
            assert( newplace < newtableLength );
    
            while ( newoccupied[ newplace ] )
            {
                assert( newplace < newtableLength );
                newplace = collisionfunction( hashvalue, newplace );
            }
    
            assert( newplace < newtableLength );
            newvalues[ newplace ] = printvalues[ i ];
    
            newoccupied[ newplace ] = true;
    
            assert( i < newmaxvalues );
            newprintvalues[ i ] = printvalues[ i ];
        }
    
        delete [] values;
        delete [] occupied;
        delete [] printvalues;
    
        values = newvalues;
        occupied = newoccupied;
        printvalues = newprintvalues;
        tableLength = newtableLength;
    }

    Oh yeah, I almost forgot the most important thing: Have you considered getting rid of those manually managed arrays and replacing them with std::vector? Manual memory management is one of the most error prone things about C++. You should prefer the alternative when ever possible.

    I'm going to go out on a limb here and bet you that you did not implement a proper copy constructor and copy assignment operator for your ContDynArray class (and seeing as your class contains pointers to dynamically allocated memory, it most likely NEEDS those). Am I right?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 11-28-2011, 11:48 AM
  2. Replies: 4
    Last Post: 07-24-2011, 10:38 PM
  3. Replies: 1
    Last Post: 11-15-2010, 11:14 AM
  4. Replies: 3
    Last Post: 10-02-2007, 10:12 PM
  5. Replies: 1
    Last Post: 01-11-2007, 05:22 PM

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