On Sat, 9 Jul 2005, yakoub wrote:

> i'm using microsoft only because my course staff required this .
> and course staff are under budget so i'm asking you people
>
>
> hash_map<int,int> H, S;
>
> for( .. ;... ;... )
> H.insert(....);
>
> ofstream out("tmp",ios::binary);
> out.write((char*)&H,sizeof(H)); <===== write H to file

this is no way to write the hash_map into a file. you're writing the
contents of the object 'H', which probably contains pointers to
dynamically-allocated memory. this does not write the actual elements of
the hash_map into the file.

> out.flush();
> ifstream in("tmp",ios::binary);
> in.read((cahr*)&S,sizeof(H)); <===== read H from file into S

so now you have to objects, 'H' and 'S', containing pointers to the same
data objects. when you delete both objects, you'll have a double-free of
each of these pointers, which will probably cause a crash.

> after this H.size()==S.size() and data are equal
> but destructor of S naturally doesn't work
> please read bellow for further info

you should enumerate all the objects in the hash table (using iterators),
and store each of them seperately into the file (and NOT via your method -
what if one of the objects contains pointers too?). the only think you may
store into a file is a native type (int, char, characters list, etc).

--guy

p.s. this has nothing to do with the operating system or the compiler.
this is about understanding memory management in C++.

--------------------------------------------------------------------------
Haifa Linux Club Mailing List (http://www.haifux.org)
To unsub send an empty message to [EMAIL PROTECTED]


Reply via email to