🖥️

CS50 Week Lecture

Jul 7, 2024

CS50 - Week Lecture

Overview

  • This lecture delves into low-level computer operations.
  • Objective: To understand the building blocks of writing code and interacting with media files.
  • Topics: Removing training wheels in C, image manipulation, memory understanding.

Images and Pixels

  • Images consist of grids of pixels.
  • Hollywood Myth: Enhancing images infinitely is unrealistic due to finite bits/bytes.
  • Artificial Intelligence: Future discussion on computers statistically creating information from limited data.
  • Image Resolution: Higher resolution = more dots/pixels, clearer images.

Real-life Example

  • Pixel artwork by volunteers using post-it notes representing pixels.
  • Representation clarifies the concept of pixels forming an image.
  • pixel art demonstration.

Memory and Addressing

  • Use syntax to navigate and manipulate files and understanding their storage.
  • Example: Storing a smiley face using a grid of zeros (black) and ones (white).
  • Memory Grid Representation: Each cell has an address.

Syntax for Memory Addressing

  • Pointers
    • 'ampersand ( ) operator' - Gets the address of a variable.
    • '* operator' - Dereference operator: Go to the address.
  • Overflowing the Stack and Heap: Risks of using too much memory.*

Basic Code Example

Example: Printing address

  • Objective: Print memory location of an integer variable.
  • Code: Print Address
#include <stdio.h> int main(void) { int n = 50; printf("%p\n", &n); return 0; }
  • Demonstrates usage of %p for printing pointers and addresses.

Addressing in Pointers

Code Example: Integer and Pointer

#include <stdio.h> int main(void) { int n = 50; int *p = &n; printf("%p\n", p); return 0; }
  • Points to an integer variable and prints its address.

Code Example: Pointers

#include <stdio.h> int main(void) { int n = 50; int *p = &n; printf("%d\n", *p); return 0; }
  • Displays the value stored at the address pointed by p.

Key Concepts:

  • Declaring Pointers and accessing their values.
  • Working with different memory locations through symbols.

String Representation

  • Internally, strings are arrays of characters.
  • string type provided by CS50 is essentially a typedef for char.

Manipulating Strings and Chars

  • Code: Demonstrating string operations using char*.*
#include <cs50.h> #include <stdio.h> int main(void) { string s = "HI"; printf("%p\n", s); printf("%p\n", &s[0]); printf("%p\n", &s[1]); printf("%p\n", &s[2]); return 0; }

Key Concepts

  • Addressing: Understand where strings are in memory.
  • Manipulating characters using char*

String Manipulation and Error Handling

  • Functions: malloc and Free help allocate and deallocate memory.
  • Example: Safely copying and manipulating strings.
#include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { char *s = get_string("Enter a string: "); if (s == NULL) { return 1; } // Allocate memory char *t = malloc((strlen(s) + 1) * sizeof(char)); strcpy(t, s); // Capitalize first letter in t if (strlen(t) > 0) { t[0] = toupper(t[0]); } printf("%s\n", s); printf("%s\n", t); // Free memory free(t); return 0; }

Key Concepts

  • Pointers:
    • Dynamic allocation using malloc.
    • Proper memory management with Free.
    • Error handling for null values and checking pointer status.

File I/O Basics

Reading and Writing to Files

  • Fundamental functions: fopen, fclose, fprintf, fscanf for file handling.
  • Example: Writing to a CSV: Phone Book App
#include <cs50.h> #include <stdio.h> #include <string.h> int main(void) { FILE *file = fopen("phonebook.csv", "a"); if (file == NULL) { return 1; } char *name = get_string("Name: "); char *number = get_string("Number: "); fprintf(file, "%s,%s\n", name, number); fclose(file); return 0; }
  • Opens or creates a CSV file, writes new entries into it, and closes it safely.

Copying Files

  • Example: implementing a CP command to copy contents from one file to another.
#include <stdio.h> #include <stdlib.h> #include <stdint.h> typedef uint8_t byte; int main(int argc, char *argv[]) { if (argc != 3) { return 1; } FILE *source = fopen(argv[1], "r"); FILE *dest = fopen(argv[2], "w"); if (source == NULL || dest == NULL) { return 1; } byte buffer; while (fread(&buffer, sizeof(byte), 1, source)) { fwrite(&buffer, sizeof(byte), 1, dest); } fclose(source); fclose(dest); return 0; }

Key Concepts

  • Managing file streams.
  • Reading/Writing binary data with fread and fwrite.
  • Command-line arguments to specify source and destination files.

Summary

  • Low-level understanding of computer operations via memory management, string handling, and file I/O.
  • Applying new techniques to manipulate data efficiently.

Emoji: 🖥️