Microsoft PowerPoint - 8ÀÏ°_Æ÷ÀÎÅÍ.ppt

Similar documents
<4D F736F F F696E74202D2036C0CFC2B05FB0B4C3BCC1F6C7E2C7C1B7CEB1D7B7A1B9D62E707074>

Microsoft PowerPoint - 9ÀÏ°_ÂüÁ¶ÀÚ.ppt

Microsoft PowerPoint - 3ÀÏ°_º¯¼ö¿Í »ó¼ö.ppt

쉽게 풀어쓴 C 프로그래밍

슬라이드 1

Microsoft PowerPoint - additional01.ppt [호환 모드]

Microsoft PowerPoint - chap06-2pointer.ppt

1. auto_ptr 다음프로그램의문제점은무엇인가? void func(void) int *p = new int; cout << " 양수입력 : "; cin >> *p; if (*p <= 0) cout << " 양수를입력해야합니다 " << endl; return; 동적할

C++ Programming

설계란 무엇인가?

Microsoft PowerPoint - Chapter 6.ppt

슬라이드 1

1. 객체의생성과대입 int 형변수 : 선언과동시에초기화하는방법 (C++) int a = 3; int a(3); // 기본타입역시클래스와같이처리가능 객체의생성 ( 복습 ) class CPoint private : int x, y; public : CPoint(int a

Microsoft PowerPoint - ch07 - 포인터 pm0415

PowerPoint Presentation

11장 포인터

Microsoft PowerPoint - additional07.ppt [호환 모드]

C++ Programming

Microsoft PowerPoint - [2009] 02.pptx

Microsoft PowerPoint - C++ 5 .pptx

PowerPoint Template

쉽게 풀어쓴 C 프로그래밍

PowerPoint 프레젠테이션

C++ Programming

PowerPoint Presentation

Microsoft PowerPoint - additional08.ppt [호환 모드]

C++ Programming

Microsoft PowerPoint - additional06.ppt [호환 모드]

설계란 무엇인가?

3. 1 포인터란 3. 2 포인터변수의선언과사용 3. 3 다차원포인터변수의선언과사용 3. 4 주소의가감산 3. 5 함수포인터

Microsoft PowerPoint - Chapter 1-rev

(Microsoft Word - \301\337\260\243\260\355\273\347.docx)

[ 마이크로프로세서 1] 2 주차 3 차시. 포인터와구조체 2 주차 3 차시포인터와구조체 학습목표 1. C 언어에서가장어려운포인터와구조체를설명할수있다. 2. Call By Value 와 Call By Reference 를구분할수있다. 학습내용 1 : 함수 (Functi

untitled

(8) getpi() 함수는정적함수이므로 main() 에서호출할수있다. (9) class Circle private double radius; static final double PI= ; // PI 이름으로 로초기화된정적상수 public

080629_CFP °ø°³¿ë.hwp

(Microsoft PowerPoint - 07\300\345.ppt [\310\243\310\257 \270\360\265\345])

목차 포인터의개요 배열과포인터 포인터의구조 실무응용예제 C 2

Microsoft PowerPoint - 03_(C_Programming)_(Korean)_Pointers

PowerPoint Template

Microsoft PowerPoint - chap10-함수의활용.pptx

C프로-3장c03逞풚

4 장클래스와객체 클래스와객체 public과 private 구조체와클래스객체의생성과생성자객체의소멸과소멸자생성자와소멸자의호출순서디폴트생성자와디폴트소멸자멤버초기화멤버함수의외부정의멤버함수의인라인함수선언 C++ 프로그래밍입문

<4D F736F F F696E74202D20C1A63038C0E520C5ACB7A1BDBABFCD20B0B4C3BC4928B0ADC0C729205BC8A3C8AF20B8F0B5E55D>

1. 표준입출력 C++ : C의모든라이브러리를포함 printf, scanf 함수사용가능예 : int, double, 문자열값을입력받고출력하기 #include <cstdio> int ivar; double dvar; char str[20]; printf("int, dou

PowerPoint Template

쉽게 풀어쓴 C 프로그래밍

chap10.PDF

설계란 무엇인가?

class Sale void makelineitem(productspecification* spec, int qty) SalesLineItem* sl = new SalesLineItem(spec, qty); ; 2. 아래의액티비티다이어그램을보고 Java 또는 C ++,

A Dynamic Grid Services Deployment Mechanism for On-Demand Resource Provisioning

<322EBCF8C8AF28BFACBDC0B9AEC1A6292E687770>

Microsoft PowerPoint - 제11장 포인터

Microsoft PowerPoint - 제11장 포인터(강의)

Microsoft PowerPoint - chap03-변수와데이터형.pptx

Microsoft PowerPoint 자바-기본문법(Ch2).pptx

금오공대 컴퓨터공학전공 강의자료

PowerPoint Presentation

Lab 3. 실습문제 (Single linked list)_해답.hwp

JAVA PROGRAMMING 실습 05. 객체의 활용

Microsoft PowerPoint - chap11-포인터의활용.pptx

gnu-lee-oop-kor-lec06-3-chap7

(Microsoft PowerPoint - 11\300\345.ppt [\310\243\310\257 \270\360\265\345])

The C++ Programming Language 5 장포인터, 배열, 구조체 5.9 연습문제 다음의선언문을순서대로작성해보자. 문자에대한포인터, 10개정수의배열, 10개정수의배열의참조자, 문자열의배열에대한포인터, 문자에대한포인터에대한포인터, 상수정수, 상수

PowerPoint Template

C# Programming Guide - Types

프입2-강의노트-C++기초

chap 5: Trees

Programming hwp

PowerPoint 프레젠테이션

제 11 장포인터 유준범 (JUNBEOM YOO) Ver 본강의자료는생능출판사의 PPT 강의자료 를기반으로제작되었습니다.

K&R2 Reference Manual 번역본

KNK_C_05_Pointers_Arrays_structures_summary_v02

Microsoft PowerPoint - lecture2.ppt

윤성우의 열혈 TCP/IP 소켓 프로그래밍

JVM 메모리구조

Microsoft PowerPoint 장강의노트.ppt

JAVA 프로그래밍실습 실습 1) 실습목표 - 메소드개념이해하기 - 매개변수이해하기 - 새메소드만들기 - Math 클래스의기존메소드이용하기 ( ) 문제 - 직사각형모양의땅이있다. 이땅의둘레, 면적과대각

