[英]<Unable to read memory> c++
I am trying to run this code in console app in vs 2013 but when I run the code give following error: 我试图在vs 2013中的控制台应用程序中运行此代码,但是当我运行代码时,出现以下错误:
-argv[1] 0x00000000 <NULL> char *
<unable to read memory>
this is the main function code: 这是主要功能代码:
int main(int argc, char **argv)
{
set_new_handler(memory_err);
if (strcmp(argv[1], "lit") == 0) {
// For Rules
TransPar par;
get_args(par, argc, argv); // get arguments
gen_rules(par); // generate rules (really, just transactions)
}
else if (strcmp(argv[1], "seq") == 0) {
// For Sequences
SeqPar par;
get_args(par, argc, argv); // get arguments
gen_seq(par); // generate sequences
}
else if (strcmp(argv[1], "tax") == 0) {
// For Rules with Taxonomies
TaxPar par;
get_args(par, argc, argv); // get arguments
gen_taxrules(par); // generate rules (really, just transactions)
}
else if (strcmp(argv[1], "-version") == 0) {
print_version();
return 0;
}
else {
cerr << "Synthetic Data Generation, ";
print_version();
cerr << "Usage: " << argv[0] << " lit|tax|seq [options]\n";
cerr << " " << argv[0]
<< " lit|tax|seq -help For more detailed list of options\n";
return 1;
}
return 0;
}
Also I should mentioned the source code was built for Unix but I modified it for running to win. 我还应该提到源代码是为Unix构建的,但为了运行而修改了它。
You need to check if argc >= 2
before using argv[1]
. 您需要在使用argv[1]
之前检查argc >= 2
。 If the user doesn't supply any arguments then you are accessing an argument that doesn't exist. 如果用户不提供任何参数,那么您正在访问的参数不存在。
According to the C and C++ standards, argv[argc]
is guaranteed to be a null pointer , which is what you're seeing here; 根据C和C ++标准, 保证argv[argc]
是一个空指针 ,这就是您在此处看到的内容。 you're passing a null pointer to strcmp()
and this is causing the crash. 您将空指针传递给strcmp()
,这导致崩溃。 (Side note: accessing argv[argc + 1]
is undefined behavior so you need to be especially careful when accessing indices 2+.) (附带说明:访问argv[argc + 1]
是未定义的行为,因此在访问索引2+时需要格外小心。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.