环境后:视窗7亲64,微软的Visual Studio 2015企业版本14.0.25424.00更新3C++上删除错误[]通过指针迭代
int testFunction()
{
std::string _orig = "[188 80% (1/2)O:152]";
std::string _orig2 = "[999 99% (1/1)O:999]";
char *orig = NULL;
char *orig2 = NULL;
orig = new char[_orig.length() + 1];
strcpy(orig, _orig.c_str());
orig2 = new char[_orig2.length() + 1];
strcpy(orig2, _orig2.c_str());
*orig++;
*orig2++;
int a = atoi(orig);
int b = atoi(orig2);
delete[] orig;
delete[] orig2;
return 0;
}
运行上面代码崩溃与“_CrtIsValidHeapPointer(块)“错误。
如果我不迭代(* orig ++和* orig2 ++),那么没有问题。
所以我的问题是,我怎样才能遍历指针,然后当我做完我需要做的与他们,删除[]他们正确?
为什么要使用'char'指针?整个使用'std :: string'。 –
我很想,但我需要使用atoi(),它不会在标准::字符串上工作 – Zyre
(1)为什么你需要它?有C++等价物。 (2)你* *实际上可以将'std :: string'中的指针传递给'atoi'就好了:'atoi(str.c_str())'起作用。 –