C++ 有没有一种方法可以;重置";非全局使用的getopt?

C++ 有没有一种方法可以;重置";非全局使用的getopt?,c++,valgrind,getopt,C++,Valgrind,Getopt,尝试多次使用getopt时,我在valgrind中遇到的错误是大小为1的无效读取。只有在执行以下操作时才会发生错误: ls-a-b ls-a-b 因此,我假设问题在于重用getopt函数 命令.h class命令{ 受保护的: //用于getopt int c=0; //命令名 字符*名称; 公众: 命令(const char*nname):名称((char*)nname){} virtual~Command(){}; 虚空进程_args(int argc,char*argv[])=0; 虚拟

尝试多次使用
getopt
时,我在valgrind中遇到的错误是
大小为1的无效读取
。只有在执行以下操作时才会发生错误:

ls-a-b
ls-a-b
因此,我假设问题在于重用
getopt
函数

命令.h

class命令{
受保护的:
//用于getopt
int c=0;
//命令名
字符*名称;
公众:
命令(const char*nname):名称((char*)nname){}
virtual~Command(){};
虚空进程_args(int argc,char*argv[])=0;
虚拟字符*get_name()常量{
返回名称;
}
};
ls.h简单地包装在一个类中:

类ls:公共命令{ 公众: ls():命令(“ls”){} ~ls(){} 无效进程参数(int-input\u-argc,char*input\u-argv[]){ int verbose_标志=0; 结构选项长_选项[]= { /*这些选项设置了一个标志*/ {“verbose”,无参数,&verbose_标志,1}, {“简短”,无参数,&verbose_标志,0}, /*这些选项不设置标志。 我们通过它们的指数来区分它们*/ {“add”,无_参数,0,'a'}, {“append”,无_参数,0,'b'}, {“delete”,必需的参数,0,'d'}, {“create”,必需的_参数,0,'c'}, {“文件”,必需的参数,0,'f'}, {0, 0, 0, 0} }; 而(1){ //移除静态结构并将其移动到外部 //其他一切都一样 } } }; main.cpp

std::vector命令;
commands.push_back(std::unique_ptr(new ls());
命令。push_back(std::unique_ptr(new shibe());
while(true){
std::字符串输入;
std::getline(std::cin,输入);
如果(输入=“退出”)
打破
std::istringstream iss(输入);
std::向量args;
std::copy(std::istream_迭代器(iss),std::istream_迭代器(),std::back_插入器(args));
int input_argc=args.size();
字符*input_argv[input_argc];
对于(int i=0;iget_name())==0){
命令[i]->进程参数(输入参数C、输入参数V);
打破
}
}
}
Valgrind输出为:

ls -a -b
--30624-- REDIR: 0x375e8812d0 (strlen) redirected to 0x480155c (_vgnU_ifunc_wrap                                                                                                 per)
--30624-- REDIR: 0x375e87f810 (__GI_strchr) redirected to 0x4a07b30 (__GI_strchr                                                                                                 )
option -a
option -b
ls -a -b
==30624== Invalid read of size 1
==30624==    at 0x375E8CDFDC: _getopt_internal_r (in /lib64/libc-2.12.so)
==30624==    by 0x375E8CF1EA: _getopt_internal (in /lib64/libc-2.12.so)
==30624==    by 0x375E8CF2D2: getopt_long (in /lib64/libc-2.12.so)
==30624==    by 0x401E1C: ls::process_args(int, char**) (ls.h:31)
==30624==    by 0x4019CB: main (main.cpp:36)
==30624==  Address 0x513e5da is 26 bytes inside a block of size 27 free'd
==30624==    at 0x4A05FD6: operator delete(void*) (vg_replace_malloc.c:480)
==30624==    by 0x4CCADFE: std::basic_string<char, std::char_traits<char>, std::                                                                                                 allocator<char> >::~basic_string() (basic_string.h:538)
==30624==    by 0x403AA5: void std::_Destroy<std::string>(std::string*) (stl_con                                                                                                 struct.h:93)
==30624==    by 0x403855: void std::_Destroy_aux<false>::__destroy<std::string*>                                                                                                 (std::string*, std::string*) (stl_construct.h:103)
==30624==    by 0x403466: void std::_Destroy<std::string*>(std::string*, std::st                                                                                                 ring*) (stl_construct.h:126)
==30624==    by 0x402DE6: void std::_Destroy<std::string*, std::string>(std::str                                                                                                 ing*, std::string*, std::allocator<std::string>&) (stl_construct.h:151)
==30624==    by 0x402878: std::vector<std::string, std::allocator<std::string> >                                                                                                 ::~vector() (stl_vector.h:415)
==30624==    by 0x401A03: main (main.cpp:26)
==30624==
--30624-- REDIR: 0x375e8846b0 (mempcpy) redirected to 0x4a09f80 (mempcpy)
non-option input_argv-elements: s b
quit
--30624-- REDIR: 0x375e87b6d0 (free) redirected to 0x4a06369 (free)
==30624==
==30624== HEAP SUMMARY:
==30624==     in use at exit: 0 bytes in 0 blocks
==30624==   total heap usage: 36 allocs, 36 frees, 916 bytes allocated
==30624==
==30624== All heap blocks were freed -- no leaks are possible
==30624==
==30624== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 6 from 6)
==30624==
ls-a-b
--30624--重读:0x375e8812d0(strlen)已重定向到0x480155c(_vgnU_ifunc_wrap per)
--30624——REDIR:0x375e87f810(_GI_strchr)重定向到0x4a07b30(_GI_strchr)
方案a
方案b
ls-a-b
==30624==大小为1的读取无效
==30624==at 0x375E8CDFDC:_getopt_internal_r(in/lib64/libc-2.12.so)
==30624==0x375E8CF1EA:_getopt_internal(in/lib64/libc-2.12.so)
==30624==by0x375e8cf2d2:getopt_long(in/lib64/libc-2.12.so)
==30624==by 0x401E1C:ls::进程参数(int,char**)(ls.h:31)
==30624==0x4019CB:main(main.cpp:36)
==30624==地址0x513e5da是大小为27 free'd的块中的26字节
==30624==at 0x4A05FD6:运算符删除(void*)(vg\u替换\u malloc.c:480)
==30624==by 0x4CCADFE:std::basic_string::~basic_string()(basic_string.h:538)
==30624==by 0x403AA5:void std::_Destroy(std::string*)(stl_con struct.h:93)
==30624==by 0x403855:void std::_Destroy_aux:_Destroy(std::string*,std::string*)(stl_construct.h:103)
==30624==by 0x403466:void std::_Destroy(std::string*,std::st ring*)(stl_construct.h:126)
==30624==by 0x402DE6:void std:_Destroy(std::str ing*,std::string*,std::allocator&)(stl_construct.h:151)
==30624==0x402878:std::vector::~vector()(stl_vector.h:415)
==30624==0x401A03:main(main.cpp:26)
==30624==
--30624--重拨:0x375e8846b0(mempcpy)重定向到0x4a09f80(mempcpy)
非选项输入_argv-elements:s b
退出
--30624--重拨:0x375e87b6d0(空闲)重定向到0x4a06369(空闲)
==30624==
==30624==堆摘要:
==30624==在出口处使用:0块中的0字节
==30624==总堆使用率:36个alloc,36个free,916个字节分配
==30624==
==30624==所有堆块都已释放--不可能存在泄漏
==30624==
==30624==错误摘要:1个上下文中的1个错误(已抑制:6个上下文中的6个)
==30624==
清楚地说明了如何重置
getopt()
(它不幸地使用了许多全局变量来与调用者通信并维护状态):

变量optind是要在argv中处理的下一个元素的索引。系统将该值初始化为1。调用者可以将其重置为1以重新扫描同一argv,或者在扫描新参数向量时将其重置为1

看起来你只是