카테고리 없음

메모리 그려보기

JaeHyunShin 2019. 11. 9. 17:26

q

깊은 복사 얕은복사 정확한 개념 익히기

-Person type 객체,원본)=(pg 30)

 

copy constructor

->얕은복사 깊은 복사 모두 가능

 

c언어는 모두 스택에 저장된다 

java-heap 

 

deep copy하는 법 기억!!

 

stringlength 

string copy함수 쓰기 위해 cstring- import하자

 

-복사생성자 안만ㄷ들어주면 자동으로 하나 생성

-사진 정리

-->CIrcle생성자.메모리 해제 에러 

초반 얕은 복사 생성자-->후반 깊은 복사 생성자 

 

 

#pragma once
using namespace std;
#include 
class Person {

char* name;
int id;
public:
Person(int id, const char* name);
~Person();
void changeName(const char* name);
void show(){
cout << id << ',' << name << endl;
}
};

Person::Person(int id, const char* name) {
this->id = id;
int len = strlen(name);
this->name=new char[len + 1];
//공백문자 위해
}
Person::~Person() {
//if(name)
//동적 메모리할당이 안되는 경우
delete[] this->name;
}
void Person::changeName(const char* name) {

if (strlen(name) <= strlen(this->name))
strcpy(this->name, name);
}
int main() {
Person father(1,"kitae");
Person duaghter(father);
//생성자 바로 호출 안하고 father를 복사할 예정
cout<<"daughter 이름 변경 후"<<
/에러가 난 이유가 중요 디ㅍㄹ트 복사 생성자가 
//name을 father에서 daughter로 얕은 복사했기에 '
같은 힙의 메모리 공간을 딸과 아빠가 가르키니 
//delete할때 메모리 

return 0;
}

g함수내부 복사생성자?