CS50 — Week 4

CS50 — Week 4

Strings Are a Lie!

  • what we’ve been treating as type “string” in CS50’s ID are really just memory addresses of the first char in any given string
    • so how does the computer find the “string” I want if I, say, want to printf it?
    • computer goes to the memory address of first char and reads chars in all subsequent memory addresses until coming to char \0 which denotes end
  • essentially a string is really just a pointer
  • string = synonym for data type char *
    • there is no string!!

The Star Operator

  • the “go there” operator (also called the “dereference” operator)
  • the address of whatever type comes before – aka a pointer to that data
  • i.e. the way we type out “the address of a char” = char *

Pointers

  • pointers are just addresses where some data is stored!
  • when using pointers, be careful:
    • give the pointer a pointee (they are two separate things, so a pointee is not set up automatically) *x = malloc(sizeof(int));
    • dereference a pointer to access its pointee *x = 42;
    • assignment (=) between pointers give them access to the same pointee (called “sharing” a pointee) y = x;
  • my own question: in these examples, then, are x and y the pointers and *x the pointee?

 Pointer Arithmetic

  • math with pointers!

Memory Leak

  • if I start dynamically allocating memory in my program, I need to worry about freeing up that allocated memory once I’m done using it (or else my program may grow much more costly in terms of memory than it needs be)
  • asking for memory and then never giving it back – memory leaks – can cause a Mac or PC to slow
  • we can use valgrind ./program name to show us memory leaks (if they otherwise seem fine, aka are able be made)

The Heap and The Stack

  • the “stack” refers to functions that are called
    • on the bottom is main()
    • stacked on top are any functions called within main(), functions called within those functions, etc. etc.
    • once a function is finished (aka has gone thru all its steps/returned some value), the computer can “forget” about it and it’s taken off the stack, so to speak
  • the “heap” refers to memory allocations that are made
    • the top would be the first malloc()and from there down would any other malloc() be
  • stack overflow
    • when the stack grows up too far
    • a comp can be hacked and damaged if a hacker can take control using a memory mistake I’ve made!
  • heap overflow
    • when the heap grows down too far
  • both are examples of what’s called buffer overflow, where you are trying to use more memory than you should
  • to prevent from hacking attacks & server compromise (at least in one way), the programmer needs to make sure that when using a chunk of memory, she checks the bounds so that no one can go in and overwrite parts of code with their own…
    • ex: take a command line argument to be put into some char * of size 10 but then don’t check (maybe with an if() statement, for example) that a user doesn’t input more than 10 chars
    • the user could then input enough that (heap’s going down, remember) the return values (little breadcrumbs comp has given itself as reminders of where to return to after current function is finished) are overwritten with the user’s own data, thereby never allowing the comp to return to the place in the program but rather possibly sending it to a user’s own malicious program, etc.

Images!

Bitmap – BMP

  • literally a map of bits with corresponding color values
Screen Shot 2017-09-23 at 11.12.56 AM
a very basic smiley face bitmap, where 1s are white and 0s are black

JPEG

  • what Facebook, cameras use
  • stores potentially millions of colors
  • allows for compression – throw away some of the 0s and 1s in order to decrease the file size for easier storage, emailing, etc.
  • all JPEG files start with the same 3 bytes: 255 216 255

Hexadecimals

  • when speaking of images & colors, decimal and binary give way to hexadecimals
  • 16 digits, from 0 – 15, borrowing alphabet letters after 9
    • 0 1 2 3 4 5 6 7 8 9 a b c d e f
  • 0xis simply a convention to signal that some hexadecimals are thereafter following
    • 0xff = 255 in decimal, 11111111 in binary
  • so the JPEG starting bytes in hexadecimal are: 0xff 0xd8 0xff

What is a File?

  • a bunch of bits, stored somewhere (on a laptop or desktop or phone, etc.)
  • what does it mean to be a Microsoft Word file vs. an Excel file vs. a JPEG vs. mp3…
    • first bits follow some pattern, demarcating that file type

struct

  • in C, allows you to create some structure inside of which you store information

.csv

  • “comma separated values” files
  • simple Microsoft Excel, Google Spreadsheets, Apple Numbers can export these
    • just series of values separated by commas

 


Today’s Resources:

https://video.cs50.net/2016/fall/lectures/4?

Advertisements
CS50 — Week One Notes, part 2

CS50 — Week One Notes, part 2

I had to break these up into two different days because it was information overload.

