struct-ptr new/delete
Wu-Jun Pei@Sprout2018
前情提要
Struct
北區第五週投影片

Struct
struct Person {
char name[64], address[128];
long long int ID, birthday;
int gender;
};
簡單表示一個人(參考身分證)
Pointer
北區上週投影片
int abc = 123;
int *ptr = &abc;
std::cout << " abc = " << abc << std::endl;
std::cout << "&abc = " << &abc << std::endl;
std::cout << " ptr = " << ptr << std::endl;
std::cout << "*ptr = " << *ptr << std::endl;
std::cout << "-----------------------" << std::endl;
(*ptr) = 456;
std::cout << " abc = " << abc << std::endl;
std::cout << "*ptr = " << *ptr << std::endl;
struct-ptr
Struct
struct Person {
char name[64], address[128];
long long int ID, birthday;
int gender;
};
Person people[Population];
身分證的一些資訊
Struct
問題:身分證上還有很多資訊,如父母、配偶
要如何表示?
找你爸的媽的爸的媽的爸的媽的爸的媽的爸?
- 用 Name 表示?
- 用 Address 表示?
- 用 ID 表示?
- 用 Birthday 表示?
Struct
問題:身分證上還有很多資訊,如父母、配偶
要如何表示?
以 ID 為例:
每次查詢 ID 要找到陣列中的位置,二分搜?
新生兒出生,插入更是問題(二分搜需要在排序好的狀況下進行)
Struct
我們正在複雜化問題!
只是找個爸媽而已,很難嗎?
Struct-ptr
I have a struct
I have a pointer
struct-ptr!!!
Struct-ptr
使用「指標」紀錄父母、配偶......
(為了方便,簡化掉其他資訊)
struct Person {
std::string name;
Person *dad, *mom, *spouse;
};
Struct-ptr
使用「指標」紀錄父母、配偶......
Person x, y;
x.name = "Rakan", y.name = "Xayah";
Person *ptrx = &x, *ptry = &y;
(*ptrx).spouse = ptry;
ptry->spouse = ptrx;
std::cout << (*ptrx).name << " <3 " << ptry->name << std::endl;
(*pointer).sth = pointer->sth
記得要加括號 (*ptr)
Practice
Person a, b, c, d;
Person *ptra = &a, *ptrb = &b, *ptrc = &c, *ptrd = &d;
a.name = "AAA", b.name = "BBB", ptrc->name = "CCC", ptrd->name = "DDD";
/* Write down the relations */
std::cout << c.name << "'s dad is " << /* TO FILL */ << std::endl;
std::cout << c.name << "'s mom is " << /* TO FILL */ << std::endl;
std::cout << ptrc->name << "'s spouse is " << /* TO FILL */ << std::endl;
std::cout << ptrd->name << "'s spouse is " << /* TO FILL */ << std::endl;
std::cout << ptrc->name << " is " << ptrc->spouse->spouse->name << std::endl;
Practice
CCC's dad is AAA
CCC's mom is BBB
CCC's spouse is DDD
DDD's spouse is CCC
CCC is CCC
Struct-ptr
Person a, b, c, d;
Person *ptra = &a, *ptrb = &b, *ptrc = &c, *ptrd = &d;
a.name = "AAA", b.name = "BBB", ptrc->name = "CCC", ptrd->name = "DDD";
c.dad = ptra;
c.mom = ptrb;
c.spouse = ptrd;
c.spouse->spouse = ptrc;
std::cout << c.name << "'s dad is " << (*c.dad).name << std::endl;
std::cout << c.name << "'s mom is " << (*(*ptrc).mom).name << std::endl;
std::cout << ptrc->name << "'s spouse is " << c.spouse->name << std::endl;
std::cout << ptrd->name << "'s spouse is " << ptrd->spouse->name << std::endl;
std::cout << ptrc->name << " is " << ptrc->spouse->spouse->name << std::endl;
new/delete
new/delete
每天都有人出生,每天也都有人死掉
要怎麼開好記憶體大小剛好呢?
成為動態配置記憶體大師!
new/delete
Person *ptrx = new Person, ptry = new Person;
ptrx->name = "Rakan", ptry->name = "Xayah";
ptrx->spouse = ptry;
ptry->spouse = ptrx;
new 運算子:配置動態配置記憶體
指標變數=new 資料型別
new/delete
int *x = new int(42);
std::cout << *x << std::endl;
new 運算子:配置動態配置記憶體
指標變數=new 資料型別(初始值)
new/delete
int *fib = new int[100];
new 運算子:動態配置記憶體 - 陣列篇
指標=new 資料型別[大小]
new/delete
delete ptrx;
delete ptry;
delete 運算子:動態刪除記憶體
delete 一個指標
new/delete
delete [] fib;
delete 運算子:動態刪除記憶體 - 陣列篇
delete [] 一個指標
區分
delete fib;
Conclusion
Conclusion
- 在 struct 上使用指標
- (*ptr).sth = ptr->sth
- 使用 new 動態配置記憶體
- 使用 delete 動態刪除記憶體
struct-ptr
By Wu-Jun Pei
struct-ptr
- 299