Ticket #5216: bad_hash.cpp

File bad_hash.cpp, 708 bytes (added by Rob Stewart <robert.stewart@…>, 12 years ago)

Bug demonstration program

Line 
1#include <iostream>
2#include <map>
3#include <vector>
4#include <boost/shared_ptr.hpp>
5#include <boost/functional/hash.hpp>
6
7int
8main()
9{
10 typedef boost::shared_ptr<int> ptr_type;
11 typedef std::map<std::size_t, int> hashes_type;
12 std::vector<ptr_type> v(25);
13 hashes_type hashes;
14 boost::hash<ptr_type> hasher;
15 ptr_type null_ptr;
16 ++hashes[hasher(null_ptr)];
17 for (int i(0); i < 25; ++i)
18 {
19 v[i] = ptr_type(new int(i));
20 std::size_t const hash(hasher(v[i]));
21 ++hashes[hash];
22 }
23 for (hashes_type::iterator it(hashes.begin()), end(hashes.end());
24 it != end; ++it)
25 {
26 std::cout << it->first << ": " << it->second << '\n';
27 }
28}