YouTube2Text

[CS61C FA20] Lecture 03.2 - C Intro: Basics: Compile v. Interpret — Transcript

by CS 61C Departmental · 2,882 words · 462 segments · language en · Watch on YouTube

Full transcript

  1. 0:00and welcome back now let's talk about
  2. 0:04the ways that a program gets run by a
  3. 0:06computer there's two main ways
  4. 0:08compilation or interpretation let's
  5. 0:10actually take a deeper dive into that
  6. 0:12so compilation is the way c works and
  7. 0:14the idea
  8. 0:15of that is you've got raw c code okay
  9. 0:19a compiler is a program that drinks in
  10. 0:22a c program and outputs something that's
  11. 0:24architecture specific from the high
  12. 0:26order
  13. 0:26something that's local to that
  14. 0:28particular architecture different
  15. 0:30architectures back in the day it was
  16. 0:32um mac versus pc but even then the
  17. 0:36apple decided to change the underlying
  18. 0:38uh
  19. 0:39cpu and the underlying instructions and
  20. 0:41architecture they used to actually be an
  21. 0:42intel one and then if actually
  22. 0:44the differences weren't that much at all
  23. 0:45and apple is now recently this won't be
  24. 0:47recent if this
  25. 0:48is a is a video as a 2020 i'll say um
  26. 0:51apple is changing their architecture to
  27. 0:53be an arm processor so they'll actually
  28. 0:55be a different instructions
  29. 0:56architecture they're going to make their
  30. 0:57own chips and as a result of that
  31. 1:00you're going to see different
  32. 1:01architectures now for pcs and macs
  33. 1:03again so we went different to the same
  34. 1:05to different again
  35. 1:07java what java does is it compiles it
  36. 1:09down as well
  37. 1:10remember java c is java compiler if you
  38. 1:12know what the c and java c means it's
  39. 1:13java compiler
  40. 1:14and it compiles it down to this
  41. 1:16something called architecture
  42. 1:17independence and that was a big deal
  43. 1:19because c you lock it into the
  44. 1:21particular architecture
  45. 1:22it it builds on you're going to build it
  46. 1:24for you know you port it to a machine
  47. 1:26and you build it for an architecture
  48. 1:27i can't just take the executable and run
  49. 1:28it on a different architecture in java
  50. 1:31you can this
  51. 1:32portable bytecode is something you can
  52. 1:33now pass around a different machine and
  53. 1:35just run that
  54. 1:35and there's an interpreter that
  55. 1:37interprets the bytecode so actually java
  56. 1:38has both a compiler and an interpreter
  57. 1:40allowing you have uh machine independent
  58. 1:44code which is actually pretty powerful
  59. 1:45python does this real you know runtime
  60. 1:48byte code conversion
  61. 1:49but python is mostly interpreted you
  62. 1:51think of it as being interpreted it does
  63. 1:52this by code conversion runtime
  64. 1:54um in c there are
  65. 1:57there's actually two step there's a
  66. 1:59picture and a slider too there's a two
  67. 2:00step process you first compile it down
  68. 2:02to dot o files this is this is the
  69. 2:05compiling it down to the assembler
  70. 2:07language and then you link those with
  71. 2:10libraries to form the executable so it's
  72. 2:11really a two-step process but it's
  73. 2:12hidden because you just type gcc and it
  74. 2:14makes executables so you're kind of
  75. 2:15missing those two steps
  76. 2:16assembling is done but you can it's
  77. 2:18often as i said done automatically
  78. 2:20there's the picture i was talking about
  79. 2:22so we've got two programs foo
  80. 2:24dot c and bar dot c they both are run
  81. 2:26through a compiler
  82. 2:28uh and let's i i decided to just
  83. 2:31highlight foo c and bold let's say i
  84. 2:33just made a change to that one
  85. 2:34the reason i highlight it i want to show
  86. 2:35you this you then compile them both down
  87. 2:37now you've got this machine code
  88. 2:39uh object file.o file we call them.o
  89. 2:41files it's machine code
  90. 2:43object files now i've got to put that
  91. 2:45together as an executable but there's
  92. 2:47you can't just run an auto file
  93. 2:48you have to then link it together with
  94. 2:50whatever libraries are needed you link
  95. 2:52them together at run time another
  96. 2:53runtime
  97. 2:54at linking time not at runtime and
  98. 2:55linking time to build
  99. 2:57adot out or the actual executable that
  100. 2:59you can now run so i can run that there
  101. 3:01the reason i highlight fu.c watch what
  102. 3:03happens if i were to change only foo dot
  103. 3:05c
  104. 3:06but what happens do i need to compile
  105. 3:08bar dot c look at that
  106. 3:09you're like no you already have bar.ob
  107. 3:11not barton chain bar.c didn't change
  108. 3:13then bar.o
  109. 3:14doesn't need to be recompiled that's one
  110. 3:16of the big ideas of what make files do
  111. 3:18for you
  112. 3:19so if i only change one file it only
  113. 3:20follows it down do i have to relink it
  114. 3:22yes i still have to re-link it together
  115. 3:23but the idea i could have bar.c and a
  116. 3:26million other programs
  117. 3:27none of them have to be compiled
  118. 3:28together those dottos are all done and
  119. 3:30now i can then link them all together
  120. 3:31with whatever libraries so it's kind of
  121. 3:33fast so it's not as bad
  122. 3:34if you make a small one line change i
  123. 3:36compile that one file and then link
  124. 3:38everybody else's.o with the new.oh i
  125. 3:40just created to make the
  126. 3:41executable advantage of compilation
  127. 3:45um well you know we've worked with make
  128. 3:47files i kind of talked about that a
  129. 3:48second ago we work with make files so
  130. 3:49you only need to actually
  131. 3:51recompile the things you change that's a
  132. 3:52big deal that's nice that's by the way
  133. 3:54another reason to segment your code into
  134. 3:56different files
  135. 3:56if you all have it in one gargantuan
  136. 3:58file and you make a change you have to
  137. 4:00then
  138. 4:01compile the entire entire thing down
  139. 4:03it's much more efficient to just
  140. 4:04have a lot of small files it's also
  141. 4:06better for management as this thing
  142. 4:07grows
  143. 4:08that's why java encourages lots of
  144. 4:10classes different file names um so that
  145. 4:12you only have to worry about the one
  146. 4:13that you've changed that's
  147. 4:14really great runtime performance is
  148. 4:16unparalleled
  149. 4:17you're never going to see anything run
  150. 4:18faster than c assembler is essentially
  151. 4:20the same thing as cc compiles down to
  152. 4:22raw assembler but compiled languages in
  153. 4:24general are as fast as it gets there's
  154. 4:26no stages of
  155. 4:28needing to read the source code again
  156. 4:30and reinterpret it at runtime
  157. 4:31you've already got the machine code
  158. 4:32you're just doing it it's raw silicon
  159. 4:34it's great
  160. 4:35run on raw silicon which is exciting now
  161. 4:38you say that but dan people still do
  162. 4:40lots of big data processing in python
  163. 4:45here's the reason because a lot of
  164. 4:47libraries have been optimized for
  165. 4:49some hardware so gpus we're going to
  166. 4:51learn about gpus in this class graphics
  167. 4:52processing units
  168. 4:53are now general purpose they for a while
  169. 4:56they were calling them gp gpus general
  170. 4:57purpose gpus
  171. 4:59before in the early days they were just
  172. 5:00their job was to take polygons and
  173. 5:01render them into pixels
  174. 5:03um now the general purpose and so you
  175. 5:05can have you can speed up your photoshop
  176. 5:06you can feed up speed up
  177. 5:08lots of computation and the from python
  178. 5:10um
  179. 5:11by calling libraries that know how to
  180. 5:13harness the gpu which is great
  181. 5:15gpu is amazing waiting to be tapped as a
  182. 5:18resource for you
  183. 5:19so python is still used because a lot of
  184. 5:21libraries optimize and use the gpu also
  185. 5:24spark is a way very easily in python to
  186. 5:27kind of as a director as a marionette
  187. 5:29controller control lots of other
  188. 5:30machines
  189. 5:31millions of machines can wake up and do
  190. 5:32your bidding and driven from python
  191. 5:34which is really fun and easy to program
  192. 5:36in
  193. 5:36i'll admit so that's great also you can
  194. 5:38make a connection
  195. 5:39write c code and expose it to python so
  196. 5:42expose it so in python you can be
  197. 5:45calling
  198. 5:46the c program so that's called cython
  199. 5:48and you can check out cython.org i
  200. 5:49believe is the website
  201. 5:51so you can also get have the best of
  202. 5:52both worlds where you're living in
  203. 5:53python up here but when you need some
  204. 5:55actual you know lots of
  205. 5:56i don't know lots of lots of work done
  206. 5:59lots of
  207. 6:00spinning in a loop doing something fast
  208. 6:02that needs to be as fast as possible
  209. 6:03well that actually could be c code doing
  210. 6:04the actual work of that
  211. 6:05that's compiled up and then run called
  212. 6:07from python so that's pretty cool
  213. 6:10disadvantages well the compile
  214. 6:13the the touch a file compile
  215. 6:16link run find the error back to that's a
  216. 6:19slower process than just interpreting or
  217. 6:20sitting in a reply sitting in a reader
  218. 6:22value
  219. 6:23evaluate print loop it's much more fun
  220. 6:25and easy to kind of have a snippet of
  221. 6:26code just pipe it there
  222. 6:27you can't really have a snippet it's not
  223. 6:28really a c interpreter some folks have
  224. 6:30tried that i actually had one at some
  225. 6:32point but that's not really a thing that
  226. 6:33people most people have or c programmers
  227. 6:34so it's hard to just have a little line
  228. 6:36of c code that you want to see what it
  229. 6:37does just to see if
  230. 6:38that does that really move the bits in
  231. 6:39this way well not really you have to
  232. 6:40actually go and
  233. 6:41make a program and make a little print
  234. 6:42thing to test that so it's a little
  235. 6:44easier to
  236. 6:45live in an interpreter so disadvantages
  237. 6:46compiled files because you don't have an
  238. 6:48interpreter play with things
  239. 6:49is not so good also compiled files as
  240. 6:52the first bullet says
  241. 6:54are machine specific so the dot o files
  242. 6:57and the executable are machine specific
  243. 6:59which means you have to then port it
  244. 7:00when you move it to another system or
  245. 7:02another operating system you'll say but
  246. 7:03dan wait
  247. 7:04windows and mac back in the day when
  248. 7:05they're both x86
  249. 7:07that's the same you know same cpu it
  250. 7:09doesn't work no because the libraries
  251. 7:11aren't the same so
  252. 7:12all these things have to work in the
  253. 7:13same ecosystem cleanly even though the
  254. 7:15actual underlying
  255. 7:16cpu is fine it's the same so
  256. 7:19the idea of of recompiling and moving it
  257. 7:22to another architecture is called
  258. 7:23porting your code and that's always a
  259. 7:25pain because things are different oh is
  260. 7:26it big indian
  261. 7:27little indian you'll learn what that
  262. 7:28means that we that means later in this
  263. 7:30class
  264. 7:30is what's the size of an int was it in
  265. 7:32fact if you ever type um
  266. 7:34configure it'll show you about a i don't
  267. 7:36know a couple hundred things it's asking
  268. 7:38you about that particular system
  269. 7:39and they're often different you know
  270. 7:40even a different system might have a
  271. 7:42different library or a different version
  272. 7:43of the os might have different versions
  273. 7:44of this you know
  274. 7:45we're now 64 bit versus 32 bits these
  275. 7:48things are all independent and all those
  276. 7:49things lock in
  277. 7:50once you lock in an executable it locks
  278. 7:52in that pattern it won't run
  279. 7:53if any of those things are changed and
  280. 7:54that's really hard sometimes so again i
  281. 7:56mentioned the change compile
  282. 7:58repeat is slower slower than it is the
  283. 8:00nice thing by the way i mentioned
  284. 8:01that you'd only have to compile the
  285. 8:04particular file that you've changed
  286. 8:06they've also come up with a parallel
  287. 8:08compiler if you type
  288. 8:09make minus j you can imagine the
  289. 8:12parallelization possibilities if i have
  290. 8:14100 files never been compiled before
  291. 8:16i can compile them all in parallel if i
  292. 8:17have a machine with 100 different cores
  293. 8:19and i can
  294. 8:20be accessing my ssd really fast i could
  295. 8:22actually be very efficient with that so
  296. 8:24they all happen at once the compile
  297. 8:25stage and then the linking together
  298. 8:27is still that you know go that's the
  299. 8:29bottleneck you're gonna see the
  300. 8:31amdahl's heartbreaking law where you
  301. 8:32have to go through a bottleneck that's a
  302. 8:34serial part of it and you all have to
  303. 8:35wait
  304. 8:36it's i'm sorry that you were fast and
  305. 8:37parallel but i still have to go through
  306. 8:38this linking bottleneck for that
  307. 8:40you'll see that when we talk about
  308. 8:40amdahl's law but just know that a
  309. 8:42bottleneck is really going to be hard to
  310. 8:43make that
  311. 8:43much faster there is the availability of
  312. 8:46a c
  313. 8:47preprocessor what that means is it's
  314. 8:49something that takes the c
  315. 8:51code you've written that might have some
  316. 8:53directives some have some things
  317. 8:55um that are special only listened to
  318. 8:58only commands for the cpr processor that
  319. 9:00have that can then
  320. 9:02that they can they can kind of processed
  321. 9:04and then release into a dot i file
  322. 9:06and the ifile is then what's the
  323. 9:07compiler see so it's actually quite
  324. 9:08interesting
  325. 9:09so anything that starts with the hashtag
  326. 9:11of pound sign um
  327. 9:13is a cpu processor command so pound sign
  328. 9:16include is the way that you include a
  329. 9:18header file into that and actually
  330. 9:20grabs the header and drops it right in
  331. 9:21doesn't just point a link to it's on a
  332. 9:23reference to it it literally grabs the
  333. 9:24dot h file and puts it in to the dot i
  334. 9:26file it actually just inserts it in
  335. 9:29standardio.h is the same thing but when
  336. 9:30you have the angle brackets it does the
  337. 9:32same thing but the idea is it's saying
  338. 9:33it's grabbing from the standard place in
  339. 9:34the library
  340. 9:35you have to have a special where they
  341. 9:37include files and your path for that
  342. 9:39but it looks for the standard place for
  343. 9:41standard libraries like that
  344. 9:44you can you can pound to find globals
  345. 9:46here i'm defining pi to be some number
  346. 9:48typically you put parentheses around
  347. 9:50them because you never know where that's
  348. 9:51going to be in an expression so just
  349. 9:53wrap things in parenthesis when you have
  350. 9:54a pound defined just to be safe
  351. 9:56there's also if def if and if def
  352. 9:59and end if and that's a way to kind of
  353. 10:01say this code will only run if
  354. 10:03this is defined before or if this has
  355. 10:04been seen or some this variable is true
  356. 10:06some expression is true
  357. 10:08so this means only run this it's
  358. 10:09actually quite clever you can kind of do
  359. 10:11debugging by
  360. 10:12wrapping your stuff by having a debug
  361. 10:13flag that goes true or false and then
  362. 10:15it won't print out anything by just
  363. 10:17recompiling with the debug flag
  364. 10:19true or false that's kind of a nice
  365. 10:21thing
  366. 10:23the full documentation for the c
  367. 10:24preprocessor is at the bottom line as
  368. 10:26well
  369. 10:27if you type dash save temps to gcc it'll
  370. 10:30actually save the files so you'll
  371. 10:31actually see the process and actually
  372. 10:32save the data files in your directory so
  373. 10:34if you want to actually see what does
  374. 10:35that if i look like take a look at it
  375. 10:37and say
  376. 10:37called save temps there's a warning the
  377. 10:41cpr browser is great one of the things
  378. 10:42you can do is you can pound to find
  379. 10:44macros macros are really great for for
  380. 10:48kind of making fast code but it can also
  381. 10:50make code that's hard to debug so let's
  382. 10:52talk about that
  383. 10:53so what happens is you're making this
  384. 10:55macro
  385. 10:56that text replaces everything it sees
  386. 10:59below it's not like it's well it's
  387. 11:01calling the macro it just text replaces
  388. 11:03it so if i said
  389. 11:04pound define min of x y to be remember
  390. 11:07we wrap parentheses around these guys
  391. 11:09just so that the
  392. 11:09kind of whatever expression gets
  393. 11:12expressed it gets
  394. 11:12evaluated first so if you see this
  395. 11:14parens x less than parens y
  396. 11:17question mark that's the ternary if that
  397. 11:18you see there if
  398. 11:20if that's true then it's uh the x
  399. 11:22expression otherwise it's the y
  400. 11:23expression so
  401. 11:24that min seems very innocuous right
  402. 11:26seems fine and mostly works
  403. 11:28except when one of the values of x and y
  404. 11:33happens to be a function call that seems
  405. 11:34fine except the function call has a side
  406. 11:37effect
  407. 11:37let's say print out hello world okay
  408. 11:39let's just say so for z has a side
  409. 11:41effect
  410. 11:42and now i say next equals min so now by
  411. 11:44the way i can call min just like i could
  412. 11:45call a function
  413. 11:46but once i've had this pound to find a
  414. 11:47macro i can call them in just like the
  415. 11:49function
  416. 11:49i say min w i call the foo z and that's
  417. 11:52fine normally what would have happened
  418. 11:54is
  419. 11:54foo z would've been called prince hello
  420. 11:56world and then
  421. 11:57you know says whatever returns to some
  422. 11:59value and then the smaller of the value
  423. 12:01the foo
  424. 12:01and w returns is the value that's set to
  425. 12:03next seems innocuous right
  426. 12:05except the idea that it does a
  427. 12:08substitution
  428. 12:09so imagine taking all of the expressions
  429. 12:11w and replacing it in x
  430. 12:13and all the expression foo z replacing
  431. 12:15it in y and what you have now
  432. 12:17two possible calls to foo z
  433. 12:20you understand so now is w less than
  434. 12:23four of z you're gonna call for z now it
  435. 12:24prints out hello world
  436. 12:26and if let's say it isn't well you're
  437. 12:28gonna go to foo z again
  438. 12:30because for z you know was the was the
  439. 12:31bigger value was the smaller value
  440. 12:33actually the smaller value right is the
  441. 12:34bigger value
  442. 12:36uh no sorry if it's the smaller value
  443. 12:37we'll look at the min there's a smaller
  444. 12:38value if
  445. 12:39foo happened to win the smaller value
  446. 12:41competition then foo gets called again
  447. 12:44to return its value and print hello
  448. 12:45world gets called twice very simple case
  449. 12:47but
  450. 12:48you can imagine if you have any side
  451. 12:49effects in this very simple case women
  452. 12:50so be careful with macros is what the
  453. 12:52point of this
  454. 12:53slide was so in general this little mini
  455. 12:56lecture was about compilation versus
  456. 12:58interpretation
  457. 12:59and thinking about the powerful idea of
  458. 13:01a cpu processor and the whole how the
  459. 13:02whole thing works in terms of linking
  460. 13:04and compilation together see the next
  461. 13:07lecture
  462. 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.