0

For some reason, the value of the first element of the bucket array is being initialized to the size of the array, and growing in size after function calls.

Header file:

#ifndef TABLE
#define TABLE

#include <iostream>
#include <cstdlib>

typedef struct {
    double successful , unsuccessful[2] ;
} Perform ;

using namespace std ;

template <class DATA>
class Table {
private :
    DATA bucket[];
    bool passbit[];
    bool isFull[];
    unsigned tableSize;
    unsigned currentSize;
    unsigned sProbes;
    unsigned pbCount;

public :
    explicit Table ( unsigned size = 5 ) ;
    void clear ( ) ;
    //bool insert ( DATA & data ) ;
    bool insertD ( DATA & data ) ;
    //bool fetch ( DATA & data ) const ;
    bool getData ( unsigned i , DATA & data , bool & apassbit ) const ;
    //Perform perform ( ) const ;
};

#endif

I'm only using the constructor and one function for now, but I will include everything.

#include <typeinfo>
#include <queue>
#include <iostream>

#include "table.hpp"

using namespace std ;

template <class DATA>
Table<DATA> :: Table ( unsigned size ) 
{
    tableSize = size;
    currentSize = 0;
    sProbes = 0;
    pbCount = 0;
    bucket[tableSize];
    cout << bucket[0] << "\n";
    passbit[tableSize];
    isFull[tableSize];
}

template <class DATA>
void Table<DATA> :: clear()
{
    currentSize = 0;
    sProbes = 0;
    pbCount = 0;
    for (int i = 0; i < tableSize; i++)
    {
        bucket[i] = 0;
        passbit[i] = false;
        isFull[i] = false;
    }
}

template <class DATA>
bool Table<DATA> :: insertD ( DATA & data )
{
    cout << bucket[0] << "\n";
    unsigned value = unsigned ( data );
    unsigned index = value % tableSize;
    unsigned step = value % (tableSize - 2) + 1;
    while ( isFull[index] && !(bucket[index] == data) )
    {
        passbit[index] = true;
        pbCount++;
        index -= step;
        if ( index < 0 )
            index += tableSize;
    }
    if ( isFull[index] && (bucket[index] == data) )
        return false;
    else
    {
        bucket[index] = data;
        isFull[index] = true;
        currentSize++;
        return true;
    }
}

template <class DATA>
bool Table<DATA> :: getData ( unsigned i , DATA & data , bool & apassbit ) const
{
    if( bucket[i] != 0 )
    {
        data = bucket[i];
        apassbit = passbit[i];
        return true;
    }
    else
        return false;
}

Main function:

#include <iostream>
#include "table.cpp"
#include "table.hpp"

int main()
{
    Table<int> table = Table<int>();
    int i = 1;
    int ii = 2;
    int iii = 3;
    int iv = 4;
    int v = 5;
    table.insertD(i);
    table.insertD(ii);
    table.insertD(iii);
    table.insertD(iv);
    table.insertD(v);
    return 0;
}

Output:

5
5
261
65797
16843013
Segmentation fault

1
  • What are you doing in the lines bucket[tableSize]; in the constructor? You cannot resize an array that way, you have to use dynamic memory allocation. Your code just returns the value at buffer[tableSize] (just rubbish) and does nothing with it. Commented May 12, 2011 at 1:03

3 Answers 3

1

The problem is that you are trying to initialize statically allocated arrays. You need to either use dynamically allocated arrays or declare your statically allocated arrays big enough to hold all the data you will need. Of course, since you are using c++, you should consider using STL vectors.

Here's how you will need to adjust your code:

class SomeClass
{
    private:
        double* someArray;
        int     someArraySize;

    public:
        SomeClass( int arraySize )
        {
            someArray = new double[arraySize];
        }

        ~SomeClass()
        {
            delete [] someArray;
        }
};

This uses dynamic allocation. Notice that you need to delete the array when a class instance is destructed.

Sign up to request clarification or add additional context in comments.

Comments

1

You are not currently allocating space for your arrays. You need to use the new operator in your constructor:

bucket = new DATA[tableSize];

etc.

1 Comment

it's now giving me a compiler error: "table.cpp:16:3 error: incompatible types in assignment of 'int*' to 'int [0]', and 2 more like it except with bool* and bool [0]
0

A declaration T obj doesn't do what you think it does. It looks like an array that you can give an arbitrary size to later, but it's not. It's a T* obj.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.