Side note to self: If I’m ever in my workspace and I accidentally made a never-ending program that’s trying to force me into infinite loops of the same banal GetInt() function, use ctrl-z!!!

Conditional Statements

  • conditional branches/expressions/statements allow programs to make decisions depending on variable values or user input

if (boolean expression)

{

}

  • (mouse down) (x < 10) etc.
  • boolean expression evaluates to true? all lines of code between curly braces will execute from top to bottom
  • boolean expression false? those lines of code will not execute

if (boolean expression)

{

}

else

{

}

  • boolean expression evaluates to true? all lines of code between 1st set of curly braces will be executed, top to bottom
  • false? follow instructions in 2nd set of curly braces
  • I can create chains of mutually exclusive branches by using if, else if (* n), else with as many different boolean expressions
  • I can layer on as many if statements as I like (creating a chain of non-mutually exclusive branches), but the else will only bind to the nearest if

switch()

  • allows me to specify distinct cases rather than having to rely on boolean expressions
  • must break between each case, or I’ll “fall thru” each case and execute them all in order (unless that’s what I’m trying to do)

int x = GetInt();
switch(x)
{
case 5:
printf("Five!\n");
case 4:
printf("Four!\n");
case 3:
printf("Three!\n");
case 2:
printf("Two!\n");
case 1:
printf("One!\n");
default:
printf("Blast off!\n");
}

The ternary operator ?:

  • useful when writing very short if-else statements
these 2 code snippets act identically
int x = GetInt();
if (b == 0)
{
x = 5;
}
else
{
x = 6;
}
int x = GetInt();
x = (b == 0) ? 5 : 6;

Loops

  • allow programs to execute lines of code repeatedly (saves me from needing to copy/paste or repeat lines of code)

while (true)

{

}

  • infinite loop: lines of code between curly braces will execute repeatedly top to bottom forever (because true is always true)

while (boolean expression)

{

}

  • if boolean expression evaluates to true, all lines of code within curly braces will execute repeatedly top to bottom, until the boolean expression evaluates false

do

{

}

while (boolean expression);

  • executes all lines of code in curly braces, then checks boolean expression – if true, goes back and repeats code block until boolean exp. evaluates to false

for (int i = 0; i < 10; i++)

{

}

  • repeats the body code a specified number of times (above, for example, 10 times)
  • what the parts?!
    • the counter variable(s) (above, i) is set (above, to 0)
    • the boolean expression is checked (above, i < 10)
      • if true, body code executes
      • if false, body code does not execute
    • after getting to the end of the body code, the counter variable i is incremented (above, by 1, using the shorthand var++)
    • the boolean exp. is checked again and either goes thru the body code again or does not
  • more generally: for (start; expr; increment) where there can be more than one statement in start and increment

 

basics of higher-level software programming languages

basics of higher-level software programming languages

Notes:

Computers are linear – they are reading top to bottom, left to right. If you want to make two things happen at once, you are creating the illusion that the computer is doing this. (But it’s not, it’s doing one thing at a time still.)

Everything’s possible with programming, but it takes some cleverness to accomplish it.

Everything has a position – the file, the program, etc.

Frames: everything you want to have happen “at the same time” has to happen within the same frame – calculate a little, show a little, calculate a little, show a little.

Anything that happens first tells the computer what’s coming next. (For example, in CSS the “.” before “.thick-outline” tells me that it’s going to be read by the computer as a class, and not something else.)

Always think about everything possible that people could do. If you have some data they can input, think about all the different data they could give you, even outside your expectations (hackers use data exceptions to break programs, for example).

If you ask the computer, what data is in that position, and that position does not exist, it’ll be “null”.

Array = database without a pointer, instead it just has numerical names to identify the location of various elements. (No “next line”, just “data piece 4” or “data piece 7”.)

Native data types = what’s already there. If it’s not a native data type (like strings in C), it’s going to take an extra cleverness to make it work.

Because the computer reads left to right, there’s precedence. Whatever comes first sets the stage. For example, it quotes tell the computer there’s text coming, and then I type “//”, this will be shown as text and not create a comment.

In C, “;” is not needed at the end of pre-processor directives (#blah input library blah), comments, function definitions, and controllers.

In C, you must start with the function “main” (like how you have to put everything you want the user to see inside the <body> tag in HTML).


4 basics:

  1. comments

  2. data

  3. functions

  4. variables/names

comments

  • text not read by the compiler or the interpreter
  • // – line comment (end comment by going to new line)
  • /* – block comment (end with */)
  • for me, for other people looking at my code, and for remembering things that for the moment aren’t visible (I create some variable but what am I using it for? etc.)

