이 경우 메모리를 올바르게 해제하는 방법은 무엇입니까?이 경우 메모리를 올바르게 해제하는 방법
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;
:
이 주요 기능이다
"조건부 점프 또는 이동은 초기화되지 않은 값 (들)에 따라 달라집니다"Valgrind의가하는 이유를 이해가 안
내가 가진 것을 쓴다 기능 :
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를 사용하십시오. –
"some code here"에 'continue'또는 'break'문이 포함되어 있지 않습니까? 내 요점은 - array_of_numbers_of_circles의 _all_ 요소가 _really_initialized인지 100 % 확실한가요? 디버거를 사용하여이를 확인할 수도 있습니다. –
좀 더 일반적이고 간단한'matrix [i * 3 + j] 대신'* (matrix + i * 3 + j)'를 사용하는 이유는 무엇입니까? – HEKTO