如何在这种情况下正确释放内存?如何在这种情况下正确释放内存
我不明白为什么Valgrind的写道:我有:
“条件跳转或移动依赖于未初始化值(S)”
这是主要功能:
int n=0;
cin >> n;
float* matrix;
matrix = new float [ n * 3 ];
for(int i = 0; i < n; i++) {
for(int j = 0; j < 3; j++) {
cin >> *(matrix + i * 3 + j);
}
}
int* array_of_numbers_of_circles = findIntersection(matrix,n);
for(int i = 0; i < n; i++) {
for(int j = 0; j < 2; j++) {
if(*(array_of_numbers_of_circles + i * 2 + j) != 0) { //it writes error in if;
cout << *(array_of_numbers_of_circles + i * 2 + j) << " ";
}
}
if(*(array_of_numbers_of_circles + i * 2 + 0) != 0 &&
*(array_of_numbers_of_circles + i * 2 + 1) != 0) { //it writes error in if here too;
cout << "\n";
}
}
delete[] matrix;
delete[] array_of_numbers_of_circles;
和功能:
int* findIntersection(float matrix[], int n) {
//some variables
int* array_of_numbers_of_circles;
array_of_numbers_of_circles = new int [ n * 2 ];
for(int i = 0; i < n; i++) {
for(int j = i + 1; j < n; j++) {
//some code here
*(array_of_numbers_of_circles + i * 2 + 0) = i + 1;
*(array_of_numbers_of_circles + i * 2 + 1) = j + 1;
}
}
return array_of_numbers_of_circles;
}
有什么问题?我不明白为什么VALGRIND会说这样的错误
先谢谢您了!
使用std :: vector自动删除内存并检查缓冲区溢出。 –
你确定,这里的“一些代码”不包含一些'continue'或'break'语句吗?我的观点是 - 是否100%确定,'array_of_numbers_of_circles'的_all_元素是_really_initialized?你也可以用调试器来检查它。 –
为什么使用'*(矩阵+我* 3 + j)'而不是更常见和简单的'矩阵[i * 3 + j]'? – HEKTO