PROJECTENGRAM

[REV] CCPROG2 COMPREHENSIVE FINALS REVIEWER

Tags: #◇Computer Science #◇CCPROG2 #◇C Programming #◇Pointers #◇Reviewer #◇Finals


CCPROG2 Comprehensive Finals Reviewer

◇Overview

This reviewer consolidates Advanced C Programming (CCPROG2). Core topics include pointer arithmetic and dereferencing, multi-dimensional arrays, strings and string library functions, user-defined structures and unions, dynamic memory allocation (malloc, calloc, realloc, free), file handling (fopen, fread, fwrite, fprintf, fscanf), and recursion.


◇1. Pointers & Memory Architecture

Pointer Operators & Equivalences

  • Address-of &x: Returns the physical memory address of variable x.
  • Dereference *p: Accesses the value stored at memory address stored in p.
  • Array-Pointer Identity: For array arr, arr[i] \equiv *(arr + i) \equiv *(i + arr) \equiv i[arr].
  • Pointer Arithmetic: If p is T*, p + 1 increments address by sizeof(T) bytes!

◇2. Dynamic Memory Management Functions

Function PrototypeDescriptionReturn on Failure
void* malloc(size_t size);Allocates uninitialized heap memory of size bytes.NULL
void* calloc(size_t num, size_t size);Allocates and zero-initializes num * size bytes.NULL
void* realloc(void *ptr, size_t new_size);Resizes an allocated block; preserves existing content.NULL (original block remains valid)
void free(void *ptr);Releases allocated heap memory back to the OS.void (Passing NULL is a safe no-op)

[!WARNING] Common Memory Vulnerabilities

  • Dangling Pointer: A pointer that points to deallocated memory after free(p). (Fix: free(p); p = NULL;).
  • Memory Leak: Allocated memory on heap lost when all pointers to it go out of scope without calling free().
  • Double Free: Calling free() twice on the same pointer causes heap corruption / crashes.

◇3. Structures & File I/O Summary

Structure Access

  • Given struct Student s; →\to Dot operator: s.id.
  • Given struct Student *p = &s; →\to Arrow operator: p->id (equivalent to (*p).id).

File Streams

  • Opening: FILE *fp = fopen("data.bin", "rb"); (Modes: "r", "w", "a", "rb", "wb"). Always check if (fp == NULL).
  • Text I/O: fprintf(fp, ...), fscanf(fp, ...), fgets(buffer, size, fp).
  • Binary I/O:
    • size_t fwrite(const void *ptr, size_t size, size_t count, FILE *stream);
    • size_t fread(void *ptr, size_t size, size_t count, FILE *stream);
  • Closing: fclose(fp); flushes buffers and releases file descriptor.

◇4. High-Yield Exam Checklist

  • Does realloc overwrite the original pointer immediately? Check if void *temp = realloc(...) to prevent leak on failure!
  • In char str[] = "Hello";, what is sizeof(str)? 66 bytes (includes null terminator \0).
  • What is strlen("Hello")? 55 (excludes \0).
  • In struct, does member ordering affect memory footprint? YES (Structure padding and alignment).
  • Did you check feof(fp) before or after reading? Check after read operation!

◇References & Navigation

CONFIRMATION // 理解度の確認

ACTIVE RECALL VERIFICATION

Select an answer to verify conceptual comprehension and reveal the technical rationale.

Q1

Given int arr[5] = {10, 20, 30, 40, 50}; int *ptr = arr + 2;, what is the value of *(ptr - 1)?

Q2

Which memory allocation function initializes all allocated bytes to zero?

Q3

Given the declaration typedef struct { char name[20]; int age; } Person; Person p, *ptr = &p;, which expression is valid?

Q4

What is the return value of fread(buf, sizeof(int), 10, fp) if the file only has 4 integers remaining?

Q5

What happens when calling free(p) on a pointer p that is NULL?

[REV] CCPROG2 Comprehensive Finals Reviewer | PROJECT ENGRAM