Microsoft PowerPoint - additional03.ppt [호환 모드]

C++ Programming

Microsoft PowerPoint - ch09 - 연결형리스트, Stack, Queue와 응용 pm0100

Microsoft PowerPoint - chap02-C프로그램시작하기.pptx

Microsoft PowerPoint - chap-11.pptx

Microsoft Word - FunctionCall

The C++ Programming Language 4 장타입과선언 4.11 연습문제 Hello,world! 프로그램을실행시킨다. 프로그램이컴파일되지않으면 B3.1 을참고하자. #include<iostream> //#include 문, 헤더파일, 전처리지시

11장 포인터

윤성우의 열혈 TCP/IP 소켓 프로그래밍

chap8.PDF

쉽게 풀어쓴 C 프로그래밍

윤성우의 열혈 TCP/IP 소켓 프로그래밍

임베디드시스템설계강의자료 6 system call 2/2 (2014 년도 1 학기 ) 김영진 아주대학교전자공학과

제 14 장포인터활용 유준범 (JUNBEOM YOO) Ver 본강의자료는생능출판사의 PPT 강의자료 를기반으로제작되었습니다.

C++ Programming

PowerPoint 프레젠테이션

금오공대 컴퓨터공학전공 강의자료

Data Structure

Slide 1

Microsoft PowerPoint - Chap12-OOP.ppt

Microsoft PowerPoint - Chapter 10.ppt

15. 윈도 운영체제

Transcription:

포인터 1

포인터란? 포인터 메모리의주소를가지고있는변수 메모리주소 100번지 101번지 102번지 103번지 int theage (4 byte) 변수의크기에따라 주로 byte 단위 주소연산자 : & 변수의주소를반환 메모리 2

#include <iostream> list 8.1 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; shortvar : 5 Address of shortvar: 0012FF7C longvar : 65535 Address of longvar: 0012FF78 svar: -65535 Address of svar: 0012FF74 3

포인터선언 포인터선언방법 int *page = 0; 정수형변수의주소를담는 ( 가리키는 ) 변수 ( 포인터 ) page 는변수 저장되는내용은주소로해석 Null 과 Wild 포인터 null 포인터 : 0 으로초기화된포인터 wild 포인터 : 초기화되지않은포인터 포인터값할당 int howold = 50 ; int *page = 0 ; page = &howold; int howold = 50 ; int *page = &howold; 4

