CS50 Lecture - Week 4

Jun 28, 2024

CS50 Lecture - Week 4

Introduction

  • Instructor: David J. Malan
  • Topics: Deeper look into how images and more are implemented using binary (0s and 1s) and how to manipulate memory using C.

Image Representation & Pixels

  • Week 0 Recap: Images are represented as a grid of pixels, each pixel defined by a bit pattern for its color.
  • Hollywood Myth: Enhancing images has limits due to pixelation.
  • Bitmap: A type of image, represented by a grid of bits where each bit can represent a color (e.g., 1 for white, 0 for black).
  • Bitmap Example: Smiley face representation by a pattern of bits.

RGB Representation

  • RGB System: Red, Green, Blue values combined to create any color.
  • Photoshop Example: RGB (0,0,0) for black, RGB (255,255,255) for white, and corresponding high values for primary colors like red, green, and blue.

Hexadecimal System

  • Basics: A base-16 system using digits 0-9 and letters A-F.
  • Counting in Hex: Demonstration of counting from 0 to 255 in hex (00 to FF).
  • Hex in Memory: Simplifies expressing binary values and memory addresses (e.g., 0x prefix for hex values).

Memory and Pointers

  • Introduction to Pointers: Variables that contain the address of another variable.
  • Operators: & (address of) and * (dereference/goto) operators.
  • Pointer Example: Demonstrating how int *p = &n; and *p = 50; works.

Strings and Memory

  • Understanding Strings: Strings in C are char*, pointers to the first character in a sequence of characters terminated by a NULL character (\0).
  • Use of &operator and *operator: How to access and modify string characters using pointer arithmetic.

Pointer Arithmetic

  • Demonstration: Use of s as a pointer and *s to access the value. Pointer arithmetic examples using strings.
  • Syntactic Sugar: Using the array notation (s[i]) vs. pointer arithmetic.
  • Importance of Null-Terminator: Implications of missing null-terminator.

String Comparison in C

  • STR Compare: Using strcmp to compare strings instead of == due to addressing issue.
  • Demo: Comparison example and debugging using print and printf.

Memory Allocation in C

  • Dynamic Memory Allocation: Using malloc for dynamic allocation and free for deallocation.
  • Examples: Correctly copying strings using malloc and strcpy.
  • Best Practices: Always checking for NULL and freeing allocated memory.

Common Bugs in C

  • Common Errors: Segmentation faults, buffer overflows, and memory leaks.
  • Tools: valgrind for detecting memory errors and leaks.
  • Example Fixes: Correctly indexing arrays, freeing memory, and ensuring proper memory allocations.

Functions and Scope

  • Swap Function: Example of passing by value vs. passing by reference using pointers.
  • Memory Segments: Explanation of stack, heap, and where variables and functions live in memory.
  • Examples: Correct implementation of the swap function and troubleshooting common related errors.

Conclusion

  • Summary: Review of today's key concepts: images, memory, pointers, dynamic memory allocation, and string manipulation.
  • Next Steps: Apply these concepts in problem sets and specific applications like manipulating image pixels for filters.