Upload
others
View
2
Download
0
Embed Size (px)
Citation preview
1
8일째포인터
포인터
2
8일째포인터
포인터란?
포인터
메모리의주소를가지고있는변수
주소연산자 : &변수의주소를반환
메모리주소
메모리
주로 byte 단위
변수의크기에따라
101번지102번지
100번지int theAge (4 byte)
103번지
3
8일째포인터
#include <iostream>
int main(){
using namespace std;unsigned short shortVar=5;unsigned long longVar=65535;long sVar = -65535;
cout << "shortVar:\t" << shortVar;cout << "\tAddress of shortVar:\t";cout << &shortVar << "\n";
cout << "longVar:\t" << longVar;cout << "\tAddress of longVar:\t" ;cout << &longVar << "\n";
cout << "sVar:\t\t" << sVar;cout << "\tAddress of sVar:\t" ;cout << &sVar << "\n";
return 0;}
list 8.1
shortVar : 5 Address of shortVar: 0012FF7ClongVar : 65535 Address of longVar: 0012FF78sVar: -65535 Address of sVar: 0012FF74
4
8일째포인터
포인터선언
포인터선언방법
pAge는변수저장되는내용은주소로해석
Null 과Wild 포인터null 포인터 : 0으로초기화된포인터wild 포인터 : 초기화되지않은포인터
포인터값할당
int *pAge = 0;
정수형변수의주소를담는(가리키는) 변수(포인터)
int howOld = 50 ;int *pAge = 0 ;pAge = &howOld;
int howOld = 50 ;int *pAge = &howOld;
5
8일째포인터
간접지정(indirection) 연산자
간접지정연산자
포인터를사용하여실제값접근
(pAge를사용하여 howOld값에접근)
int howOld = 50 ;int *pAge = &howOld;
메모리주소
101번지102번지
100번지int howOld (4 byte)
pAge100
cout << pAge ;
cout << *pAge ;
주소값이가리키는실제값을찾아가게함
6
8일째포인터
포인터, 주소, 변수구별
포인터변수
포인터변수에저장된주소값
포인터 변수에 저장된 주소값이 가리키는메모리에저장된실제값
101번지102번지
100번지 int thisVal
int thatVal
int *pPtr
104번지
108번지
int thisVal = 5;int thatVal = 7;int *pPtr = &thisVal;…int dbl_this = 2 * *pPtr;…pPtr = &thatVal;…int dbl_that = 2 * *pPtr;
7
8일째포인터
#include <iostream>
typedef unsigned short int USHORT;int main(){
using std::cout;USHORT myAge; // a variableUSHORT * pAge = 0; // a pointer
myAge = 5;cout << "myAge: " << myAge << "\n";pAge = &myAge; cout << "*pAge: " << *pAge << "\n\n";
cout << "Setting *pAge = 7...\n";*pAge = 7; cout << "*pAge: " << *pAge << "\n";cout << "myAge: " << myAge << "\n\n";
cout << "Setting myAge = 9...\n";myAge = 9;cout << "myAge: " << myAge << "\n";cout << "*pAge: " << *pAge << "\n";
return 0;}
list 8.2
8
8일째포인터
포인터를사용하는이유
자유기억장소의자료를다룰때
클래스 멤버 자료와 클래스 멤버 함수에 접근할때
함수에주소전달을할때
9
8일째포인터
자유기억공간 (heap)
메모리의 5가지영역코드영역
전역공간 : 전역변수관리스택 : 지역변수, 함수의매개변수관리자유기억공간
레지스터 : special / general purpose register지역변수의문제점
지속성
동적으로변수를잡을수없음
자유기억공간의이점
공간을제거하기전까지는메모리유지
동적으로변수를잡을수있음
포인터로자유기억공간에대한접근및관리
포인터를가진함수만이자유기억공간에접근가능
int num;std:: cin >> num ;int array[num];
10
8일째포인터
자유기억공간할당및해제
할당 ( new )
new 객체의형 (초기화값)new 객체의형 [개수]
자유기억장소할당및메모리주소반환
new unsigned short int
new long (10000)
2byte 자유기억장소할당
4byte 자유기억장소할당(10000으로초기화)
new int [10] 4byte 자유기억장소 10 개할당
11
8일째포인터
자유기억공간할당및해제 (cont.)
자유기억공간사용 ( 포인터로관리)
unsigned short int *pPointer ;pPointer = new unsigned short int ;
*pPointer = 72;std::cout << *pPointer ;
short int *pPointer = new short int ;
*pPointer = 72;std::cout << *pPointer ;
int num ;std::cin >> num ;int *array = new int[num] ;
12
8일째포인터
자유기억공간할당및해제 (cont.)
해제 ( delete )자유공간의메모리는 (스택에저장된지역변수와달리) 자동소멸되지않음강제적인해제필요
메모리누출 ( memory leak )
delete 포인터 ;
delete pPointer ;
int *pPointer = new int ;*pPointer = 72;
pPointer = new int ;*pPointer = 84;
delete pPointer ;
앞선자유기억공간을해제할방법없음
13
8일째포인터
#include <iostream>int main(){
using std::cout;
int localVariable = 5;int * pLocal = &localVariable;int * pHeap = new int;
*pHeap = 7;cout << "localVariable: " << localVariable << "\n";cout << "*pLocal: " << *pLocal << "\n";cout << "*pHeap: " << *pHeap << "\n";delete pHeap;
pHeap = new int;*pHeap = 9;cout << "*pHeap: " << *pHeap << "\n";delete pHeap;
return 0;}
list 8.4
14
8일째포인터
자유기억장소에객체만들기/지우기
만들기
지우기
리스트 8.5
자료멤버에접근
Cat *pCat = new Cat ;
delete pCat ;
(*pCat).GetAge( ) ;
pCat GetAge( ) ;
15
8일째포인터
#include <iostream>
class SimpleCat{public:
SimpleCat();~SimpleCat();
private:int itsAge;
};
SimpleCat::SimpleCat(){
std::cout << "Constructor called.\n";itsAge = 1;
}
SimpleCat::~SimpleCat(){
std::cout << "Destructor called.\n";}
list 8.5
16
8일째포인터
int main(){
std::cout << "SimpleCat Frisky...\n";SimpleCat Frisky;std::cout << "SimpleCat *pRags = new SimpleCat...\n";SimpleCat * pRags = new SimpleCat;std::cout << "delete pRags...\n";delete pRags;std::cout << "Exiting, watch Frisky go...\n";return 0;
}
17
8일째포인터
#include <iostream>
class SimpleCat{public:
SimpleCat() {itsAge = 2; }~SimpleCat() { }int GetAge() const { return itsAge; }void SetAge(int age) { itsAge = age; }
private:int itsAge;
};
int main(){
SimpleCat * Frisky = new SimpleCat;std::cout << "Frisky is " << Frisky->GetAge()
<< " years old\n";Frisky->SetAge(5);std::cout << "Frisky is " << Frisky->GetAge()
<< " years old\n";delete Frisky;return 0;
}
list 8.6
18
8일째포인터
Class 멤버자료가자유기억공간을가리키는포인터
클래스의생성자 ( 또는메소드 )자유기억공간할당
클래스의소멸자
자유기억공간해제
19
8일째포인터
#include <iostream>
class SimpleCat{public:
SimpleCat();~SimpleCat();int GetAge() const { return *itsAge; }void SetAge(int age) { *itsAge = age; }
int GetWeight() const { return *itsWeight; }void setWeight (int weight) { *itsWeight = weight; }
private:int * itsAge;int * itsWeight;
};
SimpleCat::SimpleCat(){
itsAge = new int(2);itsWeight = new int(5);
}
list 8.7
20
8일째포인터
SimpleCat::~SimpleCat(){
delete itsAge;delete itsWeight;
}
int main(){
SimpleCat *Frisky = new SimpleCat;std::cout << "Frisky is " << Frisky->GetAge()
<< " years old\n";Frisky->SetAge(5);std::cout << "Frisky is " << Frisky->GetAge()
<< " years old\n";delete Frisky;return 0;
}
21
8일째포인터
this 포인터
this 포인터그개체를가리키는포인터
모든클래스멤버함수가가지는숨겨진매개변수
컴파일러가자동적으로생성과삭제해줌
22
8일째포인터
#include <iostream.h>
class Rectangle{public:
Rectangle();~Rectangle();
void SetLength(int length) { this->itsLength = length; }
int GetLength() const { return this->itsLength; }
void SetWidth(int width) { itsWidth = width; }
int GetWidth() const { return itsWidth; }
private:int itsLength;int itsWidth;
};
list 8.8
23
8일째포인터
Rectangle::Rectangle(){
itsWidth = 5;itsLength = 10;
}Rectangle::~Rectangle(){ }
int main(){
Rectangle theRect;cout << "theRect is " << theRect.GetLength()
<< " feet long.\n";cout << "theRect is " << theRect.GetWidth()
<< " feet wide.\n";theRect.SetLength(20);theRect.SetWidth(10);cout << "theRect is " << theRect.GetLength()
<< " feet long.\n";cout << "theRect is " << theRect.GetWidth()
<< " feet wide.\n";return 0;
}
24
8일째포인터
실종된포인터
메모리해제 (delete) 후포인터를다시사용논리적에러발생
메모리해제후가능한포인터를널(null)로지정
25
8일째포인터
typedef unsigned short int USHORT;#include <iostream>
int main(){
USHORT * pInt = new USHORT;*pInt = 10;std::cout << "*pInt: " << *pInt << std::endl;delete pInt;
long * pLong = new long;*pLong = 90000;std::cout << "*pLong: " << *pLong << std::endl;
*pInt = 20; // deleted!
std::cout << "*pInt: " << *pInt << std::endl;std::cout << "*pLong: " << *pLong << std::endl;delete pLong;return 0;
}
list 8.9
26
8일째포인터
const 포인터
const의오른쪽항을상수로간주
pOne : 상수형(int)에대한포인터
pTwo : 상수포인터
const int *pOne ;int * const pTwo ;const int * const pThree ;
pOne (X)*pOne = 10 ;
int val = 5 ;const int * pOne = &val ;val = 7 ;
pOne
int a = 5, b = 7;int * const pTwo = &a ;*pTwo = 10;
(X)*pTwo = &b ;
27
8일째포인터
const 포인터 (cont.)
const 객체에대한포인터를선언하면포인터가호출할수있는메소드는 const 메소드뿐
28
8일째포인터
#include <iostream>using namespace std;
class Rectangle{public:
Rectangle();~Rectangle();void SetLength(int length) { itsLength = length; }int GetLength() const { return itsLength; }void SetWidth(int width) { itsWidth = width; }int GetWidth() const { return itsWidth; }
private:int itsLength;int itsWidth;
};
Rectangle::Rectangle(){
itsWidth = 5;itsLength = 10;
}
Rectangle::~Rectangle(){}
list 8.10
29
8일째포인터
int main(){
Rectangle* pRect = new Rectangle;const Rectangle * pConstRect = new Rectangle;Rectangle * const pConstPtr = new Rectangle;
cout << "pRect width: " << pRect->GetWidth() << " feet\n";
cout << "pConstRect width: " << pConstRect->GetWidth() << " feet\n";
cout << "pConstPtr width: " << pConstPtr->GetWidth() << " feet\n";
pRect->SetWidth(10);// pConstRect->SetWidth(10);pConstPtr->SetWidth(10);
cout << "pRect width: " << pRect->GetWidth() << " feet\n";
cout << "pConstRect width: " << pConstRect->GetWidth() << " feet\n";
cout << "pConstPtr width: " << pConstPtr->GetWidth() << " feet\n";
return 0;}