-1
내 코드가 컴파일되지만 "디버그 어설 션 실패"라는 오류 메시지가 계속 표시되는 이유를 알 수 없습니다. 왜 이런거야?디버그 어설 션 실패 (동적 유형 변수)
delete p2;
여기서 문제는 "P1 = P2"로 설정, 그래서 모두 포인터는 메모리 위치를 가리키는 것입니다 :
#include <iostream>
using namespace std;
struct Bag
{
int k;
};
int main()
{
int *p1;
int *p2;
char p3;
//k = 100; // Assigns variable of type bag to 100
p1 = new int; // Variables created using the new operator are called dynamic variables
p2 = new int;
*p1 = 30;
*p2 = 50;
p3 = 'K';
*p1 = *p1 + *p2;
p1 = p2;
cout << "The sum of the two pointers is = " << *p1 << endl;
delete p1;
delete p2; // Delete the dynamic variable p1 and return the memory occupied by p1 to the freestore to be reused.
system ("Pause");
return 0;
}
또한 원래 p2가 가리키는 메모리가 누수됩니다. –