gpt4 book ai didi

c++ - 电路可满足性求解器中的访问冲突读取位置 NULL

转载 作者:行者123 更新时间:2023-11-30 17:21:12 24 4
gpt4 key购买 nike

我尝试解决从文件中读取电路的电路可满足性问题(以文本可视化工具中呈现的形式 - 某种程度上是动态的)。如果我的电路很小,我的旋转变压器可以顺利工作(小意味着 <16-18 根电线)。如果我有 25-30 根电线,那么有 2^25-30 种可能性,我会遇到访问违规的问题。我尽可能地尝试释放内存。我每次都尝试创建表达式的新指针,但总是发生访问冲突。 Circuit Satisfiability Bug

^ 这怎么可能?

int evalBoolExprForBinaryVector(char *expr, int n, int binaryVector[]){

// create boolean expression from logical expression
char* expression = (char*) malloc(sizeof(char) * strlen(expr) + 1);
strcpy(expression, expr);

for(int binaryVectorCounter=0; binaryVectorCounter<n; binaryVectorCounter++){
char* currentSearchedIdentifier = (char*) malloc(sizeof(char) * 10);
char* index =(char*) malloc(sizeof(char) * 10);
char* valueOfIndex = (char*) malloc(sizeof(char)*2);
strcpy(currentSearchedIdentifier,"v[");
sprintf(index, "%d", binaryVectorCounter);
strcat(currentSearchedIdentifier, index);
strcat(currentSearchedIdentifier, "]");
sprintf(valueOfIndex, "%d", binaryVector[binaryVectorCounter]);
expression = str_replace(expression,currentSearchedIdentifier,valueOfIndex);
free(currentSearchedIdentifier);
free(index);
free(valueOfIndex);
}
// here my expression will be something like
// ( 0 | 1 ) & (!0 | !1) & ...
// evaluate this
return evalBoolExpr(expression);
};

这是我的代码,以便更好地理解。

程序因 strlen.asm 中的此异常而中断:

main_loop:
mov eax,dword ptr [ecx] ; read 4 bytes

预先感谢您的任何想法。

最佳答案

我用c++方式重写了这部分,一切都很顺利(有些延迟,但至少可以成功完成)

void replaceAll(std::string& str, const std::string& from, const std::string& to) {
if(from.empty())
return;
size_t start_pos = 0;
while((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length();
}
}

int evalBoolExprForBinaryVector(char *expr, int n, int binaryVector[]){

std::string expression(expr);

for(int binaryVectorCounter=0; binaryVectorCounter<n; binaryVectorCounter++){
std::string currentSearchedIdentifier, valueOfIndex;
currentSearchedIdentifier = "v[" + std::to_string(binaryVectorCounter) + "]";
valueOfIndex = std::to_string(binaryVector[binaryVectorCounter]);
replaceAll(expression,currentSearchedIdentifier,valueOfIndex);
}

char *cstr = new char[expression.length() + 1];
strcpy(cstr, expression.c_str());

return evalBoolExpr(cstr);
};

关于c++ - 电路可满足性求解器中的访问冲突读取位置 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28364266/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com