[Click] how not to use HashMaps

Eddie Kohler kohler at icir.org
Fri Oct 10 09:31:09 EDT 2003


This Grid bug caused explosions:

mission% cvs diff -r1.21 elements/grid/sr/srcr.hh
Index: elements/grid/sr/srcr.hh
===================================================================
RCS file: /home/am0/click/cvsroot/release/one/elements/grid/sr/srcr.hh,v
retrieving revision 1.21
retrieving revision 1.22
diff -u -r1.21 -r1.22
--- elements/grid/sr/srcr.hh    10 Oct 2003 04:40:24 -0000      1.21
+++ elements/grid/sr/srcr.hh    10 Oct 2003 15:23:39 -0000      1.22
@@ -120,8 +120,8 @@
     typedef HashMap<IPAddress, int> IPCount;
     IPCount _errors_sent;

-    BadNeighbor() {memset(this, 0, sizeof(*this)); }
-    BadNeighbor(IPAddress ip) {memset(this, 0, sizeof(*this)); _ip = ip;}
+    BadNeighbor() : _ip(), _when(), _timeout() { }
+    BadNeighbor(IPAddress ip) : _ip(ip), _when(), _timeout() { }
     bool still_bad() {
       struct timeval expire;
       struct timeval now;

Upshot: Please don't use memset() on objects that have constructors! It
worked with the old HashMap (because freshly initialized HashMaps had
all-zero members), but not the new HashMap.

XOXO
Eddie


More information about the click mailing list