[CS61C FA20] Lecture 03.2 - C Intro: Basics: Compile v. Interpret — Transcript
Full transcript
- 0:00and welcome back now let's talk about
- 0:04the ways that a program gets run by a
- 0:06computer there's two main ways
- 0:08compilation or interpretation let's
- 0:10actually take a deeper dive into that
- 0:12so compilation is the way c works and
- 0:14the idea
- 0:15of that is you've got raw c code okay
- 0:19a compiler is a program that drinks in
- 0:22a c program and outputs something that's
- 0:24architecture specific from the high
- 0:26order
- 0:26something that's local to that
- 0:28particular architecture different
- 0:30architectures back in the day it was
- 0:32um mac versus pc but even then the
- 0:36apple decided to change the underlying
- 0:38uh
- 0:39cpu and the underlying instructions and
- 0:41architecture they used to actually be an
- 0:42intel one and then if actually
- 0:44the differences weren't that much at all
- 0:45and apple is now recently this won't be
- 0:47recent if this
- 0:48is a is a video as a 2020 i'll say um
- 0:51apple is changing their architecture to
- 0:53be an arm processor so they'll actually
- 0:55be a different instructions
- 0:56architecture they're going to make their
- 0:57own chips and as a result of that
- 1:00you're going to see different
- 1:01architectures now for pcs and macs
- 1:03again so we went different to the same
- 1:05to different again
- 1:07java what java does is it compiles it
- 1:09down as well
- 1:10remember java c is java compiler if you
- 1:12know what the c and java c means it's
- 1:13java compiler
- 1:14and it compiles it down to this
- 1:16something called architecture
- 1:17independence and that was a big deal
- 1:19because c you lock it into the
- 1:21particular architecture
- 1:22it it builds on you're going to build it
- 1:24for you know you port it to a machine
- 1:26and you build it for an architecture
- 1:27i can't just take the executable and run
- 1:28it on a different architecture in java
- 1:31you can this
- 1:32portable bytecode is something you can
- 1:33now pass around a different machine and
- 1:35just run that
- 1:35and there's an interpreter that
- 1:37interprets the bytecode so actually java
- 1:38has both a compiler and an interpreter
- 1:40allowing you have uh machine independent
- 1:44code which is actually pretty powerful
- 1:45python does this real you know runtime
- 1:48byte code conversion
- 1:49but python is mostly interpreted you
- 1:51think of it as being interpreted it does
- 1:52this by code conversion runtime
- 1:54um in c there are
- 1:57there's actually two step there's a
- 1:59picture and a slider too there's a two
- 2:00step process you first compile it down
- 2:02to dot o files this is this is the
- 2:05compiling it down to the assembler
- 2:07language and then you link those with
- 2:10libraries to form the executable so it's
- 2:11really a two-step process but it's
- 2:12hidden because you just type gcc and it
- 2:14makes executables so you're kind of
- 2:15missing those two steps
- 2:16assembling is done but you can it's
- 2:18often as i said done automatically
- 2:20there's the picture i was talking about
- 2:22so we've got two programs foo
- 2:24dot c and bar dot c they both are run
- 2:26through a compiler
- 2:28uh and let's i i decided to just
- 2:31highlight foo c and bold let's say i
- 2:33just made a change to that one
- 2:34the reason i highlight it i want to show
- 2:35you this you then compile them both down
- 2:37now you've got this machine code
- 2:39uh object file.o file we call them.o
- 2:41files it's machine code
- 2:43object files now i've got to put that
- 2:45together as an executable but there's
- 2:47you can't just run an auto file
- 2:48you have to then link it together with
- 2:50whatever libraries are needed you link
- 2:52them together at run time another
- 2:53runtime
- 2:54at linking time not at runtime and
- 2:55linking time to build
- 2:57adot out or the actual executable that
- 2:59you can now run so i can run that there
- 3:01the reason i highlight fu.c watch what
- 3:03happens if i were to change only foo dot
- 3:05c
- 3:06but what happens do i need to compile
- 3:08bar dot c look at that
- 3:09you're like no you already have bar.ob
- 3:11not barton chain bar.c didn't change
- 3:13then bar.o
- 3:14doesn't need to be recompiled that's one
- 3:16of the big ideas of what make files do
- 3:18for you
- 3:19so if i only change one file it only
- 3:20follows it down do i have to relink it
- 3:22yes i still have to re-link it together
- 3:23but the idea i could have bar.c and a
- 3:26million other programs
- 3:27none of them have to be compiled
- 3:28together those dottos are all done and
- 3:30now i can then link them all together
- 3:31with whatever libraries so it's kind of
- 3:33fast so it's not as bad
- 3:34if you make a small one line change i
- 3:36compile that one file and then link
- 3:38everybody else's.o with the new.oh i
- 3:40just created to make the
- 3:41executable advantage of compilation
- 3:45um well you know we've worked with make
- 3:47files i kind of talked about that a
- 3:48second ago we work with make files so
- 3:49you only need to actually
- 3:51recompile the things you change that's a
- 3:52big deal that's nice that's by the way
- 3:54another reason to segment your code into
- 3:56different files
- 3:56if you all have it in one gargantuan
- 3:58file and you make a change you have to
- 4:00then
- 4:01compile the entire entire thing down
- 4:03it's much more efficient to just
- 4:04have a lot of small files it's also
- 4:06better for management as this thing
- 4:07grows
- 4:08that's why java encourages lots of
- 4:10classes different file names um so that
- 4:12you only have to worry about the one
- 4:13that you've changed that's
- 4:14really great runtime performance is
- 4:16unparalleled
- 4:17you're never going to see anything run
- 4:18faster than c assembler is essentially
- 4:20the same thing as cc compiles down to
- 4:22raw assembler but compiled languages in
- 4:24general are as fast as it gets there's
- 4:26no stages of
- 4:28needing to read the source code again
- 4:30and reinterpret it at runtime
- 4:31you've already got the machine code
- 4:32you're just doing it it's raw silicon
- 4:34it's great
- 4:35run on raw silicon which is exciting now
- 4:38you say that but dan people still do
- 4:40lots of big data processing in python
- 4:45here's the reason because a lot of
- 4:47libraries have been optimized for
- 4:49some hardware so gpus we're going to
- 4:51learn about gpus in this class graphics
- 4:52processing units
- 4:53are now general purpose they for a while
- 4:56they were calling them gp gpus general
- 4:57purpose gpus
- 4:59before in the early days they were just
- 5:00their job was to take polygons and
- 5:01render them into pixels
- 5:03um now the general purpose and so you
- 5:05can have you can speed up your photoshop
- 5:06you can feed up speed up
- 5:08lots of computation and the from python
- 5:10um
- 5:11by calling libraries that know how to
- 5:13harness the gpu which is great
- 5:15gpu is amazing waiting to be tapped as a
- 5:18resource for you
- 5:19so python is still used because a lot of
- 5:21libraries optimize and use the gpu also
- 5:24spark is a way very easily in python to
- 5:27kind of as a director as a marionette
- 5:29controller control lots of other
- 5:30machines
- 5:31millions of machines can wake up and do
- 5:32your bidding and driven from python
- 5:34which is really fun and easy to program
- 5:36in
- 5:36i'll admit so that's great also you can
- 5:38make a connection
- 5:39write c code and expose it to python so
- 5:42expose it so in python you can be
- 5:45calling
- 5:46the c program so that's called cython
- 5:48and you can check out cython.org i
- 5:49believe is the website
- 5:51so you can also get have the best of
- 5:52both worlds where you're living in
- 5:53python up here but when you need some
- 5:55actual you know lots of
- 5:56i don't know lots of lots of work done
- 5:59lots of
- 6:00spinning in a loop doing something fast
- 6:02that needs to be as fast as possible
- 6:03well that actually could be c code doing
- 6:04the actual work of that
- 6:05that's compiled up and then run called
- 6:07from python so that's pretty cool
- 6:10disadvantages well the compile
- 6:13the the touch a file compile
- 6:16link run find the error back to that's a
- 6:19slower process than just interpreting or
- 6:20sitting in a reply sitting in a reader
- 6:22value
- 6:23evaluate print loop it's much more fun
- 6:25and easy to kind of have a snippet of
- 6:26code just pipe it there
- 6:27you can't really have a snippet it's not
- 6:28really a c interpreter some folks have
- 6:30tried that i actually had one at some
- 6:32point but that's not really a thing that
- 6:33people most people have or c programmers
- 6:34so it's hard to just have a little line
- 6:36of c code that you want to see what it
- 6:37does just to see if
- 6:38that does that really move the bits in
- 6:39this way well not really you have to
- 6:40actually go and
- 6:41make a program and make a little print
- 6:42thing to test that so it's a little
- 6:44easier to
- 6:45live in an interpreter so disadvantages
- 6:46compiled files because you don't have an
- 6:48interpreter play with things
- 6:49is not so good also compiled files as
- 6:52the first bullet says
- 6:54are machine specific so the dot o files
- 6:57and the executable are machine specific
- 6:59which means you have to then port it
- 7:00when you move it to another system or
- 7:02another operating system you'll say but
- 7:03dan wait
- 7:04windows and mac back in the day when
- 7:05they're both x86
- 7:07that's the same you know same cpu it
- 7:09doesn't work no because the libraries
- 7:11aren't the same so
- 7:12all these things have to work in the
- 7:13same ecosystem cleanly even though the
- 7:15actual underlying
- 7:16cpu is fine it's the same so
- 7:19the idea of of recompiling and moving it
- 7:22to another architecture is called
- 7:23porting your code and that's always a
- 7:25pain because things are different oh is
- 7:26it big indian
- 7:27little indian you'll learn what that
- 7:28means that we that means later in this
- 7:30class
- 7:30is what's the size of an int was it in
- 7:32fact if you ever type um
- 7:34configure it'll show you about a i don't
- 7:36know a couple hundred things it's asking
- 7:38you about that particular system
- 7:39and they're often different you know
- 7:40even a different system might have a
- 7:42different library or a different version
- 7:43of the os might have different versions
- 7:44of this you know
- 7:45we're now 64 bit versus 32 bits these
- 7:48things are all independent and all those
- 7:49things lock in
- 7:50once you lock in an executable it locks
- 7:52in that pattern it won't run
- 7:53if any of those things are changed and
- 7:54that's really hard sometimes so again i
- 7:56mentioned the change compile
- 7:58repeat is slower slower than it is the
- 8:00nice thing by the way i mentioned
- 8:01that you'd only have to compile the
- 8:04particular file that you've changed
- 8:06they've also come up with a parallel
- 8:08compiler if you type
- 8:09make minus j you can imagine the
- 8:12parallelization possibilities if i have
- 8:14100 files never been compiled before
- 8:16i can compile them all in parallel if i
- 8:17have a machine with 100 different cores
- 8:19and i can
- 8:20be accessing my ssd really fast i could
- 8:22actually be very efficient with that so
- 8:24they all happen at once the compile
- 8:25stage and then the linking together
- 8:27is still that you know go that's the
- 8:29bottleneck you're gonna see the
- 8:31amdahl's heartbreaking law where you
- 8:32have to go through a bottleneck that's a
- 8:34serial part of it and you all have to
- 8:35wait
- 8:36it's i'm sorry that you were fast and
- 8:37parallel but i still have to go through
- 8:38this linking bottleneck for that
- 8:40you'll see that when we talk about
- 8:40amdahl's law but just know that a
- 8:42bottleneck is really going to be hard to
- 8:43make that
- 8:43much faster there is the availability of
- 8:46a c
- 8:47preprocessor what that means is it's
- 8:49something that takes the c
- 8:51code you've written that might have some
- 8:53directives some have some things
- 8:55um that are special only listened to
- 8:58only commands for the cpr processor that
- 9:00have that can then
- 9:02that they can they can kind of processed
- 9:04and then release into a dot i file
- 9:06and the ifile is then what's the
- 9:07compiler see so it's actually quite
- 9:08interesting
- 9:09so anything that starts with the hashtag
- 9:11of pound sign um
- 9:13is a cpu processor command so pound sign
- 9:16include is the way that you include a
- 9:18header file into that and actually
- 9:20grabs the header and drops it right in
- 9:21doesn't just point a link to it's on a
- 9:23reference to it it literally grabs the
- 9:24dot h file and puts it in to the dot i
- 9:26file it actually just inserts it in
- 9:29standardio.h is the same thing but when
- 9:30you have the angle brackets it does the
- 9:32same thing but the idea is it's saying
- 9:33it's grabbing from the standard place in
- 9:34the library
- 9:35you have to have a special where they
- 9:37include files and your path for that
- 9:39but it looks for the standard place for
- 9:41standard libraries like that
- 9:44you can you can pound to find globals
- 9:46here i'm defining pi to be some number
- 9:48typically you put parentheses around
- 9:50them because you never know where that's
- 9:51going to be in an expression so just
- 9:53wrap things in parenthesis when you have
- 9:54a pound defined just to be safe
- 9:56there's also if def if and if def
- 9:59and end if and that's a way to kind of
- 10:01say this code will only run if
- 10:03this is defined before or if this has
- 10:04been seen or some this variable is true
- 10:06some expression is true
- 10:08so this means only run this it's
- 10:09actually quite clever you can kind of do
- 10:11debugging by
- 10:12wrapping your stuff by having a debug
- 10:13flag that goes true or false and then
- 10:15it won't print out anything by just
- 10:17recompiling with the debug flag
- 10:19true or false that's kind of a nice
- 10:21thing
- 10:23the full documentation for the c
- 10:24preprocessor is at the bottom line as
- 10:26well
- 10:27if you type dash save temps to gcc it'll
- 10:30actually save the files so you'll
- 10:31actually see the process and actually
- 10:32save the data files in your directory so
- 10:34if you want to actually see what does
- 10:35that if i look like take a look at it
- 10:37and say
- 10:37called save temps there's a warning the
- 10:41cpr browser is great one of the things
- 10:42you can do is you can pound to find
- 10:44macros macros are really great for for
- 10:48kind of making fast code but it can also
- 10:50make code that's hard to debug so let's
- 10:52talk about that
- 10:53so what happens is you're making this
- 10:55macro
- 10:56that text replaces everything it sees
- 10:59below it's not like it's well it's
- 11:01calling the macro it just text replaces
- 11:03it so if i said
- 11:04pound define min of x y to be remember
- 11:07we wrap parentheses around these guys
- 11:09just so that the
- 11:09kind of whatever expression gets
- 11:12expressed it gets
- 11:12evaluated first so if you see this
- 11:14parens x less than parens y
- 11:17question mark that's the ternary if that
- 11:18you see there if
- 11:20if that's true then it's uh the x
- 11:22expression otherwise it's the y
- 11:23expression so
- 11:24that min seems very innocuous right
- 11:26seems fine and mostly works
- 11:28except when one of the values of x and y
- 11:33happens to be a function call that seems
- 11:34fine except the function call has a side
- 11:37effect
- 11:37let's say print out hello world okay
- 11:39let's just say so for z has a side
- 11:41effect
- 11:42and now i say next equals min so now by
- 11:44the way i can call min just like i could
- 11:45call a function
- 11:46but once i've had this pound to find a
- 11:47macro i can call them in just like the
- 11:49function
- 11:49i say min w i call the foo z and that's
- 11:52fine normally what would have happened
- 11:54is
- 11:54foo z would've been called prince hello
- 11:56world and then
- 11:57you know says whatever returns to some
- 11:59value and then the smaller of the value
- 12:01the foo
- 12:01and w returns is the value that's set to
- 12:03next seems innocuous right
- 12:05except the idea that it does a
- 12:08substitution
- 12:09so imagine taking all of the expressions
- 12:11w and replacing it in x
- 12:13and all the expression foo z replacing
- 12:15it in y and what you have now
- 12:17two possible calls to foo z
- 12:20you understand so now is w less than
- 12:23four of z you're gonna call for z now it
- 12:24prints out hello world
- 12:26and if let's say it isn't well you're
- 12:28gonna go to foo z again
- 12:30because for z you know was the was the
- 12:31bigger value was the smaller value
- 12:33actually the smaller value right is the
- 12:34bigger value
- 12:36uh no sorry if it's the smaller value
- 12:37we'll look at the min there's a smaller
- 12:38value if
- 12:39foo happened to win the smaller value
- 12:41competition then foo gets called again
- 12:44to return its value and print hello
- 12:45world gets called twice very simple case
- 12:47but
- 12:48you can imagine if you have any side
- 12:49effects in this very simple case women
- 12:50so be careful with macros is what the
- 12:52point of this
- 12:53slide was so in general this little mini
- 12:56lecture was about compilation versus
- 12:58interpretation
- 12:59and thinking about the powerful idea of
- 13:01a cpu processor and the whole how the
- 13:02whole thing works in terms of linking
- 13:04and compilation together see the next
- 13:07lecture
- 13:08thanks
About this transcript
This page contains the full transcript of [CS61C FA20] Lecture 03.2 - C Intro: Basics: Compile v. Interpret by CS 61C Departmental, generated from the public captions YouTube serves with the video. The transcript has 2,882 words across 462 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.