Hi all,
I am writing to a map in a while loop and running into some issues.
Here is a pseudo code --
/* Standard headers included from namespace std*/
int main () {
string line;
size_t lines_written = 0;
map<Myobj, stringmymap;
try {
while (input file is being read) {
Myobj *obj;
...(read line)..
/* Construct obj on dynamic store based on the line read (if not
already created, else reuse)*/
itr = mymap.find(obj);
if (itr == mymap.end()) {
mymap.insert(std::make_pair(obj, line));
lines_written += line.length();
} else {
/* Append current record to existing value */
mymap[obj] += line;
lines_written += mymap[obj].length();
}
if (lines_written BIG_VALUE) {
/* Write the map using the obj objects to some files (I get files
sorted by obj) */
...
/* Clear the map */
mymap.clear();
lines_written = 0;
}
} /* while loop */
} catch(...) {
/* Code reaching here */
}
}
I see that my code goes into the exception block after a while when I
execute it for big files with BIG_VALUE set to something like
(1024*1024). I am trying to write the files when maps have grown
sufficiently large. I then try to erase everything for the next
iteration. It works fine for small files.
It seems that map insertions are failing and map.clear is not helping
me here. I am probably running out of memory. Is there any bit that I
am missing or that I could improve?
Thanks ..
P.S. - I feel that I could go the C way and handle memory management
on my own by continually writing to a buffer on stack and later
sorting it based on obj (the not so recommended way).