Tug*_*.44 0 c++ access-violation
我无法弄清楚为什么,但这是我的代码:
char expression[256] = {};
cin >> expression;
cout << endl;
int** variableTable = NULL;
int numOfVals = getNumberOfVariables(expression);
variableTable = static_cast<int**>(calloc(numOfVals, sizeof(int*)));
for (int i = 0; i < numOfVals; i++)
{
variableTable[i] = static_cast<int*>(calloc(2, sizeof(int)));
}
fillPromoterTable(expression, variableTable, numOfVals);
Run Code Online (Sandbox Code Playgroud)
这是fillPromoterTable
void fillPromoterTable(const char* expression, int** variableTable, int numOfVals)
{
char promoter[15] = {};
char *token;
char* expCpy = pcstrdup(expression);
for (int i = 0; numOfVals; i++)
{
token = strtok(expCpy, "+-*/");
int nLen = istrlen(token);
for (int j = 0; j < nLen; j++)
{
if (isdigit(token[j]))
promoter[j] = token[j];
if (isalpha(token[j]))
break;
}
variableTable[i][0] = atoi(promoter);
memset(promoter, '\0', 15);
token = strtok(NULL, "+-*/");
}
free(token);
free(expCpy);
}
Run Code Online (Sandbox Code Playgroud)
在这一行:
variableTable[i][0] = atoi(promoter);
Run Code Online (Sandbox Code Playgroud)
我收到一个错误,说我正在尝试写入地址0xFDFDFDFD我无法弄清楚为什么会发生这种情况我可以使用一些帮助.
更改函数fillPromoterTable中的for循环条件
for (int i = 0; numOfVals; i++)
Run Code Online (Sandbox Code Playgroud)
至:
for (int i = 0; i < numOfVals; i++)
Run Code Online (Sandbox Code Playgroud)