Christian Christmann <[EMAIL PROTECTED]> writes:

> class HashTable 
> { 
>     map<int, void*> nhash; 
>     map<int, void*> getNhash(); 

Consider using a typedef next time.


> ... 
> } 
> ---------- 
>
> My hashtable.cpp: 
>
> map<int, void*> HashTable::getNhash() 
> { 
>   return nhash; 
> } 
>
> void HashTable::Insert(int ky,void* entr) 
> { 
>   nhash.insert(pair<int, void*>(ky, entr)); 
> } 
> ... 
>
> ---------- 
>
> In another class I use this hashtable: 
>
> HashTable lNodes; 
> const std::map<int, void*>::const_iterator begin = lNodes.getNhash().begin(); 
> const std::map<int, void*>::const_iterator end = lNodes.getNhash().end(); 

getNhash() returns its result by value. begin and end are thus
unrelated; in particular, end is not reachable from begin.


> std::map<int, void*>::const_iterator iter = begin; 
>
> cout << "Size: " << lNodes.getNhash().size() << endl; 
>     while (iter != end) 
>     { 
>       cout << "Key: " << iter->first << endl; 
>       ++iter; 
>     } 
>
>
>
> The output: 
>
> Size: 3 
> Key: 0x80a2de0 

Bad luck. With more luck, the iteration would have continued until
iter->first would have attempted to access inaccessible memory, which
would have caused the program to crash.

If you change getNhash()'s return value to map<int, void*> &, you
should get the results you expect.
_______________________________________________
Help-gplusplus mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/help-gplusplus

Reply via email to