[program_options] Clearing variables_map

Hello, I'm using boost::program_options to parse an command line within a running program (i.e. not just at startup, whenever a new command comes in), and am trying to avoid memory deallocation/reallocation. Is there any way reuse a variable_map that has already had options stored into it? If I use the std::map<>.clear() it seems to clear the stored values, but when I next try to parse, the results are still empty. I think this is happening because the m_final variable (as far as I can understand). Is there some reason why the variables_map can't be cleared and reused? thanks in advance for any insight you can provide, Jason.

Jason Reich wrote:
Hello,
I'm using boost::program_options to parse an command line within a running program (i.e. not just at startup, whenever a new command comes in), and am trying to avoid memory deallocation/reallocation. Is there any way reuse a variable_map that has already had options stored into it?
If I use the std::map<>.clear() it seems to clear the stored values, but when I next try to parse, the results are still empty. I think this is happening because the m_final variable (as far as I can understand).
Is there some reason why the variables_map can't be cleared and reused?
Why do you think that calling clear() and using the same variables_map again will cut down on memory deallocation/reallocation? It does not seem obvious to me that it will. - Volodya

"Vladimir Prus"
Jason Reich wrote: <snip>
Is there some reason why the variables_map can't be cleared and reused?
Why do you think that calling clear() and using the same variables_map again will cut down on memory deallocation/reallocation? It does not seem obvious to me that it will.
- Volodya
Hi Volodya, I know there will be memory allocation behind the scenes, but I think it would simplify the option processing if each processor could reuse an object, rather than having to delete and reallocate one each time a command arrives. Like a regular std::map, the variable_map it can be cleared, but it can't be reused. Also, many of the classes within program_option use private members and methods that limit the ability for the classes to be extended. Is there any reason why they weren't made protected to better allow extension? thanks Jason.

Jason Reich wrote:
"Vladimir Prus"
wrote in message news:hpmi0m$ei2$1@dough.gmane.org... Jason Reich wrote: <snip>
Is there some reason why the variables_map can't be cleared and reused?
Why do you think that calling clear() and using the same variables_map again will cut down on memory deallocation/reallocation? It does not seem obvious to me that it will.
- Volodya
Hi Volodya, I know there will be memory allocation behind the scenes, but I think it would simplify the option processing if each processor could reuse an object, rather than having to delete and reallocate one each time a command arrives. Like a regular std::map, the variable_map it can be cleared, but it can't be reused.
I do not think you will get any benefit, actually, except for saving the time necessary to construct variables_map instance itself -- which is very small. Unlike std::vector, clearing std::map is not required to keep any storage around.
Also, many of the classes within program_option use private members and methods that limit the ability for the classes to be extended. Is there any reason why they weren't made protected to better allow extension?
Presumably, because those methods are not intended to be called by derived classes? Do you have specific examples? - Volodya
participants (2)
-
Jason Reich
-
Vladimir Prus