๐ŸŒ CS50x: Introduction to Computer Science ยท subject

CS50x: Introduction to Computer Science Memory Syllabus

The Memory syllabus for CS50x: Introduction to Computer Science in full: 4 chapters and 12 topics, every one listed below, plus 50 Memory flashcards with answers.

4Chapters
12Topics
50Flashcards

Memory syllabus: all 4 chapters

Expand any chapter to see its topics and sub-topics.

  1. Pointers and Addresses

    3 topics
    • Hexadecimal
    • Pointers and Dereferencing
    • Segmentation Faults
  2. Dynamic Memory

    4 topics
    • Stack and Heap
    • Dynamic Memory Allocation
    • Call Stacks
    • Buffer Overflow
  3. Custom Types and Files

    3 topics
    • Defining Custom Types
    • File I/O and File Pointers
    • Working with Images
  4. Debugging Memory

    2 topics
    • Valgrind
    • Memory Leaks

CS50x: Introduction to Computer Science Memory flashcards โ€” 50 questions with answers

  1. What is the hexadecimal (base-16) number system and which symbols does it use?

    A positional number system with base $16$. It uses sixteen symbols: the digits $0$โ€“$9$ for values zero through nine and the letters Aโ€“F for values $10$ through $15$.

  2. Why is hexadecimal convenient for representing computer memory and addresses?

    Because $16 = 2^{4}$, one hex digit maps exactly to $4$ bits, so one byte ($8$ bits) is exactly two hex digits. This makes binary data far more compact and readable than base-2.

  3. What does the prefix 0x mean in a value like 0xFF in C?

    It signals that the number is written in hexadecimal (base $16$), distinguishing it from decimal. It has no effect on the value itself; $\text{0xFF} = 255$ in decimal.

  4. Convert the decimal number $255$ to hexadecimal, showing the place values.

    $$255 = 15 \times 16^{1} + 15 \times 16^{0} = \text{0xFF}$$ since each hex digit position represents a power of $16$.

  5. What is the general formula for the decimal value of a hexadecimal number with digits $d_{n} d_{n-1} \dots d_{0}$?

    $$\text{value} = \sum_{i=0}^{n} d_{i} \times 16^{i}$$ where each digit $d_{i}$ is between $0$ and $15$ (Aโ€“F standing for $10$โ€“$15$).

  6. What is a pointer in C?

    A variable that stores a memory address โ€” typically the address of another variable. On a modern 64-bit system a pointer occupies $8$ bytes.

  7. What does the & (address-of) operator do in C?

    Applied to a variable, it yields that variable's memory address. Example: if int n = 50;, then &n is the address in memory where n is stored.

  8. What does the * (dereference) operator do when applied to a pointer?

    It 'goes to' the address stored in the pointer and accesses the value there. If int *p = &n;, then *p reads (or writes) the value of n.

  9. What does the declaration int *p; mean, and what is the role of * in the declaration versus in an expression?

    It declares p as a pointer to an int. In a declaration, * marks the variable as a pointer type; in an expression, *p dereferences the pointer to access the pointed-to value.

  10. What is a NULL pointer and why should you initialize pointers you don't yet have an address for to NULL?

    NULL is a special value meaning the pointer points to nothing. Initializing to NULL prevents the pointer from holding a garbage address, and lets you safely test if (p == NULL) before dereferencing.

  11. What is a segmentation fault?

    A runtime error that occurs when a program tries to access a 'segment' of memory it isn't allowed to touch โ€” e.g., reading or writing outside its permitted address space. The operating system terminates the program.

  12. Name three common causes of segmentation faults in C.

    (1) Dereferencing a NULL or uninitialized (garbage) pointer, (2) accessing an array out of bounds, and (3) using memory after it has been freed (use-after-free).

  13. What typically happens if you dereference a NULL pointer, and why is that behavior actually useful?

    The program crashes with a segmentation fault. This is useful because it fails immediately and predictably, rather than silently corrupting some unknown region of memory.

  14. What is the stack region of a program's memory used for?

    It stores local variables and function call frames. Memory on the stack is managed automatically: it is allocated when a function is called and reclaimed when the function returns.

  15. What is the heap region of a program's memory used for?

    It is the pool of memory for dynamic allocation via malloc (and friends). Heap memory persists until the programmer explicitly releases it with free โ€” it is managed manually.

  16. In the classic memory layout diagram, how are machine code, globals, heap, and stack arranged, and in which directions do the heap and stack grow?

    From top: machine code, then global variables, then the heap, with the stack at the bottom. The heap grows downward and the stack grows upward, toward each other โ€” so they can collide if either grows too large.

  17. Compare stack allocation and heap allocation with respect to management, lifetime, and speed.

    Stack: automatic management, lifetime tied to the function's scope, very fast. Heap: manual management (malloc/free), lifetime lasts until explicitly freed, slower and vulnerable to leaks if free is forgotten.

  18. What is a stack overflow, and what commonly causes one?

    When the stack grows beyond its allowed space and overruns other memory โ€” most commonly caused by infinite (or extremely deep) recursion, or by very large local arrays.

  19. What is the purpose and behavior of malloc in C?

    malloc(size) dynamically allocates size bytes on the heap and returns a pointer (void *) to the first byte. The memory is uninitialized (contains garbage), and malloc returns NULL if allocation fails.

  20. What are the three key rules for using free correctly?

    (1) Only free memory that was obtained from malloc/calloc/realloc, (2) free each block exactly once (no double free), and (3) never access memory after it has been freed.

