가능한 중복 :
When should I use the new keyword in C++?개체 변수의 수명을 관리하는 가장 좋은 방법은 무엇입니까?
나는 전문 프로그래머가 아니에요 내가 경험 작은 프로젝트 작업을, 그래서 여기에 무슨 일이 일어나고 있는지 이해하는 약간의 문제가있어 .
나는 보통 class_name var_name
을 사용하여 개체를 만듭니다. 하지만 이제는 Objective-C를 배우는 중입니다. Objective-C는 거의 모든 것이 포인터이고 메모리 사용에 대한 제어력이 더 큽니다.
이제 무한 루프가 포함 된 응용 프로그램을 만들고 있습니다.
제 질문은 어떤 옵션을 사용하면 메모리 사용을 관리하여 메모리 사용량을 줄일 수 있습니까? new
및
#include <stdio.h>
#include <iostream>
#include <deque>
using namespace std;
class myclass
{
public:
int a;
float b;
deque<int> array;
myclass() {cout <<"myclass constructed\n";}
~myclass() {cout <<"myclass destroyed\n";}
//Other methods
int suma();
int resta();
};
int main(int argc, char** argv)
{
myclass hola;
for(1)
{
myclass *hola;
hola = new myclass;
// Work with object hola.
hola->a = 1;
delete hola;
}
return 0;
}
delete
사용 (나를 위해)
정상 선언
#include <stdio.h> #include <iostream> #include <deque> using namespace std; class myclass { public: int a; float b; deque<int> array; myclass() {cout <<"myclass constructed\n";} ~myclass() {cout <<"myclass destroyed\n";} //Other methods int suma(); int resta(); }; int main(int argc, char** argv) { myclass hola; for(1) { // Work with object hola. hola.a = 1; } return 0; }
나는 옵션 2는 메모리를 적게 사용하고,보다 효율적으로 양단 큐를 해제 생각합니다. 그 맞습니까? 그들 사이의 [다른] 차이점은 무엇입니까?
각 옵션을 사용할 위치가 정말 혼란 스럽습니다.
지금에 개최 :이 질문에 대한
대답은 좋은 읽기입니다. 두 버전은 같은 일을하지 않습니다. 그래서, 정말로 질문은 무엇입니까? –
구문 수정 주셔서 감사. 나는 그것을 올바른 방식으로 넣는 것을 미쳐 가고있다. –
Nitpick on your English : 단어 "친구"는 질문을 의미하지는 않으며, 보통 친구를 의미합니다. :-) –