sparsehash file io

5 views
Skip to first unread message

a.anand

unread,
Sep 17, 2009, 11:59:38 AM9/17/09
to google-sparsehash
hi.. i am using sparsehash api of reading and writing hashtables to
files (using read_metadata and read_nopointer_data). for reading, i
create a new hashtable and read from file into this hashtable. as i
clear the items in the new hashtable, i get sometimes error like
double pointer corruption, or terminate called after throwing an
instance of std::length error. I am using std::string as key, any clue
why these errors are coming up?

thanks
ashok

Craig Silverstein

unread,
Sep 17, 2009, 12:01:35 PM9/17/09
to google-s...@googlegroups.com
} i get sometimes error like double pointer corruption, or terminate
} called after throwing an instance of std::length error. I am using
} std::string as key, any clue why these errors are coming up?

You will have to use placement new to initialize the strings. Can you
give a code sample of what you're doing now?

craig

Ashok Anand

unread,
Sep 17, 2009, 1:06:25 PM9/17/09
to google-s...@googlegroups.com
here is the sample..

#define key_t std::string

// loc_t is a structure with 3 unsigned integer fields
sparse_hash_map<key_t, loc_t> myMap;
// for writing to file pointed by pointer fp
myMap.write_metadata(fp);
myMap.write_nopointer_data(fp);
// for reading from file pointed by fp
sparse_hash_map<key_t, loc_t> rMap;
rMap.read_metadata(fp);
rMap.read_nopointer_data(fp);
// after reading and lookup is done
rMap.clear();

is it not the right way? please let me know

thanks
ashok

Craig Silverstein

unread,
Sep 17, 2009, 1:37:53 PM9/17/09
to Ashok Anand, google-s...@googlegroups.com
} here is the sample..
}
} #define key_t std::string
}
} // loc_t is a structure with 3 unsigned integer fields
} sparse_hash_map<key_t, loc_t> myMap;
} // for writing to file pointed by pointer fp
} myMap.write_metadata(fp);
} myMap.write_nopointer_data(fp);
} // for reading from file pointed by fp
} sparse_hash_map<key_t, loc_t> rMap;
} rMap.read_metadata(fp);
} rMap.read_nopointer_data(fp);
} // after reading and lookup is done
} rMap.clear();

Ah, I see. That won't work at all because string has pointers in it.
read/write_nopointer is intended for Plain Old Data types. You will
need to do something more complicated. Look at the documentation; it
has an example of how to use read/write IO with non-trivial key/value
types -- I forget the exact example, but it may even be for a string.

craig

TopBoy

unread,
Sep 23, 2009, 9:48:14 PM9/23/09
to google-sparsehash
I use the char* as the key, so it does not work either?

Craig Silverstein

unread,
Sep 24, 2009, 3:03:02 PM9/24/09
to google-s...@googlegroups.com
} I use the char* as the key, so it does not work either?

No. char* definitely doesn't fall into the category of "nopointer
data".

craig

Reply all
Reply to author
Forward
0 new messages