String Pools: Reusing Memory

Dear all, First, may I blaspheme by invoking Java. In Java, a pool of strings, initially empty, is maintained privately by the class String. This is essentially a case of the Flyweight design pattern (http://en.wikipedia.org/wiki/Flyweight_pattern) where memory usage is minimised by sharing as much data as possible with other similar objects. Thus if we have two different instances of a string "foo" we shall reuse a single area of memory. In Java there is a concept of string internisation: http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#intern%28%29 My question is, what is the best way to implement this in C++? Is the answer to this a combination of std::string and boost::fast_pool_allocator, as discussed here: http://www.gamedev.net/community/forums/topic.asp?topic_id=534428 Or are there better ways? Perhaps there is something like an intern_string? What would be the best practice where memory usage is an issue? Many thanks, Paul

Bilokon, Paul escribió:
Dear all,
First, may I blaspheme by invoking Java. In Java, a pool of strings, initially empty, is maintained privately by the class String. This is essentially a case of the Flyweight design pattern (http://en.wikipedia.org/wiki/Flyweight_pattern) where memory usage is minimised by sharing as much data as possible with other similar objects. Thus if we have two different instances of a string "foo" we shall reuse a single area of memory. In Java there is a concept of string internisation:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#intern%28%29
My question is, what is the best way to implement this in C++? Is the answer to this a combination of std::string and boost::fast_pool_allocator, as discussed here:
http://www.gamedev.net/community/forums/topic.asp?topic_id=534428
Or are there better ways? Perhaps there is something like an intern_string? What would be the best practice where memory usage is an issue?
Boost provides an interpretation of the Flyweight pattern: http://www.boost.org/libs/flyweight You can then use boost::flyweightstd::string directly or a light wrapper around it as your intern string type. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
participants (2)
-
Bilokon, Paul
-
joaquin@tid.es