04-25-2004, 04:41 PM | #1 (permalink) |
Insane
Location: Rio Grande Valley, Texas
|
[c++] Segmentation Fault with STL vector of lists.
I can't see a problem with this, but obviously my compiler does:
I'm including an external link for niceness: http://www.asl.cs.panam.edu/~babbitt/CSCI3333a4/ Can anyone give me a hand?
__________________
"I can't understand why people are frightened of new ideas. I'm frightened of the old ones." -- John Cage (1912 - 1992) |
04-25-2004, 07:32 PM | #2 (permalink) |
Junkie
Location: San Francisco
|
You have to resize the vector HashTable::table before you use it, or supply a size to its constructor. I'm not sure what vector::reserve does but my guess is it allocates memory but doesn't actually resize. If you're not going to be resizing the vector more than once you don't need to bother with reserve, just change it to resize.
|
04-25-2004, 09:19 PM | #3 (permalink) |
Location: Waterloo, Ontario
|
Next time, strcrssd, please also post the error message. It probably told you what your problem is and, if you couldn't understand it, chances are that one of us could have.
By the way, I just copied your code into my compiler (VC++ 6.0) and it compiled just fine. VC++ 6.0 is not the world's most compliant compiler so... what was your error message, again? Also, there are many improvements to your code that can be made but I'll just name one (other than not using the std namespace!). You might want to typedef your containers. Not only will it save you typing but it will also better abstract your containers. Have you ever wondered why all the STL containers look so similar? It's so that if you were ever to change your mind and decide to use another container, chances are that you can do so and not affect your entire code. However, this will not work if you explicitly declare your type at every insance. So, your class might look like this: Code:
template<class Object>
class HashTable{
 public:
 typedef List std::list<typename Object>; // newer compilers might need the typename keyword, here...

 HashTable(unsigned long tableSize);
 void insert(unsigned long key, Object p);

 List retrieve(unsigned long key);
 bool retrieve(unsigned long key, List* output); // this is more efficient because you won't copy an entire container...

 protected:
 unsigned long hash(unsigned long key);

 private:
 typedef Table std::vector<List>; // no one needs to know what this is so make it private...

 // unsigned long tableSize; you don't really need this because you'll implicitly store the size in the Table container, table.size()
 Table table;
}; Code:
for(int i=0; i<3; i++){
 HashTable::List objList;
 if( ht.retrieve( static_cast<unsigned long>(i), &objList) ){
 for(HashTable::List::iterator i = objList.begin(); i != objList.end(); i++){
 // cout << objList.front().getName() << endl; this is what you used to have
 cout << i->getName() << endl; // this is probably what you wanted...
 }
 }
} This is the essence of maintainability... |
04-25-2004, 10:37 PM | #4 (permalink) | |
Junkie
Location: San Francisco
|
Quote:
|
|
04-25-2004, 11:43 PM | #5 (permalink) |
Location: Waterloo, Ontario
|
Ah, yes--that would make sense. I obviously didn't... read the subject line... or the other link...
Anyway, I think n0nsensical has figured the problem out. You need to call resize() to in order for the constructors of the vector objects to be called. reserve() reallocates memory without calling these constructors... |
Tags |
fault, lists, segmentation, stl, vector |
|
|