Initializing a hashtable?

This is a discussion on Initializing a hashtable? within the C++ Programming forums, part of the General Programming Boards category; I was adding data to the hashtable and i have to check for collision. Well, in my strcmp its causing ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221

    Initializing a hashtable?

    I was adding data to the hashtable and i have to check for collision.
    Well, in my strcmp its causing a crash since there is no data
    and its not initialized.(i think) So, here i proceed to try to initialize it.
    When i do it calls the operator= function where i dont know what to do with? whatever i try to do it just crashes. Probobly because s contains no data?

    Here are my headers
    Code:
    class hashmap
    {
    public:
    	hashmap(int capacity);
    	~hashmap(void);
    
    
    	bool get(char const * const symbol, stock& s,
    		int& symbolHash, int& hashIndex, int& usedIndex) const;
    
    
    	bool put(const stock& s, int& symbolHash, int& hashIndex, int& usedIndex);
    
    	bool remove(char const * const symbol, stock &s,
    		int& symbolHash, int& hashIndex, int& usedIndex);
    
    	friend ostream& operator<<(ostream& out, const hashmap& h);
    
    private:
    
    	stock* hashTable;	           
    	size_t Tablesize;
    
    	static int hashStr(char const * const symbol);	// hashing function
    
    };
    Code:
    class stock
    {
    public:
    	stock(char const * const symbol, char const * const name, int sharePrice, date priceDate);
    	// sharePrice is given as a number of CENTS
    	stock(const stock& s);						// copy constructor
    	stock(void);								// default constructor
    	char const * const getSymbol(void) const;
    	stock& operator=(const stock& s);
    	stock& operator=(stock const * const s);
    	~stock(void);
    
    	// display column headers
    	static void displayHeaders(ostream& out);	// display the headers when this instance is printed
    
    	// prints share price as DOLLARS
    	// (e.g. 2483 would print out as 24.83 and 200 would print out as 2.00)
    	friend ostream& operator<<(ostream& out, const stock& s);
    
    	friend class hashmap;
    
    
    private:
    
    	int  sharePrice;
    	char* name;
    	char* symbol;
    	date priceDate;
    	 int size;
    
    
    };
    This is where i am trying to initialize the table
    Code:
    hashmap::hashmap(int capacity): Tablesize(capacity),hashTable( new stock[capacity] )
    
    {
    
    for(int i=0; i<capacity; i++)
    hashTable[i] = NULL;
    
    
    }
    then this function is callled:

    Code:
    stock& stock::operator=(stock const * const s)
    {
    
    
    	name = NULL;
    	symbol = NULL;
    	delete name;
    	delete symbol;
    
    	strcpy(name, s->name); // crashes
    
    
    	return *this;
    }
    I think i need to do a copy here of some sort. however, what ever i do with s is crashes since s contains no data. What do i need to do in that function?

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Why not check if 's' is NULL, and if it is return a NULL pointer without doing anything else, otherwise continue on with the overloading.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You shouldn't be assigning null to each object in the hashmap constructor. Either have the hashTable hold pointers to stock objects, or make sure that a default constructed stock object is valid.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    Quote Originally Posted by mrsirpoopsalot View Post
    Code:
    stock& stock::operator=(stock const * const s)
    {
    	name = NULL;
    	symbol = NULL;
    	delete name;
    	delete symbol;
    
    	strcpy(name, s->name); // crashes
    
    	return *this;
    }
    Are you sure you can't see a problem with executing even any two of these three statements in the order shown?:
    Code:
    	name = NULL;
    	delete name;
    	strcpy(name, s->name);
    If this were building a house it would be roof, then walls, then foundations.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 03:09 AM
  2. Missing Entries in Hashtable
    By wuzzo87 in forum C Programming
    Replies: 3
    Last Post: 05-13-2007, 12:46 AM
  3. Replies: 12
    Last Post: 10-22-2006, 08:37 PM
  4. Initializing and Solving matrices
    By scottmanc in forum C++ Programming
    Replies: 1
    Last Post: 11-16-2003, 03:06 PM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 11:54 AM

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