[CS61C FA20] Lecture 04.1 - C Intro: Pointers, Arrays, Strings: Pointers and Bugs — Transcript
Full transcript
- 0:00and welcome back now let's talk about
- 0:03what a pointer is
- 0:05exciting we titled this bugs and
- 0:07pointers because really
- 0:08that's what this is the source of so
- 0:11many bugs i just
- 0:12it's so sad to share with you this is
- 0:14like giving you a very
- 0:15sharp edge thing with the blade and then
- 0:18cut and don't cut yourself well you got
- 0:20to cut yourself and
- 0:20you can have bugs thanks to pointers so
- 0:22pointers are the reason for this whole
- 0:23thing
- 0:24but they're powerful we'll talk about
- 0:25that the last slide we'll talk about why
- 0:26we use pointers in the first place
- 0:28so more about variable declaration
- 0:30variable declaration
- 0:31so mostly it's similar to java except
- 0:33that java
- 0:35declares variables and that gives them a
- 0:38default value that makes sense for that
- 0:39variable
- 0:40if the number gives it 0 probably not in
- 0:43c c
- 0:44when you make a new variable without
- 0:45giving it a value garbage
- 0:48i'll say it again garbage it has no idea
- 0:50it's a box
- 0:51and it's unknown inside that's the
- 0:53source of a ton of problems so
- 0:55you have to always make a new variable
- 0:57and then initialize it before you can
- 0:58use it you could have used it in java
- 1:00and you might have gotten lazy and just
- 1:01started using oh there's zero
- 1:02i make a new counter it would probably
- 1:04default to zero let me now you know as
- 1:06accumulator let me
- 1:07add numbers to it no you've got to
- 1:09declare something if it's garbage in
- 1:10and just keep adding some numbers into
- 1:12it it's going to be continued to be
- 1:12garbage so you got to make sure this
- 1:14so here's some example declarations you
- 1:17used to have to declare things at the
- 1:18top
- 1:18so int a is b a is 0 b is 10.
- 1:22in ansi c you couldn't do this in 1986
- 1:25you couldn't do this for
- 1:26int i is one that's a really standard
- 1:28thing to do in java you can't do that
- 1:29here
- 1:30but you can do it in c99 and beyond you
- 1:32can just say four into i
- 1:33zero so i kind of mentioned that before
- 1:34but it's useful to know
- 1:37so undefined behavior undefined behavior
- 1:40in c means
- 1:42it's unpredictable it might run 100
- 1:44times on one computer and then 100 first
- 1:46time doesn't work
- 1:47or it might be great in this computer
- 1:48the same code don't touch it i compile
- 1:50it for this computer
- 1:51and it doesn't work or i upgrade the os
- 1:53and the program doesn't work anymore
- 1:54because of a new library change
- 1:56all these things are undefined behavior
- 1:58and a lot of these things are really
- 1:59challenging to find
- 2:01things that are undefined things are
- 2:03kind of uh random hardware produced
- 2:05seems like evade you
- 2:06we call them heisenbugs if you haven't
- 2:08heard that you start using that word
- 2:10heisenbugs heisenberg's uncertainty
- 2:12principle so the quantum
- 2:13quantum physics so think about the idea
- 2:17that you want to avoid that you want to
- 2:18avoid that space and have
- 2:20actually you want to have repeatable
- 2:21bugs bugs are repeatable we can find
- 2:23them right i just logically trace it
- 2:24down and i can find them
- 2:25those are called boar bugs so
- 2:27heisenberg's bad hard to trace and hard
- 2:29to find and may never even show up until
- 2:31the millionth time
- 2:32you run it after you ship the code out
- 2:34that's really hard
- 2:35it's really tough for you as a as a
- 2:37software developer
- 2:38board bugs easy you can trace it you
- 2:40test it and test you test it
- 2:41so you find these board bugs board bugs
- 2:42are easy to find
- 2:44okay pointers the first big
- 2:48uh duality you have is an address
- 2:51versus a value okay um
- 2:54for now we're gonna live in an abstract
- 2:56world in which all of your memory you're
- 2:58gonna have access to and be able to
- 2:59control
- 3:00is one really big array an infinitely
- 3:03big array
- 3:04starts at zero and goes up to some value
- 3:07that's it
- 3:08some goes up to some address
- 3:10relationships i should i should use it
- 3:12use the right terms
- 3:13goes up to some address each cell
- 3:16each cell has a value the guy inside the
- 3:19cell and has an address the address of
- 3:21the cell like a house house has an
- 3:22address and has as a contents
- 3:23has the value okay because we start from
- 3:26zero
- 3:27does it make sense to use int's signed
- 3:29or unsigned numbers
- 3:31as your as your addressing mode you're
- 3:33right it doesn't make sense to use a
- 3:35signed number because you don't have
- 3:36negative
- 3:37addresses so don't confuse an address or
- 3:40the value
- 3:41but now think of it as an infinitely
- 3:43large array everything's free and wild
- 3:46it'll get more constrained and more
- 3:47painful later but for now the free and
- 3:48wild and
- 3:49the 60s free and wild everything's great
- 3:52infinite array go crazy
- 3:54here's an example this picture uh this
- 3:56picture here on the right if i can show
- 3:58you let me if i can get this uh pen to
- 4:00work
- 4:01these are addresses 101 305.
- 4:06these are your values 104. so the house
- 4:10at 104
- 4:11holds the value 23. okay so for now
- 4:14understand that
- 4:15i have another house holding 42
- 4:17somewhere longer down there okay
- 4:21a pointer a pointer
- 4:25is a cell whose value
- 4:28is somebody else's address that's simple
- 4:32that's it simple as that okay pointer we
- 4:35say
- 4:36points to that address so we'll actually
- 4:38have a picture for that
- 4:40so there's my locations up top 101 305
- 4:43my names let me even have some names
- 4:45there x and y
- 4:46x what's x's address 104 what's x's
- 4:50value
- 4:5023. i'm going to make a new guy well
- 4:53i have a new i have a new variable i
- 4:56don't know where it lives i don't know
- 4:57its address
- 4:59but for p to point to x
- 5:02and to have access to x means what
- 5:05should be in p's
- 5:06cell anybody anyone anyone bueller
- 5:09bueller that's right 104. so if i
- 5:12point if p points to x it means that x's
- 5:15address is in p
- 5:16cell or is p's value okay not too hard
- 5:21here's some syntax so the way i declare
- 5:23and tell c
- 5:24that p is going to be a pointer it's
- 5:27going to be
- 5:28it's an integer it's a pointer to an
- 5:30integer that's what it says
- 5:31it says it i'm now going to hold a box
- 5:34and the box the
- 5:35i'm going to have i'm going to have a
- 5:37value inside of i'm going to live
- 5:38somewhere
- 5:39i'm going to value inside of me that
- 5:40value if i were to follow it is going to
- 5:42be an
- 5:43integer in that location that's what
- 5:44that says p points to an integer
- 5:47the next line says that p equals address
- 5:50of y
- 5:51i i say don't you say ampersand no p
- 5:54equals address of y read that as address
- 5:57of y
- 5:57p now if p hat by the way the first line
- 6:00what is that what's the goal what's p
- 6:01what's p store after that first line
- 6:04garbage it's a box
- 6:05it had garbage inside remember that the
- 6:07line ago it had garbage in that side of
- 6:08that box i showed it as blank it's
- 6:10really garbage
- 6:11p equals adder so y says ah p is going
- 6:13to now
- 6:14get the address of y and put it in p's
- 6:17value
- 6:18now how do you use it well
- 6:21you star has this kind of overloaded
- 6:24idea
- 6:24so if you're declaring it star says to
- 6:26the c compiler this is i'm going to make
- 6:28this a pointer
- 6:29but if i'm using it and i want to get i
- 6:31say i got pointer p
- 6:32i want to follow the pointer to get
- 6:34whatever value is
- 6:36behind it then you're going to say star
- 6:37p again we're going to say star p there
- 6:39and by the way that star p
- 6:40could be on the left side or the right
- 6:41hand side of the equal sign it's on the
- 6:42right hand side of the equal sign as it
- 6:44is here
- 6:44it's getting the value out of it and
- 6:45storing in z you could have swapped that
- 6:47and said star p
- 6:48equals e would you take z value across
- 6:50the equal sign and stuff it in wherever
- 6:52p
- 6:52not into p whatever p is pointing to
- 6:55okay which is here why why would it got
- 6:57a new value
- 6:58why would have gotten z out of that okay
- 7:00by the way it's called dereferencing
- 7:01that's really important that's the
- 7:03dereference operator
- 7:05so let's see this actually piece of code
- 7:07let's do this so
- 7:08instar p and x what do p and x contain
- 7:13garbage exactly right nothing we have no
- 7:15idea okay
- 7:18x equals three well now i know x is box
- 7:20x's value is three
- 7:21if i say p equals address of x what i
- 7:24love about this picture here on the
- 7:26right
- 7:26is that i don't actually care where x
- 7:28lives you shouldn't need to care where x
- 7:30lives wherever x lives
- 7:31p now can get to it that's what's really
- 7:33nice so it'll be the address i don't
- 7:34really care what it is
- 7:35is it 5 is it 27 is it a million who
- 7:38cares it just knows that p
- 7:39can connect to x and get its value and
- 7:41write x's value for it it's kind of nice
- 7:43it's like giving some of your key to
- 7:44your house they can now get in and do
- 7:45your thing there
- 7:47pretty cool how do you get the value of
- 7:49it here's how you get the value of it so
- 7:52i just showed you this on the previous
- 7:53slide there's actually a line of c code
- 7:54p points to
- 7:55and now star percent d says treat it as
- 7:57a signed integer
- 7:58and say go follow it and say it's going
- 8:01to pray 3 because star p
- 8:02is now kind of the same as x so if you
- 8:04see star p you can kind of think of it
- 8:06as x
- 8:06actually the same way kind of neat and
- 8:09again as i mentioned before the star is
- 8:10being used in two different ways here
- 8:12it's being used both to declare
- 8:13p as a pointer to an integer and it's
- 8:15being used to de-reference it as well
- 8:19so how to change a value a variable
- 8:21pointed to well you can use the
- 8:22reference in front of the equal sign
- 8:24so here's this p and now star p equals
- 8:27five and this is i mean i'm going slowly
- 8:29through something i kind of said before
- 8:30but guess what now x's value got five
- 8:34because i followed that this is the
- 8:35example of having star p on the left
- 8:36hand side of the equal sign
- 8:38nothing special there so now we can use
- 8:40pointers let's start with something
- 8:41really cool
- 8:42i want to add one i have y here at the
- 8:45higher level maybe it's at the main
- 8:46level or global level
- 8:48probably in main i'll just say i'm in
- 8:49main and i've got y bound is three
- 8:52and now i got this add one guy and i
- 8:54want to change what y is
- 8:55so i'm going to call add one of y and i
- 8:57want y to have a new value
- 8:59does i have a new value let's see is y4
- 9:03yeah y still three why is that
- 9:07because c and java are passed by value
- 9:10the system says oh here's add one of y
- 9:13the system says
- 9:14hey why what's your value y says i'm
- 9:16three before you called me i mean in the
- 9:18call right before add one
- 9:20i was three so three and only three gets
- 9:23transferred
- 9:24to x x has no connection with y
- 9:27so now x is three local variable x is
- 9:29three
- 9:30x the local variable x becomes four
- 9:33when it's all done y doesn't change that
- 9:36x change the two different guys and y
- 9:38stays the same
- 9:39so i got to figure out a way boy i wish
- 9:41i knew a way
- 9:42using the things i just kind of learned
- 9:44a slight slide or two ago
- 9:45to change y at the top level by a
- 9:48subroutine add one
- 9:50boy i wonder how to change add one hmm
- 9:54pointers pointers so now watch this
- 9:58and hit watch watch and learn watch and
- 10:00learn
- 10:02add one i don't pass in y because that
- 10:04just passes in the three i want to pass
- 10:06in the address remember the 123 whatever
- 10:08the address of
- 10:09of y is i want to pass that in and i
- 10:11have to declare the argument of add1 to
- 10:13be a pointer
- 10:14and now it's receiving the address so
- 10:16now in fact the two guys
- 10:17i have two i have y and i have a pointer
- 10:20but it's in the subroutine now
- 10:22so my subroutine can get access to y and
- 10:24by saying star p equals star p plus one
- 10:26grabs the value of y it was three it got
- 10:29to four plus one there's four
- 10:30four stuffs it back into y follows the
- 10:33pointer
- 10:33stuffs it back into y look at that int y
- 10:37is 3 at the end of that program y is now
- 10:394.
- 10:40do a little shimmy do a little shimmy do
- 10:41a little shimmy pretty cool
- 10:43all right so that's how you can change a
- 10:45value at the top level
- 10:46by using a subroutine but you got to use
- 10:48pointers for that pretty cool
- 10:51danger danger will robinson so the
- 10:54problem
- 10:55that you might have guessed is that that
- 10:57whole initialization to
- 10:59garbage thing is gonna come up and give
- 11:00us trouble because
- 11:02local variables are not initialized
- 11:04they're garbage right so look at this
- 11:06in star p here's a box right pointer oh
- 11:09sorry instar pointer
- 11:10here's a box pointer what's inside of it
- 11:15i don't know nothing garbage which means
- 11:18it's like a live wire imagine like a you
- 11:20know power line comes down it's all
- 11:23it's pointing to anything it's anywhere
- 11:25in memory
- 11:26and what do you do with the next line
- 11:28star pointer equals five
- 11:30you're taking five and following that
- 11:32line pointing anywhere and stuffing five
- 11:33in it
- 11:34you don't have any control of that
- 11:35member you don't have right privilege to
- 11:37write into some part of memory
- 11:38that is a big trouble that's that's
- 11:40clearly an error
- 11:42uh and so that is a very common error
- 11:44for beginning programmers
- 11:45that they don't set pointer to point to
- 11:47something they just kind of follow it
- 11:48and we're going to see a video i'm going
- 11:49to
- 11:50link you to a wonderful video from my
- 11:51colleague nick parlante at stanford who
- 11:53made this outstanding
- 11:54binky pointer video they're going to see
- 11:55this after this
- 11:57so i showed you a lot of danger
- 12:01i showed you some cool power right you
- 12:02can now change the variable up in main
- 12:05in a subroutine you couldn't do that
- 12:06before if you didn't have pointers
- 12:07that's kind of cool
- 12:08so here's another reason we use pointers
- 12:11i got a big struct remember i showed you
- 12:13how to use structure a couple of videos
- 12:14ago so i got this big structure a lot of
- 12:15data big arrays
- 12:16i haven't talked about arrays yet but a
- 12:17lot of data and i want to
- 12:20pass it to a subroutine to process do i
- 12:23copy all the values and give it to you
- 12:25isn't it a little cleaner to literally
- 12:27just pass
- 12:28a pointer now this big thing only has to
- 12:30be stored once in memory
- 12:32and i pass a link to kind of a link to
- 12:34it a pointer to it and now you can be
- 12:35reading and writing to it and doing work
- 12:37on it averaging it all
- 12:38playing with it modifying it whatever
- 12:40you want to do normalizing it
- 12:41i don't have to pass all the data at
- 12:43once for you to do that so it's really
- 12:44clean like in one here's a blue here's a
- 12:46very lightweight
- 12:47there's a left or thin lether thin
- 12:50pointer and i give it to you and then
- 12:51the subroutine does that
- 12:52great really nice okay so in general
- 12:56pointers are
- 12:56everybody works in c does anything
- 12:58that's not kind of non-trivial it's
- 13:00dealing with pointers and so you need to
- 13:01be able to be fluent in that
- 13:02and make sure you're not copying huge
- 13:04amounts of data when you call that
- 13:05drawbacks so we're going to see
- 13:06drawbacks i talked about bugs and
- 13:08pointers as bugs and pointers and bugs
- 13:09and pointers we're going to show about
- 13:11um i feel like every other slide is
- 13:13here's how you can shoot yourself in the
- 13:14foot with pointers
- 13:15like it's just really hard memory
- 13:17management and pointers are really hard
- 13:18but they're really powerful i mean
- 13:20having full access to the raw it's like
- 13:22as i mentioned before with my analogy
- 13:23take all the you know the grill stuff of
- 13:25her car and i got an engine and that and
- 13:27wow look how fast this is they can tweak
- 13:28it in here and add some carburetor and
- 13:30add some nitrous
- 13:31but you can put your finger lose your
- 13:32finger too and that's really what c is
- 13:33like see it's just like an
- 13:34engine it's like a car without any
- 13:36protection of anything it's like the
- 13:37road is
- 13:38zoom zoom like your feet are just if you
- 13:39put your feet down it'll be
- 13:41bad okay so imagine a car crazy fast
- 13:44crazy cup you know
- 13:45um ability to make any change what if
- 13:47you know what you're doing
- 13:49if you don't it can be really dangerous
- 13:51really really no seat belt that's just
- 13:52it
- 13:52it's wide open you've seen these cars
- 13:54i've seen everyone you see a car that
- 13:55has like three wheels
- 13:56i don't know i'm going rogue and it's
- 13:58like the guy's sitting at a motorcycle
- 13:59but it's a front two-wheel car in the
- 14:00back and it's just under it's like it's
- 14:02this close to the road it's a little too
- 14:03dangerous to me flipped over there's no
- 14:05roll bar
- 14:05okay that's kind of what it's like but
- 14:07even worse without any kind of
- 14:09grill or anything protecting you all
- 14:10right having some fun we'll see at the
- 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.