YouTube2Text

[CS61C FA20] Lecture 04.1 - C Intro: Pointers, Arrays, Strings: Pointers and Bugs — Transcript

by CS 61C Departmental · 3,048 words · 472 segments · language en · Watch on YouTube

Full transcript

  1. 0:00and welcome back now let's talk about
  2. 0:03what a pointer is
  3. 0:05exciting we titled this bugs and
  4. 0:07pointers because really
  5. 0:08that's what this is the source of so
  6. 0:11many bugs i just
  7. 0:12it's so sad to share with you this is
  8. 0:14like giving you a very
  9. 0:15sharp edge thing with the blade and then
  10. 0:18cut and don't cut yourself well you got
  11. 0:20to cut yourself and
  12. 0:20you can have bugs thanks to pointers so
  13. 0:22pointers are the reason for this whole
  14. 0:23thing
  15. 0:24but they're powerful we'll talk about
  16. 0:25that the last slide we'll talk about why
  17. 0:26we use pointers in the first place
  18. 0:28so more about variable declaration
  19. 0:30variable declaration
  20. 0:31so mostly it's similar to java except
  21. 0:33that java
  22. 0:35declares variables and that gives them a
  23. 0:38default value that makes sense for that
  24. 0:39variable
  25. 0:40if the number gives it 0 probably not in
  26. 0:43c c
  27. 0:44when you make a new variable without
  28. 0:45giving it a value garbage
  29. 0:48i'll say it again garbage it has no idea
  30. 0:50it's a box
  31. 0:51and it's unknown inside that's the
  32. 0:53source of a ton of problems so
  33. 0:55you have to always make a new variable
  34. 0:57and then initialize it before you can
  35. 0:58use it you could have used it in java
  36. 1:00and you might have gotten lazy and just
  37. 1:01started using oh there's zero
  38. 1:02i make a new counter it would probably
  39. 1:04default to zero let me now you know as
  40. 1:06accumulator let me
  41. 1:07add numbers to it no you've got to
  42. 1:09declare something if it's garbage in
  43. 1:10and just keep adding some numbers into
  44. 1:12it it's going to be continued to be
  45. 1:12garbage so you got to make sure this
  46. 1:14so here's some example declarations you
  47. 1:17used to have to declare things at the
  48. 1:18top
  49. 1:18so int a is b a is 0 b is 10.
  50. 1:22in ansi c you couldn't do this in 1986
  51. 1:25you couldn't do this for
  52. 1:26int i is one that's a really standard
  53. 1:28thing to do in java you can't do that
  54. 1:29here
  55. 1:30but you can do it in c99 and beyond you
  56. 1:32can just say four into i
  57. 1:33zero so i kind of mentioned that before
  58. 1:34but it's useful to know
  59. 1:37so undefined behavior undefined behavior
  60. 1:40in c means
  61. 1:42it's unpredictable it might run 100
  62. 1:44times on one computer and then 100 first
  63. 1:46time doesn't work
  64. 1:47or it might be great in this computer
  65. 1:48the same code don't touch it i compile
  66. 1:50it for this computer
  67. 1:51and it doesn't work or i upgrade the os
  68. 1:53and the program doesn't work anymore
  69. 1:54because of a new library change
  70. 1:56all these things are undefined behavior
  71. 1:58and a lot of these things are really
  72. 1:59challenging to find
  73. 2:01things that are undefined things are
  74. 2:03kind of uh random hardware produced
  75. 2:05seems like evade you
  76. 2:06we call them heisenbugs if you haven't
  77. 2:08heard that you start using that word
  78. 2:10heisenbugs heisenberg's uncertainty
  79. 2:12principle so the quantum
  80. 2:13quantum physics so think about the idea
  81. 2:17that you want to avoid that you want to
  82. 2:18avoid that space and have
  83. 2:20actually you want to have repeatable
  84. 2:21bugs bugs are repeatable we can find
  85. 2:23them right i just logically trace it
  86. 2:24down and i can find them
  87. 2:25those are called boar bugs so
  88. 2:27heisenberg's bad hard to trace and hard
  89. 2:29to find and may never even show up until
  90. 2:31the millionth time
  91. 2:32you run it after you ship the code out
  92. 2:34that's really hard
  93. 2:35it's really tough for you as a as a
  94. 2:37software developer
  95. 2:38board bugs easy you can trace it you
  96. 2:40test it and test you test it
  97. 2:41so you find these board bugs board bugs
  98. 2:42are easy to find
  99. 2:44okay pointers the first big
  100. 2:48uh duality you have is an address
  101. 2:51versus a value okay um
  102. 2:54for now we're gonna live in an abstract
  103. 2:56world in which all of your memory you're
  104. 2:58gonna have access to and be able to
  105. 2:59control
  106. 3:00is one really big array an infinitely
  107. 3:03big array
  108. 3:04starts at zero and goes up to some value
  109. 3:07that's it
  110. 3:08some goes up to some address
  111. 3:10relationships i should i should use it
  112. 3:12use the right terms
  113. 3:13goes up to some address each cell
  114. 3:16each cell has a value the guy inside the
  115. 3:19cell and has an address the address of
  116. 3:21the cell like a house house has an
  117. 3:22address and has as a contents
  118. 3:23has the value okay because we start from
  119. 3:26zero
  120. 3:27does it make sense to use int's signed
  121. 3:29or unsigned numbers
  122. 3:31as your as your addressing mode you're
  123. 3:33right it doesn't make sense to use a
  124. 3:35signed number because you don't have
  125. 3:36negative
  126. 3:37addresses so don't confuse an address or
  127. 3:40the value
  128. 3:41but now think of it as an infinitely
  129. 3:43large array everything's free and wild
  130. 3:46it'll get more constrained and more
  131. 3:47painful later but for now the free and
  132. 3:48wild and
  133. 3:49the 60s free and wild everything's great
  134. 3:52infinite array go crazy
  135. 3:54here's an example this picture uh this
  136. 3:56picture here on the right if i can show
  137. 3:58you let me if i can get this uh pen to
  138. 4:00work
  139. 4:01these are addresses 101 305.
  140. 4:06these are your values 104. so the house
  141. 4:10at 104
  142. 4:11holds the value 23. okay so for now
  143. 4:14understand that
  144. 4:15i have another house holding 42
  145. 4:17somewhere longer down there okay
  146. 4:21a pointer a pointer
  147. 4:25is a cell whose value
  148. 4:28is somebody else's address that's simple
  149. 4:32that's it simple as that okay pointer we
  150. 4:35say
  151. 4:36points to that address so we'll actually
  152. 4:38have a picture for that
  153. 4:40so there's my locations up top 101 305
  154. 4:43my names let me even have some names
  155. 4:45there x and y
  156. 4:46x what's x's address 104 what's x's
  157. 4:50value
  158. 4:5023. i'm going to make a new guy well
  159. 4:53i have a new i have a new variable i
  160. 4:56don't know where it lives i don't know
  161. 4:57its address
  162. 4:59but for p to point to x
  163. 5:02and to have access to x means what
  164. 5:05should be in p's
  165. 5:06cell anybody anyone anyone bueller
  166. 5:09bueller that's right 104. so if i
  167. 5:12point if p points to x it means that x's
  168. 5:15address is in p
  169. 5:16cell or is p's value okay not too hard
  170. 5:21here's some syntax so the way i declare
  171. 5:23and tell c
  172. 5:24that p is going to be a pointer it's
  173. 5:27going to be
  174. 5:28it's an integer it's a pointer to an
  175. 5:30integer that's what it says
  176. 5:31it says it i'm now going to hold a box
  177. 5:34and the box the
  178. 5:35i'm going to have i'm going to have a
  179. 5:37value inside of i'm going to live
  180. 5:38somewhere
  181. 5:39i'm going to value inside of me that
  182. 5:40value if i were to follow it is going to
  183. 5:42be an
  184. 5:43integer in that location that's what
  185. 5:44that says p points to an integer
  186. 5:47the next line says that p equals address
  187. 5:50of y
  188. 5:51i i say don't you say ampersand no p
  189. 5:54equals address of y read that as address
  190. 5:57of y
  191. 5:57p now if p hat by the way the first line
  192. 6:00what is that what's the goal what's p
  193. 6:01what's p store after that first line
  194. 6:04garbage it's a box
  195. 6:05it had garbage inside remember that the
  196. 6:07line ago it had garbage in that side of
  197. 6:08that box i showed it as blank it's
  198. 6:10really garbage
  199. 6:11p equals adder so y says ah p is going
  200. 6:13to now
  201. 6:14get the address of y and put it in p's
  202. 6:17value
  203. 6:18now how do you use it well
  204. 6:21you star has this kind of overloaded
  205. 6:24idea
  206. 6:24so if you're declaring it star says to
  207. 6:26the c compiler this is i'm going to make
  208. 6:28this a pointer
  209. 6:29but if i'm using it and i want to get i
  210. 6:31say i got pointer p
  211. 6:32i want to follow the pointer to get
  212. 6:34whatever value is
  213. 6:36behind it then you're going to say star
  214. 6:37p again we're going to say star p there
  215. 6:39and by the way that star p
  216. 6:40could be on the left side or the right
  217. 6:41hand side of the equal sign it's on the
  218. 6:42right hand side of the equal sign as it
  219. 6:44is here
  220. 6:44it's getting the value out of it and
  221. 6:45storing in z you could have swapped that
  222. 6:47and said star p
  223. 6:48equals e would you take z value across
  224. 6:50the equal sign and stuff it in wherever
  225. 6:52p
  226. 6:52not into p whatever p is pointing to
  227. 6:55okay which is here why why would it got
  228. 6:57a new value
  229. 6:58why would have gotten z out of that okay
  230. 7:00by the way it's called dereferencing
  231. 7:01that's really important that's the
  232. 7:03dereference operator
  233. 7:05so let's see this actually piece of code
  234. 7:07let's do this so
  235. 7:08instar p and x what do p and x contain
  236. 7:13garbage exactly right nothing we have no
  237. 7:15idea okay
  238. 7:18x equals three well now i know x is box
  239. 7:20x's value is three
  240. 7:21if i say p equals address of x what i
  241. 7:24love about this picture here on the
  242. 7:26right
  243. 7:26is that i don't actually care where x
  244. 7:28lives you shouldn't need to care where x
  245. 7:30lives wherever x lives
  246. 7:31p now can get to it that's what's really
  247. 7:33nice so it'll be the address i don't
  248. 7:34really care what it is
  249. 7:35is it 5 is it 27 is it a million who
  250. 7:38cares it just knows that p
  251. 7:39can connect to x and get its value and
  252. 7:41write x's value for it it's kind of nice
  253. 7:43it's like giving some of your key to
  254. 7:44your house they can now get in and do
  255. 7:45your thing there
  256. 7:47pretty cool how do you get the value of
  257. 7:49it here's how you get the value of it so
  258. 7:52i just showed you this on the previous
  259. 7:53slide there's actually a line of c code
  260. 7:54p points to
  261. 7:55and now star percent d says treat it as
  262. 7:57a signed integer
  263. 7:58and say go follow it and say it's going
  264. 8:01to pray 3 because star p
  265. 8:02is now kind of the same as x so if you
  266. 8:04see star p you can kind of think of it
  267. 8:06as x
  268. 8:06actually the same way kind of neat and
  269. 8:09again as i mentioned before the star is
  270. 8:10being used in two different ways here
  271. 8:12it's being used both to declare
  272. 8:13p as a pointer to an integer and it's
  273. 8:15being used to de-reference it as well
  274. 8:19so how to change a value a variable
  275. 8:21pointed to well you can use the
  276. 8:22reference in front of the equal sign
  277. 8:24so here's this p and now star p equals
  278. 8:27five and this is i mean i'm going slowly
  279. 8:29through something i kind of said before
  280. 8:30but guess what now x's value got five
  281. 8:34because i followed that this is the
  282. 8:35example of having star p on the left
  283. 8:36hand side of the equal sign
  284. 8:38nothing special there so now we can use
  285. 8:40pointers let's start with something
  286. 8:41really cool
  287. 8:42i want to add one i have y here at the
  288. 8:45higher level maybe it's at the main
  289. 8:46level or global level
  290. 8:48probably in main i'll just say i'm in
  291. 8:49main and i've got y bound is three
  292. 8:52and now i got this add one guy and i
  293. 8:54want to change what y is
  294. 8:55so i'm going to call add one of y and i
  295. 8:57want y to have a new value
  296. 8:59does i have a new value let's see is y4
  297. 9:03yeah y still three why is that
  298. 9:07because c and java are passed by value
  299. 9:10the system says oh here's add one of y
  300. 9:13the system says
  301. 9:14hey why what's your value y says i'm
  302. 9:16three before you called me i mean in the
  303. 9:18call right before add one
  304. 9:20i was three so three and only three gets
  305. 9:23transferred
  306. 9:24to x x has no connection with y
  307. 9:27so now x is three local variable x is
  308. 9:29three
  309. 9:30x the local variable x becomes four
  310. 9:33when it's all done y doesn't change that
  311. 9:36x change the two different guys and y
  312. 9:38stays the same
  313. 9:39so i got to figure out a way boy i wish
  314. 9:41i knew a way
  315. 9:42using the things i just kind of learned
  316. 9:44a slight slide or two ago
  317. 9:45to change y at the top level by a
  318. 9:48subroutine add one
  319. 9:50boy i wonder how to change add one hmm
  320. 9:54pointers pointers so now watch this
  321. 9:58and hit watch watch and learn watch and
  322. 10:00learn
  323. 10:02add one i don't pass in y because that
  324. 10:04just passes in the three i want to pass
  325. 10:06in the address remember the 123 whatever
  326. 10:08the address of
  327. 10:09of y is i want to pass that in and i
  328. 10:11have to declare the argument of add1 to
  329. 10:13be a pointer
  330. 10:14and now it's receiving the address so
  331. 10:16now in fact the two guys
  332. 10:17i have two i have y and i have a pointer
  333. 10:20but it's in the subroutine now
  334. 10:22so my subroutine can get access to y and
  335. 10:24by saying star p equals star p plus one
  336. 10:26grabs the value of y it was three it got
  337. 10:29to four plus one there's four
  338. 10:30four stuffs it back into y follows the
  339. 10:33pointer
  340. 10:33stuffs it back into y look at that int y
  341. 10:37is 3 at the end of that program y is now
  342. 10:394.
  343. 10:40do a little shimmy do a little shimmy do
  344. 10:41a little shimmy pretty cool
  345. 10:43all right so that's how you can change a
  346. 10:45value at the top level
  347. 10:46by using a subroutine but you got to use
  348. 10:48pointers for that pretty cool
  349. 10:51danger danger will robinson so the
  350. 10:54problem
  351. 10:55that you might have guessed is that that
  352. 10:57whole initialization to
  353. 10:59garbage thing is gonna come up and give
  354. 11:00us trouble because
  355. 11:02local variables are not initialized
  356. 11:04they're garbage right so look at this
  357. 11:06in star p here's a box right pointer oh
  358. 11:09sorry instar pointer
  359. 11:10here's a box pointer what's inside of it
  360. 11:15i don't know nothing garbage which means
  361. 11:18it's like a live wire imagine like a you
  362. 11:20know power line comes down it's all
  363. 11:23it's pointing to anything it's anywhere
  364. 11:25in memory
  365. 11:26and what do you do with the next line
  366. 11:28star pointer equals five
  367. 11:30you're taking five and following that
  368. 11:32line pointing anywhere and stuffing five
  369. 11:33in it
  370. 11:34you don't have any control of that
  371. 11:35member you don't have right privilege to
  372. 11:37write into some part of memory
  373. 11:38that is a big trouble that's that's
  374. 11:40clearly an error
  375. 11:42uh and so that is a very common error
  376. 11:44for beginning programmers
  377. 11:45that they don't set pointer to point to
  378. 11:47something they just kind of follow it
  379. 11:48and we're going to see a video i'm going
  380. 11:49to
  381. 11:50link you to a wonderful video from my
  382. 11:51colleague nick parlante at stanford who
  383. 11:53made this outstanding
  384. 11:54binky pointer video they're going to see
  385. 11:55this after this
  386. 11:57so i showed you a lot of danger
  387. 12:01i showed you some cool power right you
  388. 12:02can now change the variable up in main
  389. 12:05in a subroutine you couldn't do that
  390. 12:06before if you didn't have pointers
  391. 12:07that's kind of cool
  392. 12:08so here's another reason we use pointers
  393. 12:11i got a big struct remember i showed you
  394. 12:13how to use structure a couple of videos
  395. 12:14ago so i got this big structure a lot of
  396. 12:15data big arrays
  397. 12:16i haven't talked about arrays yet but a
  398. 12:17lot of data and i want to
  399. 12:20pass it to a subroutine to process do i
  400. 12:23copy all the values and give it to you
  401. 12:25isn't it a little cleaner to literally
  402. 12:27just pass
  403. 12:28a pointer now this big thing only has to
  404. 12:30be stored once in memory
  405. 12:32and i pass a link to kind of a link to
  406. 12:34it a pointer to it and now you can be
  407. 12:35reading and writing to it and doing work
  408. 12:37on it averaging it all
  409. 12:38playing with it modifying it whatever
  410. 12:40you want to do normalizing it
  411. 12:41i don't have to pass all the data at
  412. 12:43once for you to do that so it's really
  413. 12:44clean like in one here's a blue here's a
  414. 12:46very lightweight
  415. 12:47there's a left or thin lether thin
  416. 12:50pointer and i give it to you and then
  417. 12:51the subroutine does that
  418. 12:52great really nice okay so in general
  419. 12:56pointers are
  420. 12:56everybody works in c does anything
  421. 12:58that's not kind of non-trivial it's
  422. 13:00dealing with pointers and so you need to
  423. 13:01be able to be fluent in that
  424. 13:02and make sure you're not copying huge
  425. 13:04amounts of data when you call that
  426. 13:05drawbacks so we're going to see
  427. 13:06drawbacks i talked about bugs and
  428. 13:08pointers as bugs and pointers and bugs
  429. 13:09and pointers we're going to show about
  430. 13:11um i feel like every other slide is
  431. 13:13here's how you can shoot yourself in the
  432. 13:14foot with pointers
  433. 13:15like it's just really hard memory
  434. 13:17management and pointers are really hard
  435. 13:18but they're really powerful i mean
  436. 13:20having full access to the raw it's like
  437. 13:22as i mentioned before with my analogy
  438. 13:23take all the you know the grill stuff of
  439. 13:25her car and i got an engine and that and
  440. 13:27wow look how fast this is they can tweak
  441. 13:28it in here and add some carburetor and
  442. 13:30add some nitrous
  443. 13:31but you can put your finger lose your
  444. 13:32finger too and that's really what c is
  445. 13:33like see it's just like an
  446. 13:34engine it's like a car without any
  447. 13:36protection of anything it's like the
  448. 13:37road is
  449. 13:38zoom zoom like your feet are just if you
  450. 13:39put your feet down it'll be
  451. 13:41bad okay so imagine a car crazy fast
  452. 13:44crazy cup you know
  453. 13:45um ability to make any change what if
  454. 13:47you know what you're doing
  455. 13:49if you don't it can be really dangerous
  456. 13:51really really no seat belt that's just
  457. 13:52it
  458. 13:52it's wide open you've seen these cars
  459. 13:54i've seen everyone you see a car that
  460. 13:55has like three wheels
  461. 13:56i don't know i'm going rogue and it's
  462. 13:58like the guy's sitting at a motorcycle
  463. 13:59but it's a front two-wheel car in the
  464. 14:00back and it's just under it's like it's
  465. 14:02this close to the road it's a little too
  466. 14:03dangerous to me flipped over there's no
  467. 14:05roll bar
  468. 14:05okay that's kind of what it's like but
  469. 14:07even worse without any kind of
  470. 14:09grill or anything protecting you all
  471. 14:10right having some fun we'll see at the
  472. 14:12next video

About this transcript

This page contains the full transcript of [CS61C FA20] Lecture 04.1 - C Intro: Pointers, Arrays, Strings: Pointers and Bugs by CS 61C Departmental, generated from the public captions YouTube serves with the video. The transcript has 3,048 words across 472 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.