data

  • number types
    • boolean
      • 0 or 1 – one binary digit
      • for example: “true” returns a boolean 1
      • use it every time you can
    • integers (C: “int”)
      • shorts, longs
      • if you can stick with integers, do! otherwise it’s a big mess
    • floating points/floats
      • decimals are infinite! (and therefore can get very messy)
  • character types (C: “char”)
    • strings = a string of characters/a blip of text
      • in C, there’s support for how you can make your own strings, but it’s not it’s own type like it is in C++
        • one way: make an array of letters
        • a bunch of things stored in your memory, accessed thru pointers telling your computer where in the memory to look for that piece of data
        • can use functions like “printf”
  • pointer types
    • every type of data is really just a position on the memory that your computer is accessing
    • the computer does not hold anything in its hands – it goes line by line and accesses the memory
    • even RAM is used in the same way, where the computer has to call it up from memory to access it, but it’s just a smaller amount and therefore easier/faster for the computer to get through it
    • pointers tell the computer where to look in the memory

functions

  • basic, native functions
    • aka the actual building blocks
      • examples: +, int, return, true, const char, etc. (everything pink in Xcode)
      • “int” locates, sets up memory space that’s the right size to store integers
  • library/included functions
    • a group of other people’s functions (built off native functions)
    • problem could be that I don’t know how it works, since someone else made it
      • documentation (description, examples, source code) or testing can help
  • custom functions
    • my own (also built off native functions)
  • functions work with data
    • functions that work with integers only, for example
  • 3 things functions do:
    • input data into the function from the code (either actual user input I type in and can see or from variables I’ve set up with some stored memory of some data) = parameters
      • a function can have no parameters, many (a lot of things to input), etc.
    • output data into the code = return
      • for example, if I am using the function “+” and I input the data (3, 3) the output will be (6)
        • if I set a variable y = 3+3, then y will have a value of 6 anywhere it appears in my code
      • a function can only have one return -> need many returns? use multiple functions
      • the “return” function helps other functions return
    • do
      • changing a pixel in a game, putting a command prompt on the screen, making a sound
      • many functions could come first and pass data around before you ever come to a “do” that the user can see
      • can be a do function just on its own, for example “delay” that makes the program wait
      • code pointer movement functions, just moving code pointers around, like “if” “then” “loop” etc. are do
      • you can’t program a functionality – every function with a “do” is native
    • some functions will do all 3, many will have parameters and then do something or give a return
    • syntax of a function: name (parameters)
    • most of the time, my functions will call other functions – functions inside functions
      • subdividing functions into smaller little pieces is useful because then I can reuse them other places – allows for flexibility & allows me to work on smaller tasks at a time for more gratification/feeling of progress
    • when you use a function, it’s a “call
    • before you ever use it, you need to define a function (tell the computer what to do)

variables

  • there’ll be many custom things I’ll make – functions, variables, arrays & structs of different types
    • variables = holding data directly
    • arrays = bunch of different data each with a number to identify it
    • structs = multiple pieces of data held in a single location (using different sub-names to access them within that location)
  • start by declaring (its type), then space and the name of the variable
    • ex: “int variable_name”

Today’s resources:

A 4-hour FaceTime with my older brother.

Aside

viking school’s “How a Website is Built” notes

An example workflow to make a simple blog for a group of 3 friends.

The workflow =

discuss user needs with the client ->

build a mockup & check with client for satisfaction ->

break the mockup into bite-sized stories

story by story, do the following:

->-> determine, set up data architecture

 ->-> build back end

->-> build front end

-> refactor (restructure existing code w/out changing its external behavior), iterate (repeat) & ship (will learn more about this later) site to production

  • he uses Balsamiq to create mockups
  • when creating a story, think about who, what and why
  • in our example, we have 3 stories:
    • As an [author], I want to [publish posts on the site] so that I can [put my thoughts on the web]
    • As a [reader], I want to [easily view the most recent posts] so that I can [stay up to date]
    • As a [reader], I want to [write a comment on that post] so that I can [offer my thoughts on a post]
  • next step is to prioritize which stories to tackle first
  • he uses PivotalTracker to track stories/his progress
  • once selected the story to work on, determine data needs
    • on a whiteboard, pencil/paper etc. – diagram out what you’re going to have to work thru to start planning how you’ll implement it
  • work thru backend->frontend
  • check site/debug throughout
  • refactor (he says his code isn’t good and should be cleaned up – I’m curious as to what this actually means)
  • present to client