|
other라는 포인터에 대한 메모리는 할당이 되지 않습니다.
person이라는 struct에 대한 메모리를 동적할당하면
포인터에 대한 메모리만 잡히는 것입니다.
ex)
struct person *a, *b, *c;
a = new struct person;
b = new struct person;
c = new struct person;
a->face = 10;
b->face = 20;
c->face = 30;
cout << a->other->face << endl; // <-- 메모리 참조 에러가 생깁니다.
b->other = c;
cout << b->other->face << endl; // 30이 출력이 됩니다.
c->other = new struct person;
c->other->face = 40;
cout << c->other->face << endl; // 40이 출력됨.
참고하세요.
문성훈 님이 쓰신 글 :
: 안녕하세요?
:
: struct person
: {
: int face;
: int arm;
: int body;
: person *other;
: }
:
: 이 있다고 치고, 동적할당을 하면
:
: 메모리에 int 3개 person형 포인터 1개
:
: 가 확보되잖아요?
:
: 그럼 person->other->face
:
: 이렇게도 쓸 수 있는 것 같던데...
:
: 그럼 person->other->face 는 언제 메모리가
:
: 확보된 건가요? 그럼...
:
|