使用提升对字符串进行标记化时,将令牌转换为 char* const* 时失败
tokenizing string with boost fails when casting tokens to char* const*
我正在使用boost::tokenizer
在C++中标记字符串,然后我想将其传递给execv
。
请考虑以下代码片段(可编译(:
#include <iostream>
#include <cstdlib>
#include <vector>
#include <boost/tokenizer.hpp>
// I will put every token into this vector
std::vector<const char*> argc;
// this is the command I want to parse
std::string command = "/bin/ls -la -R";
void test_tokenizer() {
// tokenizer is needed because arguments can be in quotes
boost::tokenizer<boost::escaped_list_separator<char> > scriptArguments(
command,
boost::escaped_list_separator<char>("\", " ", """));
boost::tokenizer<boost::escaped_list_separator<char> >::iterator argument;
for(argument = scriptArguments.begin();
argument!=scriptArguments.end();
++argument) {
argc.push_back(argument->c_str());
std::cout << argument->c_str() << std::endl;
}
argc.push_back(NULL);
}
void test_raw() {
argc.push_back("/bin/ls");
argc.push_back("-l");
argc.push_back("-R");
argc.push_back(NULL);
}
int main() {
// this works OK
/*test_raw();
execv(argc[0], (char* const*)&argc[0]);
std::cerr << "execv failed";
_exit(1);
*/
// this is not working
test_tokenizer();
execv(argc[0], (char* const*)&argc[0]);
std::cerr << "execv failed";
_exit(2);
}
当我运行这个脚本时,它会调用test_tokenizer()
,它会打印"execv 失败"。(尽管它很好地打印了参数(。
但是,如果我将test_tokenizer
更改为test_raw
它运行良好。
这一定是一些简单的解决方案,但我没有找到。
PS.:我也把它放到一个在线编译器中,支持提升。
boost::tokenizer
按值(默认情况下为std::string
(将令牌保存在令牌迭代器中。
因此,当迭代器被修改时,argument->c_str()
指向的字符数组可能会被修改或失效,并且其生存期最迟将以argument
的生存期结束。
因此,当您尝试使用argc
时,您的程序具有未定义的行为。
如果你想继续使用boost::tokenizer
,我建议将令牌保存在一个std::vector<std::string>
中,然后将它们转换为指针数组。
相关文章:
- LINUX 操作系统上的错误:令牌之前预期的构造函数、析构函数或类型转换'('?
- 错误 c++ 在令牌之前'('预期的构造函数、析构函数或类型转换
- 错误:令牌之前的预期构造函数、析构函数或类型转换'*'
- C++模板错误:'<'令牌之前的预期构造函数、析构函数或类型转换
- 错误:'<'令牌之前的预期构造函数、析构函数、类型转换
- 当捕获boost::bad_exical_cast时,我可以访问要强制转换的字符串/令牌吗
- 如何避免在转换字符串令牌流时重复istringstream构造
- 令牌之前的预期构造函数、析构函数或类型转换'*'
- C++ 错误:令牌之前的预期构造函数、析构函数或类型转换'*'
- C++错误:"令牌之前的预期构造函数、析构函数或类型转换'<'
- 函数定义中'*'令牌之前的预期构造函数、析构函数或类型转换
- C++令牌之前'('预期的构造函数、析构函数或类型转换
- 错误:')'令牌强制转换问题之前期望的主表达式
- 错误:在源文件中的用户定义类'&'令牌之前,预期的构造函数、析构函数或类型转换
- 错误:令牌之前'('预期的构造函数、析构函数或类型转换?
- 令牌之前的预期构造函数、析构函数或类型转换'('
- 令牌之前的预期构造函数、析构函数或类型转换'<<'
- C++ 错误:在"("令牌之前进行预期的构造函数、析构函数或类型转换
- 生成令牌并转换为c++代码
- 我收到一个错误:重载运算符= 时'&'令牌之前的预期构造函数、析构函数或类型转换=