C, C++

c++ 메모리 동적할당 초기화, 메모리 누수

Jcon 2022. 8. 16. 15:17
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
 
using namespace std;
 
class Circle
{
public:
    Circle() {}
    Circle(int a) {}
};
 
int main()
{
    int *= new int(10); //동적생성 초기화
    cout << *p; // 10을 출력한다.
    //int *p = new int[10](5); -> 배열은 안된다. 
 
    Circle *= new Circle(30); //Circle(30) 생성자 호출
    delete q;
 
    //※메모리 누수 
    int *= new int;
    int b;
    a = &b; //a가 b를 가리키게 되어 4바이트 메모리가 누수된다.
}
cs