간접지정 (indirection) 연산자 간접지정연산자 메모리주소 포인터를사용하여실제값접근 (page를사용하여 howold 값에접근 ) int howold = 50 ; int *page = &howold; 100번지 101번지 102번지 int howold (4 byte) 100 page cout << page ; cout << *page ; 주소값이가리키는실제값을찾아가게함 5

구별 포인터변수 포인터, 주소, 변수 포인터변수에저장된주소값 포인터변수에저장된주소값이가리키는메모리에저장된실제값 100번지 int thisval 101번지 102번지 104 번지 int thatval 108 번지 int *pptr int thisval = 5; int thatval = 7; int *pptr = &thisval; int dbl_this = 2 * *pptr; pptr = &thatval; int dbl_that = 2 * *pptr; 6

#include <iostream> list 8.2 typedef unsigned short int USHORT; int main() using std::cout; USHORT myage; // a variable USHORT * 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; 7

포인터를사용하는이유 자유기억장소의자료를다룰때 클래스멤버자료와클래스멤버함수에접근할때 함수에주소전달을할때 8

자유기억공간 (heap) 메모리의 5 가지영역 코드영역 전역공간 : 전역변수관리 스택 : 지역변수, 함수의매개변수관리 자유기억공간 레지스터 : special / general purpose register 지역변수의문제점 지속성 동적으로변수를잡을수없음 int num; std:: cin >> num ; int array[num]; 자유기억공간의이점 공간을제거하기전까지는메모리유지 동적으로변수를잡을수있음포인터로자유기억공간에대한접근및관리포인터를가진함수만이자유기억공간에접근가능 9

자유기억공간할당및해제 할당 ( new ) new 객체의형 ( 초기화값 ) new 객체의형 [ 개수 ] 자유기억장소할당및메모리주소반환 new unsigned short int new long (10000) new int [10] 2byte 자유기억장소할당 4byte 자유기억장소할당 (10000 으로초기화 ) 4byte 자유기억장소 10 개할당 10

자유기억공간할당및해제 (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] ; 11

자유기억공간할당및해제 (cont.) 해제 ( delete ) 자유공간의메모리는 ( 스택에저장된지역변수와달리 ) 자동소멸되지않음 강제적인해제필요 delete 포인터 ; delete ppointer ; 메모리누출 ( memory leak ) int *ppointer = new int ; *ppointer = 72; ppointer = new int ; *ppointer = 84; delete ppointer ; 앞선자유기억공간을해제할방법없음 12

#include <iostream> int main() using std::cout; list 8.4 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; 13

자유기억장소에객체만들기 / 지우기 만들기 Cat *pcat = new Cat ; 지우기 delete pcat ; 리스트 8.5 자료멤버에접근 (*pcat).getage( ) ; pcat GetAge( ) ; 14

#include <iostream> class SimpleCat public: SimpleCat(); ~SimpleCat(); private: int itsage; ; list 8.5 SimpleCat::SimpleCat() std::cout << "Constructor called.\n"; itsage = 1; SimpleCat::~SimpleCat() std::cout << "Destructor called.\n"; 15

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; 16

#include <iostream> list 8.6 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; 17

Class 멤버자료가자유기억공간을가리키는포인터 클래스의생성자 ( 또는메소드 ) 자유기억공간할당 클래스의소멸자 자유기억공간해제 18

#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 19

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; 20

this 포인터 this 포인터 그개체를가리키는포인터 모든클래스멤버함수가가지는숨겨진매개변수 컴파일러가자동적으로생성과삭제해줌 21

#include <iostream.h> class Rectangle public: Rectangle(); ~Rectangle(); list 8.8 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; ; 22

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; 23

실종된포인터 메모리해제 (delete) 후포인터를다시사용 논리적에러발생 메모리해제후가능한포인터를널 (null) 로지정 24

typedef unsigned short int USHORT; #include <iostream> list 8.9 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; 25

const 포인터 const 의오른쪽항을상수로간주 const int *pone ; int * const ptwo ; const int * const pthree ; pone : 상수형 (int) 에대한포인터 int val = 5 ; const int * pone = &val ; val = 7 ; pone *pone = 10 ; (X) ptwo : 상수포인터 int a = 5, b = 7; int * const ptwo = &a ; *ptwo = 10; pone *ptwo = &b ; (X) 26

const 포인터 (cont.) const 객체에대한포인터를선언하면포인터가호출할수있는메소드는 const 메소드뿐 27

#include <iostream> using namespace std; list 8.10 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() 28

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; 29