Understanding Computer Fundamentals and C Programming

Aug 29, 2024

CS50 Week 4 Lecture Notes

Introduction

  • Overview of the week's focus: Lower-level understanding of how computers work.
  • Aim: To give a mental model of what occurs inside a computer when code is executed.
  • Removal of some 'training wheels' in C programming.
  • Introduction to media types like image files and understanding their composition in computers.

Understanding Image Files

  • Image files (e.g., JPEG, PNG) are composed of pixels, which are finite in number.
  • Hollywood vs Reality: Unlike in movies, enhancing an image infinitely is impossible due to finite pixel data.
  • AI and Images: AI might predict or create data statistically where none exists.
  • Demonstration with pixel art on stage.
  • Resolution: Higher resolution means more pixels, leading to clearer images.

Pixels and Images

  • Images are made of a grid of pixels, represented by dots.
  • Demonstration of pixel art using Google Sheets as a grid to create images.

Representing Colors

  • RGB (Red, Green, Blue) model is used in computing.
  • Hexadecimal (base 16) is used for color representation in computing.
    • Hexadecimal uses symbols 0-9, A-F for values 0-15.
    • FF in hexadecimal represents the value 255 in decimal.
  • Explanation of hexadecimal arithmetic and its application in computing.

Memory and Addresses

  • Computers have memory arranged in a grid-like structure with each byte having an address.
  • Hexadecimal notation is used to describe memory addresses.
  • Example: Variable n with value 50 stored at an address.

Pointers in C

  • Pointers are variables that store memory addresses.
  • Ampersand (&) is used to get the address of a variable.
  • Asterisk (*) is used to dereference a pointer, accessing the value at an address.
  • Example: Use of pointers to access and modify variables in memory.
  • Visualization using foam fingers to understand pointers and memory access.

Strings in C

  • String is a sequence of characters terminated by a null character (\0).
  • Internally, a string in C is represented as a char * (pointer to the first character).
  • String manipulation using pointers.

Memory Management

  • Dynamic memory allocation in C using malloc().
  • Error checking with NULL to prevent memory allocation errors.
  • Use of free() to release dynamically allocated memory.
  • Valgrind: A tool to check memory usage and find memory leaks.

Copying and Comparing Strings

  • Comparing strings with strcmp() instead of ==.
  • Copying strings safely using strcpy().
  • Importance of not just copying pointers but duplicating data to avoid unintended modifications.

File I/O in C

  • Types of file operations: fopen, fclose, fprintf, fscanf, fread, fwrite.
  • Example: Writing a program to save user input to a file persistently.
  • Vocabulary: Files are accessed through pointers in C.

Final Thoughts

  • Introduction to manipulating images via file I/O, leveraging understanding of memory and pointers.
  • Pointers and memory management are core skills in C, serving as a foundation for understanding how computers operate at a low level.