Show the other 30 Memory flashcards
  1. Why must you always check whether malloc returned NULL before using the pointer?

    If the system is out of memory, malloc returns NULL. Dereferencing that NULL pointer causes a segmentation fault, so the program should detect NULL and handle the failure (e.g., return an error) instead.

  2. What does realloc(ptr, newSize) do?

    It resizes a previously allocated heap block to newSize bytes. It may move the block to a new location โ€” copying the old data over automatically โ€” and returns a pointer to the (possibly new) block, or NULL on failure.

  3. How do you correctly allocate space on the heap for an array of $n$ integers, and why use sizeof?

    int *arr = malloc(n * sizeof(int)); Using sizeof(int) makes the code portable, since the size of int can differ across systems, and the total bytes needed is $n \times \text{sizeof(int)}$.

  4. What is the difference between malloc and calloc?

    malloc(size) allocates uninitialized memory (garbage values); calloc(n, size) allocates memory for $n$ elements of the given size and zero-initializes every byte.

  5. What is a call stack?

    The runtime structure that tracks active function calls: each time a function is called, a stack frame holding its parameters and local variables is pushed on top; when the function returns, its frame is popped off.

  6. In call-stack terminology, what is the 'active' frame, and what state are the frames below it in?

    The frame on top of the stack โ€” the currently executing function โ€” is active. All frames beneath it are paused, waiting for the functions above them to return before resuming.

  7. What happens to a function's local variables when that function returns?

    Its stack frame is popped and that memory is reclaimed for reuse by future calls. The local variables cease to exist, and any pointers to them become invalid (dangling).

  8. Why is it a bug to return a pointer to a local (stack) variable from a function, and what is the standard fix?

    The local variable lives in the function's stack frame, which is destroyed on return, leaving a dangling pointer to reclaimed memory. Fix: allocate the data on the heap with malloc so it outlives the function.

  9. How does recursion interact with the call stack, and what does each recursive call contribute?

    Each recursive call pushes a new, separate stack frame with its own copies of the parameters and locals. Frames pop in reverse order as base cases return; unbounded recursion exhausts the stack (stack overflow).

  10. What is a buffer overflow?

    Writing (or reading) past the end of a buffer โ€” a region of memory such as an array โ€” thereby touching adjacent memory. It can corrupt data, crash the program, or be exploited by attackers to hijack control flow.

  11. Why are functions like strcpy or scanf with %s into a fixed-size array considered dangerous?

    They copy input without checking the destination's size, so input longer than the buffer overflows it and overwrites neighboring memory. Safer alternatives bound the length (e.g., fgets, or a width like %9s for a 10-char buffer).

  12. Distinguish a buffer overflow from a stack overflow.

    Buffer overflow: writing beyond the bounds of a specific array/buffer (on stack or heap). Stack overflow: the entire call stack outgrowing its allotted region, typically from deep recursion. Both 'overflow' memory boundaries but at different scales.

  13. What does the typedef keyword do in C? Give the general syntax.

    It creates an alias (a new name) for an existing type: typedef old_type new_name; For example, typedef unsigned char BYTE; lets you write BYTE instead of unsigned char.

  14. What is a struct in C and how do you combine it with typedef to define a custom type like a person with a name and number?

    A struct groups related variables (members) into one type. Example: typedef struct { string name; string number; } person; โ€” after which person p; declares one, with members p.name and p.number.

  15. How do you access a member of a struct through a pointer, and what is the equivalent longhand form?

    With the arrow operator: p->member. It is exactly equivalent to (*p).member โ€” dereference the pointer first, then access the member with the dot operator.

  16. How is CS50's string type actually defined, and what does that reveal about strings in C?

    typedef char *string; โ€” a string is really just a char *, a pointer to the first character of a null-terminated ($\backslash 0$-terminated) sequence of characters in memory.

  17. What does fopen do, and what do the modes "r", "w", and "a" mean?

    fopen(filename, mode) opens a file and returns a FILE * used for all subsequent operations (NULL on failure). "r" = read; "w" = write (truncates/creates the file); "a" = append (writes are added at the end).

  18. Why must you call fclose on every file you fopen, and why check fopen's return value first?

    fclose flushes buffered data to disk and releases the FILE resource โ€” forgetting it can lose data and leak resources. fopen returns NULL if the file can't be opened, and using a NULL FILE * crashes the program.

  19. What are the four arguments of fread, in order?

    fread(buffer, size, quantity, file): (1) a pointer to where the data goes, (2) the size in bytes of each element, (3) how many elements to read, (4) the FILE * to read from. It returns the number of elements actually read.

  20. What do fprintf and fwrite do, and how do they differ?

    Both write to a file via a FILE *. fprintf writes formatted text (like printf but to a file); fwrite(buffer, size, quantity, file) writes raw binary bytes from memory, with no formatting.

  21. How can a program detect a file's format from its first bytes, using JPEG as an example?

    By checking its signature ('magic number'): the first bytes of a JPEG are $\text{0xff}$, $\text{0xd8}$, $\text{0xff}$, and the fourth byte is $\text{0xe0}$ through $\text{0xef}$. File formats begin with characteristic patterns that identify them.

  22. How is a single pixel represented in a 24-bit BMP image?

    As three bytes โ€” $8$ bits each โ€” for the blue, green, and red channels, stored in BGR order. Each channel value ranges from $0$ to $255$, giving $2^{24} \approx 16.7$ million possible colors.

  23. In hexadecimal color notation, what color is 0x0000FF interpreted as RGB, and why?

    Pure blue: the red byte is $\text{0x00} = 0$, the green byte is $\text{0x00} = 0$, and the blue byte is $\text{0xFF} = 255$ (maximum intensity).

  24. When copying or filtering a BMP file, why must a program handle the file's headers before the pixel data?

    A BMP begins with metadata headers (BITMAPFILEHEADER and BITMAPINFOHEADER) describing the file type, size, width, and height. The program must read/copy these first, then process the pixel bytes that follow โ€” otherwise the output file is malformed.

  25. What is Valgrind and when do you use it?

    A command-line tool that runs your program and analyzes its memory usage, detecting memory leaks and invalid memory accesses (e.g., out-of-bounds reads/writes) that might not cause visible crashes. Run it as: valgrind ./program.

  26. In a Valgrind report, what does an 'Invalid write of size 4' message indicate?

    The program wrote $4$ bytes (e.g., one int) to memory it doesn't own โ€” typically writing one element past the end of a heap-allocated int array (an off-by-one / heap buffer overflow).

  27. In Valgrind's leak summary, what does memory that is 'definitely lost' mean?

    Heap memory that was allocated but never freed, and to which no pointer remains at program exit โ€” a genuine memory leak the programmer should fix by adding the missing free.

  28. What is a memory leak?

    Heap memory that a program allocates (via malloc, etc.) but never frees. The memory stays reserved and unusable; in long-running programs, accumulating leaks can exhaust available memory and degrade or crash the system.

  29. What is the fundamental discipline for preventing memory leaks in C?

    Every allocation must be paired with exactly one deallocation: each malloc/calloc/realloc'd block must eventually be passed to free exactly once, before the last pointer to it is lost or goes out of scope.

  30. Contrast a memory leak, a dangling pointer (use-after-free), and a double free.

    Leak: allocated memory is never freed (wasted memory). Dangling pointer: memory is freed but then still accessed โ€” undefined behavior, often a segfault. Double free: the same block is freed twice, corrupting the allocator's state. Leaks waste memory; the other two are invalid operations on freed memory.

CS50x: Introduction to Computer Science Memory FAQ

What is in the CS50x: Introduction to Computer Science Memory syllabus?

CS50x: Introduction to Computer Science Memory is split into 4 chapters: Pointers and Addresses, Dynamic Memory, Custom Types and Files and Debugging Memory. Together they contain 12 topics.

How many chapters are in CS50x: Introduction to Computer Science Memory?

4 chapters, containing 12 topics. The largest chapter is Dynamic Memory, with 4 topics.

Are there flashcards for CS50x: Introduction to Computer Science Memory?

Yes โ€” a 50-card Memory deck, printed in full in the flashcards section of this page and available in the Examius app with spaced repetition. The free plan covers one exam of your choice, including its flashcards; Examius Pro, a one-time purchase, unlocks every other exam and deck.