Bài giảng Kỹ thuật lập trình - Chương 3: Các kỹ thuật xây dựng chương trình phần mềm (Phần 2) - Vũ Thị Hương Giang

Phẩm chất của 1 chương trình tốt

– Cấu trúc tốt

– Logic chương trình + các biểu thức được diễn đạt theo cách thông thường

– Tên dùng trong phương trình có tính chất miêu tả – Chú thích hợp lý

Tôn trọng chiến lược divide/conquer/association

Làm thế nào để tạo ra chương trình có phẩm chất tốt

- Thiết kế top-down

– Tinh chỉnh từng bước

pdf 135 trang xuanthi 27/12/2022 1520
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Kỹ thuật lập trình - Chương 3: Các kỹ thuật xây dựng chương trình phần mềm (Phần 2) - Vũ Thị Hương Giang", để tải tài liệu gốc về máy hãy click vào nút Download ở trên.

File đính kèm:

  • pdfbai_giang_ky_thuat_lap_trinh_chuong_3_cac_ky_thuat_xay_dung.pdf

Nội dung text: Bài giảng Kỹ thuật lập trình - Chương 3: Các kỹ thuật xây dựng chương trình phần mềm (Phần 2) - Vũ Thị Hương Giang

  1. 3. Thiết kế dữ liệu • Bài toán: cho các bộ dữ liệu mẫu như sau: • Cần thiết kế cấu trúc – (tên sinh viên, điểm) dữ liệu cho phép thực • (“john smith”, 84) hiện các thao tác sau: • (“jane doe”, 93) • (“bill clinton”, 81) – Create: Tạo mới các bộ • dữ liệu – (tên cầu thủ, vị trí chơi trên sân) – Add: Thêm mới các dữ • (“Ruth”, 3) liệu thành phần • (“Gehrig”, 4) • (“Mantle”, 7) – Search: Tìm kiếm các • dữ liệu thành phần – (tên biến, giá trị) • (“maxLength”, 2000) – Free: Hủy cấu trúc dữ • (“i”, 7) liệu • (“j”, -10) • –
  2. Data Structure #1: Linked List • Data structure: Các nút, mỗi nút chứa cặp key/value và con trỏ trỏ đến nút tiếp theo "Mantle" "Gehrig" "Ruth" 7 4 3 NULL • Algorithms: – Create: Cấp phát nút giả trỏ đến nút thật đầu tiên – Add: Tạo nút mới, thêm vào đầu danh sách – Search: Tìm kiếm tuyến tính – Free: Giải phóng các nút trong khi duyệt, giải phóng các nút giả
  3. Linked List: Create (1) struct Table *Table_create(void) { struct Table *t; t = (struct Table*) malloc(sizeof(struct Table)); t->first = NULL; return t; } struct Table *t; t = Table_create(); t STACK HEAP
  4. Linked List: Create (3) struct Table *Table_create(void) { struct Table *t; t = (struct Table*) malloc(sizeof(struct Table)); t->first = NULL; return t; } struct Table *t; t = Table_create(); NULL t t STACK HEAP
  5. Linked List: Add (1) void Table_add(struct Table *t, const char *key, int value) { struct Node *p = (struct Node*)malloc(sizeof(struct Node)); p->key = key; p->value = value; p->next = t->first; t->first = p; } struct Table *t; Các con trỏ trỏ đến Table_add(t, "Ruth", 3); các xâu nằm trong Table_add(t, "Gehrig", 4); Table_add(t, "Mantle", 7); vùng RODATA "Gehrig" "Ruth" 4 3 t NULL STACK HEAP
  6. Linked List: Add (3) void Table_add(struct Table *t, const char *key, int value) { struct Node *p = (struct Node*)malloc(sizeof(struct Node)); p->key = key; p->value = value; p->next = t->first; t->first = p; } struct Table *t; Table_add(t, "Ruth", 3); Table_add(t, "Gehrig", 4); "Mantle" Table_add(t, "Mantle", 7); p 7 value 7 key "Mantle" "Gehrig" "Ruth" t 4 3 t NULL STACK HEAP
  7. Linked List: Add (5) void Table_add(struct Table *t, const char *key, int value) { struct Node *p = (struct Node*)malloc(sizeof(struct Node)); p->key = key; p->value = value; p->next = t->first; t->first = p; } struct Table *t; Table_add(t, "Ruth", 3); Table_add(t, "Gehrig", 4); "Mantle" Table_add(t, "Mantle", 7); p 7 value 7 key "Mantle" "Gehrig" "Ruth" t 4 3 t NULL STACK HEAP
  8. Linked List: Search (1) int Table_search(struct Table *t, const char *key, int *value) { struct Node *p; for (p = t->first; p != NULL; p = p->next) if (strcmp(p->key, key) == 0) { *value = p->value; return 1; } struct Table *t; return 0; int value; } int found; found = Table_search(t, "Gehrig", &value); found "Mantle" "Gehrig" "Ruth" value 7 4 3 t NULL STACK HEAP
  9. Linked List: Search (3) int Table_search(struct Table *t, const char *key, int *value) { struct Node *p; for (p = t->first; p != NULL; p = p->next) if (strcmp(p->key, key) == 0) { *value = p->value; return 1; } struct Table *t; return 0; int value; } int found; found = Table_search(t, "Gehrig", &value); p value key "Gehrig" t found "Mantle" "Gehrig" "Ruth" value 7 4 3 t NULL STACK HEAP
  10. Linked List: Search (5) int Table_search(struct Table *t, const char *key, int *value) { struct Node *p; for (p = t->first; p != NULL; p = p->next) if (strcmp(p->key, key) == 0) { *value = p->value; return 1; } struct Table *t; return 0; int value; } int found; found = Table_search(t, "Gehrig", &value); found 1 "Mantle" "Gehrig" "Ruth" value 4 7 4 3 t NULL STACK HEAP
  11. Linked List: Free (2) void Table_free(struct Table *t) { struct Node *p; struct Node *nextp; for (p = t->first; p != NULL; p = nextp) { nextp = p->next; free(p); } free(t); /* Free the dummy node */ } struct Table *t; Table_free(t); "Mantle" "Gehrig" "Ruth" t 7 4 3 t NULL STACK HEAP
  12. Linked List: Free (4) void Table_free(struct Table *t) { struct Node *p; struct Node *nextp; for (p = t->first; p != NULL; p = nextp) { nextp = p->next; free(p); } free(t); /* Free the dummy node */ } struct Table *t; Table_free(t); nextp p "Mantle" "Gehrig" "Ruth" t 7 4 3 t NULL STACK HEAP
  13. Linked List: Free (6) void Table_free(struct Table *t) { struct Node *p; struct Node *nextp; for (p = t->first; p != NULL; p = nextp) { nextp = p->next; free(p); } free(t); /* Free the dummy node */ } struct Table *t; Table_free(t); nextp p "Gehrig" "Ruth" t 4 3 t NULL STACK HEAP
  14. Linked List: Free (8) void Table_free(struct Table *t) { struct Node *p; struct Node *nextp; for (p = t->first; p != NULL; p = nextp) { nextp = p->next; free(p); } free(t); /* Free the dummy node */ } struct Table *t; Table_free(t); nextp NULL p NULL t t STACK HEAP
  15. Hiệu năng của chương trình khi cài đặt CTDL bằng Linked List • Phân tích độ phức tạp về thời gian: – Create: O(1), nhanh – Add: O(1), nhanh – Search: O(n), chậm – Free: O(n), chậm • Giải pháp khác: luôn giữ các nút được sắp xếp theo thứ tự tăng/giảm dần của khóa – Create: O(1), nhanh – Add: O(n), chậm; cần duyệt danh sách trước khi tìm được chỗ để thêm – Search: O(n), vẫn chậm; cần duyệt một phần danh sách – Free: O(n), chậm
  16. Băm khóa kiểu string thành giá trị kiểu int • Hàm băm đơn giản sẽ không đưa ra được nhiều khóa phân biệt – Số các ký tự trong xâu % ARRAYSIZE – Tổng các giá trị ASCII của các ký tự trong xâu % ARRAYSIZE – • Hàm băm hợp lý – Tổng các giá trị có tính đến trọng số của các ký tự trong xâu: xi giá trị ASCII của ký tự, a trọng số của ký tự, i vị trí trong xâu i • ( a xi) mod ARRAYSIZE – Trường hợp tốt nhất: a và ARRAYSIZE đều là số nguyên tố • E.g., a = 65599, ARRAYSIZE = 1024
  17. Ví dụ về bảng băm Cho ARRAYSIZE = 7 Tìm kiếm và thêm vào nếu không tìm thấy các xâu sau: the, cat, in, the, hat Bảng băm ban đầu là rỗng Từ 1: hash(“the”) = 965156977. 965156977 % 7 = 1. Tìm xâu “the” trong DS móc nối table[1] : không tìm thấy 0 1 2 3 4 5 6
  18. Ví dụ về bảng băm Từ 2: “cat”. hash(“cat”) = 3895848756. 3895848756 % 7 = 2. Tìm trong DS móc nối table[2] xâu “cat”: không tìm thấy Now: table[2] = makelink(key, value, table[2]) 0 1 the 2 3 4 5 6
  19. Ví dụ về bảng băm Từ 4: “the”. hash(“the”) = 965156977. 965156977 % 7 = 1. Tìm xâu “the” trong DS móc nối table[1] ; có thấy 0 1 the 2 3 cat 4 5 in 6
  20. Ví dụ về bảng băm Thêm vào đầu danh sách dễ hơn 0 1 the 2 3 hat cat 4 5 in 6
  21. Bảng băm: Create (1) struct Table *Table_create(void) { struct Table *t; t = (struct Table*)calloc(1, sizeof(struct Table)); return t; } struct Table *t; t = Table_create(); t STACK HEAP
  22. Hash Table: Create (3) struct Table *Table_create(void) { struct Table *t; t = (struct Table*)calloc(1, sizeof(struct Table)); return t; } struct Table *t; t = Table_create(); 0 NULL 1 NULL t 1023 NULL t STACK HEAP
  23. Bảng băm: Add (1) void Table_add(struct Table *t, const char *key, int value) { struct Node *p = (struct Node*)malloc(sizeof(struct Node)); int h = hash(key); p->key = key; p->value = value; struct Table *t; p->next = t->array[h]; t->array[h] = p; Table_add(t, "Ruth", 3); } Table_add(t, "Gehrig", 4); Table_add(t, "Mantle", 7); Con trỏ trỏ đến các NULL xâu trong vùng 0 "Ruth" 1 NULL nhớ RODATA 3 NULL 23 "Gehrig" 4 723 NULL NULL 806 t 1023 NULL STACK HEAP
  24. Bảng băm: Add (3) void Table_add(struct Table *t, const char *key, int value) { struct Node *p = (struct Node*)malloc(sizeof(struct Node)); int h = hash(key); p->key = key; p->value = value; struct Table *t; p->next = t->array[h]; t->array[h] = p; Table_add(t, "Ruth", 3); } Table_add(t, "Gehrig", 4); Table_add(t, "Mantle", 7); 0 NULL "Ruth" NULL 1 3 806 NULL h 23 "Gehrig" p 4 723 value 7 NULL "Mantle" "Mantle" key 806 NULL 7 t t 1023 NULL STACK HEAP
  25. Bảng băm: Add (5) void Table_add(struct Table *t, const char *key, int value) { struct Node *p = (struct Node*)malloc(sizeof(struct Node)); int h = hash(key); p->key = key; p->value = value; struct Table *t; p->next = t->array[h]; t->array[h] = p; Table_add(t, "Ruth", 3); } Table_add(t, "Gehrig", 4); Table_add(t, "Mantle", 7); 0 NULL "Ruth" NULL 1 3 806 NULL h 23 "Gehrig" p 4 723 value 7 NULL "Mantle" key "Mantle" 7 806 t NULL t 1023 NULL STACK HEAP
  26. Bảng băm: Search (1) int Table_search(struct Table *t, const char *key, int *value) { struct Node *p; int h = hash(key); for (p = t->array[h]; p != NULL; p = p->next) if (strcmp(p->key, key) == 0) { struct Table *t; *value = p->value; int value; return 1; int found; } return 0; found = } Table_search(t, "Gehrig", &value); 0 NULL NULL 1 "Ruth" 3 23 NULL "Gehrig" 4 723 NULL "Mantle" found 806 value 7 t 1023 NULL NULL STACK HEAP
  27. Bảng băm: Search (3) int Table_search(struct Table *t, const char *key, int *value) { struct Node *p; int h = hash(key); for (p = t->array[h]; p != NULL; p = p->next) if (strcmp(p->key, key) == 0) { struct Table *t; *value = p->value; int value; return 1; int found; } return 0; found = } Table_search(t, "Gehrig", &value); 0 NULL 723 h 1 NULL p "Ruth" 3 value 23 key "Gehrig" NULL "Gehrig" 4 t 723 NULL "Mantle" found 806 value 7 t 1023 NULL NULL STACK HEAP
  28. Bảng băm: Search (5) int Table_search(struct Table *t, const char *key, int *value) { struct Node *p; int h = hash(key); for (p = t->array[h]; p != NULL; p = p->next) if (strcmp(p->key, key) == 0) { struct Table *t; *value = p->value; int value; return 1; int found; } return 0; found = } Table_search(t, "Gehrig", &value); 0 NULL 723 h 1 NULL p "Ruth" 3 value 23 key "Gehrig" NULL "Gehrig" 4 t 723 NULL "Mantle" found 806 value 4 7 t 1023 NULL NULL STACK HEAP
  29. Bảng băm: Free (1) void Table_free(struct Table *t) { struct Node *p; struct Node *nextp; int b; for (b = 0; b array[b]; p != NULL; p = nextp) { nextp = p->next; free(p); struct Table *t; } free(t); Table_free(t); } 0 NULL NULL 1 "Ruth" 3 23 NULL "Gehrig" 4 723 NULL "Mantle" 806 7 t 1023 NULL NULL STACK HEAP
  30. Bảng băm: Free (3) void Table_free(struct Table *t) { struct Node *p; struct Node *nextp; int b; for (b = 0; b array[b]; p != NULL; p = nextp) { nextp = p->next; free(p); struct Table *t; } free(t); Table_free(t); } 0 NULL NULL 1 "Ruth" 3 23 b NULL "Gehrig" 4 nextp 723 NULL "Mantle" p 806 t 7 t 1023 NULL NULL STACK HEAP
  31. Bảng băm: Free (5) void Table_free(struct Table *t) { struct Node *p; struct Node *nextp; int b; for (b = 0; b array[b]; p != NULL; p = nextp) { nextp = p->next; free(p); struct Table *t; } free(t); Table_free(t); } b 1024 nextp p t t STACK HEAP
  32. Bảng băm: hiệu năng • Create: O(1), nhanh • Add: O(1), nhanh • Search: O(1), nhanh trong trường hợp kích thước nhỏ • Free: O(n), chậm
  33. Key Ownership (cont.) • Problem: Xét đoạn mã sau Sau khi gọi Table_add(), bảng chứa struct Table t; char k[100] = "Ruth"; địa chỉ ô nhớ k LTV thay đổi xâu tại địa chỉ ô nhớ k Table_add(t, k, 3); Rồi LTV thay đổi khóa trong bảng strcpy(k, "Gehrig"); – Vấn đề trong bảng băm • Khóa của nút hiện tại đổi từ “Ruth” thành “Gehrig” • Nút hiện có đặt sai chỗ • Bảng băm bị hỏng!!! – Vấn đề này có thể xảy ra đối với các cấu trúc dữ liệu khác
  34. Tóm tắt • Các cấu trúc dữ liệu phổ biến và các giải thuật liên quan – Linked list • Unsorted => thêm mới nhanh, tìm kiếm chậm • Sorted => thêm mới chậm, tìm kiếm chậm – Hash table • Thêm mới nhanh, tìm kiếm nhanh nếu hàm băm hoạt động tốt • Khó đánh giá việc lưu trữ các cặp khóa/giá trị • Rất phổ biến • Các chủ để liên quan: – Giải thuật băm – Quản lý bộ nhớ • Khác – #1: tiểu xảo tạo ra các hàm băm nhanh hơn – #2: ví dụ về cấu trúc dữ liệu khác
  35. Recall: Bitwise Operators in C • Bitwise AND (&) • Bitwise OR (|) & 0 1 | 0 1 0 0 0 0 0 1 1 0 1 1 1 1 – Thực hiện phép chia lấy dư, nhưng ít tốn kém hơn • E.g., h = 53 & 15; Cách khác 53 0 0 1 1 0 1 0 1 Đảo 0 thành 1, 1 thành 0 E.g., đặt 3 bits cuối thành 0 & 15 0 0 0 0 1 1 1 1 x = x & ~7; 5 0 0 0 0 0 1 0 1
  36. Tăng tốc việc so sánh các khóa • Cho mọi hàm so sánh giá trị không tầm thường • Trick: lưu trữ toàn bộ giá trị băm trong CTDL int Table_search(struct Table *t, const char *key, int *value) { struct Node *p; int h = hash(key); /* No % in hash function */ for (p = t->array[h%1024]; p != NULL; p = p->next) if ((p->hash == h) && strcmp(p->key, key) == 0) { *value = p->value; return 1; } return 0; }
  37. Expanding Array • Ý tưởng • Cấu trúc dữ liệu: mảng có thể mở rộng kích thước khi cần • Giải thuật Create: cấp phát mảng lưu trữ các cặp khóa/giá trị, ban đầu có rất ít phần tử • Giải thuật Add: Nếu hết chỗ để lưu trữ thêm phần tử mới, thì tăng gấp đôi kích thước mảng và đặt cặp khóa/giá trị cần thêm vào vào trong phần tử đầu tiên chưa sử dụng – Để tăng tính hiệu quả, không nên mở rộng mảng theo kiểu tuyến tính • Giải thuật Search: tìm kiếm tuyến tính • Giải thuật Free: giải phóng mảng
  38. Expanding Array: Create (1) struct Table *Table_create(void) { struct Table *t; t = (struct Table*) malloc(sizeof(struct Table)); t->pairCount = 0; t->arraySize = INITIAL_SIZE; t->array = (struct Pair*) calloc(INITIAL_SIZE, sizeof(struct Pair)); return t; } { struct Table *t; t t = Table_create(); STACK HEAP }
  39. Expanding Array: Create (3) struct Table *Table_create(void) { struct Table *t; t = (struct Table*) malloc(sizeof(struct Table)); t->pairCount = 0; t->arraySize = INITIAL_SIZE; t->array = (struct Pair*) calloc(INITIAL_SIZE, sizeof(struct Pair)); return t; } 0 { 2 struct Table *t; t t t = Table_create(); STACK HEAP }
  40. Expanding Array: Create (5) struct Table *Table_create(void) { struct Table *t; t = (struct Table*) malloc(sizeof(struct Table)); t->pairCount = 0; t->arraySize = INITIAL_SIZE; t->array = (struct Pair*) calloc(INITIAL_SIZE, sizeof(struct Pair)); return t; } 0 { 2 struct Table *t; t t = Table_create(); STACK HEAP }
  41. Expanding Array: Add (2) void Table_add(struct Table *t, const char *key, int value) { /* Expand if necessary. */ if (t->pairCount == t->arraySize) { t->arraySize *= GROWTH_FACTOR; This is a pointer to a t->array = (struct Pair*)realloc(t->array, string that exists in t->arraySize * sizeof(struct Pair)); the RODATA section } t->array[t->pairCount].key = key; t->array[t->pairCount].value = value; t->pairCount++; } "Ruth" 3 value 7 2 struct Table *t; key "Mantle" 2 "Gehrig" t 4 Table_add(t, "Ruth", 3); t Table_add(t, "Gehrig", 4); Table_add(t, "Mantle", 7); STACK HEAP
  42. Expanding Array: Add (4) void Table_add(struct Table *t, "Ruth" const char *key, int value) { 3 /* Expand if necessary. */ if (t->pairCount == t->arraySize) { "Gehrig" t->arraySize *= GROWTH_FACTOR; t->array = (struct Pair*)realloc(t->array, 4 t->arraySize * sizeof(struct Pair)); "Mantle" } t->array[t->pairCount].key = key; 7 t->array[t->pairCount].value = value; t->pairCount++; } value 7 3 struct Table *t; key "Mantle" 4 t Table_add(t, "Ruth", 3); t Table_add(t, "Gehrig", 4); Table_add(t, "Mantle", 7); STACK HEAP
  43. Expanding Array: Search (1) int Table_search(struct Table *t, const char *key, int *value) { int i; for (i = 0; i pairCount; i++) { struct Pair p = t->array[i]; if (strcmp(p.key, key) == 0) { *value = p.value; "Ruth" return 1; } 3 } "Gehrig" return 0; } 4 "Mantle" 7 struct Table *t; 3 int value; 4 int found; found found = value Table_search(t, "Gehrig", &value); t STACK HEAP
  44. Expanding Array: Search (3) int Table_search(struct Table *t, const char *key, int *value) { int i; for (i = 0; i pairCount; i++) { struct Pair p = t->array[i]; if (strcmp(p.key, key) == 0) { *value = p.value; "Ruth" return 1; } 3 } "Gehrig" return 0; } 4 p "Mantle" i 1 7 struct Table *t; value key "Gehrig" 3 int value; 4 int found; t found found = value Table_search(t, "Gehrig", &value); t STACK HEAP
  45. Expanding Array: Search (5) int Table_search(struct Table *t, const char *key, int *value) { int i; for (i = 0; i pairCount; i++) { struct Pair p = t->array[i]; if (strcmp(p.key, key) == 0) { *value = p.value; "Ruth" return 1; } 3 } "Gehrig" return 0; } 4 "Mantle" 7 struct Table *t; 3 int value; 4 int found; found 1 found = value 4 Table_search(t, "Gehrig", &value); t STACK HEAP
  46. Expanding Array: Free (2) void Table_free(struct Table *t) { free(t->array); free(t); } "Ruth" 3 "Gehrig" 4 "Mantle" struct Table *t; 7 3 Table_free(t); 4 t t STACK HEAP
  47. Expanding Array: Free (4) void Table_free(struct Table *t) { free(t->array); free(t); } struct Table *t; Table_free(t); t t STACK HEAP
  48. Expanding Array: hiệu năng • Thời gian tính toán – Create: O(1), nhanh – Add: O(1), nhanh – Search: O(n), chậm – Free: O(1), nhanh • Giải pháp khác: Sắp xếp mảng theo khóa – Create: O(1), nhanh – Add: O(n), chậm; cần phải dịch 1 cặp khóa/ giá trị để có chỗ thêm mới – Search: O(log n), chấp nhận được, có thể sử dụng tìm kiếm nhị phân – Free: O(1), nhanh
  49. 5.1. Quản lý bộ nhớđ ộng trong C Trong thư viện stdlib.h malloc(size_t n), calloc(size_t m, size_t n) . Cấp phát một vùng nhớm ới. realloc(void * ptr, size_t n) . Thay đổi kích thước cho một vùng nhớđ ãđ ược cấp phát free(void * ptr) . Giải phóng cho vùng nhớđ ãđ ược cấp phát size_t làki ểu dữ liệu chứa kích thước đối tượng (sốbyte) size_t sizeof(doi_tuong): Kích thước của doi_tuong Ví dụ: sizeof(int) 2
  50. #include #include void main() { int i, kthuoc; int *a; double *dPtr = malloc(sizeof(double)); printf("Hay nhap vao kich thuoc cua mang: "); scanf("%d", &kthuoc); a = (int *) calloc(kthuoc, sizeof(int)); if (a == NULL || dPtr == NULL ) //Khong du bo nho printf("Khong du bo nho"); else // Da duoc cap phat du { *dPtr = -8.67; for (i=0;i<kthuoc;i++) a[i] = i; a[kthuoc-1] = *dPtr; for (i=0; i<kthuoc; i++) printf("%d", a[i]); } getch(); }
  51. 5.1.2. Thay đổi và giải phóng bộ nhớ void free(void * ptr); Giải phóng vùng nhớđ ộng bắt đầu từđ ịa chỉptr Nếu ptr làcon trỏnull thìl ời gọi hàm làvô nghĩa. void *realloc(void * ptr, size_t n); Giải phóng vùng nhớt ừđ ịa chỉptr vàc ấp phát một vùng nhớ mới n byte và trảv ềđ ịa chỉ của vùng nhớđ ó Vùng nhớm ới cóth ểb ắt đầu giống địa chỉ của vùng nhớ cũ. Giữnguyên nội dung của vùng nhớ cũ cho tới kích thước của vùng nhớ nào nhỏhơn . Nếu vùng nhớ mới không bắt đầu từ địa chỉ của vùng nhớ cũ thì hàm realloc( ) sao chép nội dung vùng nhớ cũ sang vùng nhơ mới. . Nếu vùng nhớ mới lớn hơn vùng nhớ cũ thì giá trị của các byte dư ra không được xác định. Nếu ptr làcon trỏnull thì hàm này giống như hàm malloc()
  52. #include #include #include void main() { char dong[80], *tbao; puts(“Nhap vao mot dong: "); gets(dong); tbao = realloc(NULL, strlen(dong)+1); strcpy(tbao, dong); puts(tbao); puts(“Nhap vao mot dong khac: "); gets(dong); tbao = realloc(tbao,(strlen(tbao) + strlen(dong)+1)); strcat(tbao, dong); puts(tbao); getch(); }
  53. Ví dụ- Quản lý bộ nhớ động trong C++ #include int main() { int i,n; long total=100,x,*ptr; cout > n; ptr = new long [n]; if (ptr==NULL) exit(1); for (i=0;i > ptr[i] } cout << “Danh sach cac so : \n” for (i=0;i<n;i++) cout << ptr[i] << “,”; delete [] ptr; return 0; }