Updates: Tyler on PowerHacking CSS to cutestrap your app in this latest episode!

Topic 7 - Dynamic Memory and Structs

Outline:

  • a short intro to float: size, %f, casting / coercion
  • structs for related data in a record
    • composite data structures, defining them vs. declaring a type
    • the . operator for field access
    • sizes of structs may be padded
      • example of a struct with char and int, sizeof
      • first gcc compiler directive: __attribute__((packed))
    • working with planet data and printing out a struct by reference
  • struct fields as record offsets in memory
  • computer memory - storing data vs. storing a memory address (pointer)
  • passing pointers to functions
    • address of memory (&) and dereference / lookup memory *
    • writing a function to take a pointer and modify the original
    • (re-)introduction to passing by reference / value
      • modifying data outside of the stack frame via pointer
      • pointers don’t require much data copy
  • structure dereference: (*). vs. ->
    • the “pew pew” operator
  • Data for today from NASA, (download, CSV 130 bytes):
Mercury,87.969
Venus,224.701
Earth,365.256
Mars,686.98
Jupiter,4332.59
Saturn,10759.22
Uranus,30688.5
Neptune,60182.0
Pluto,90560

Transcript:

Hello everybody!
How are you! We are back
for learning on mondays this is CSG
learn
c as an additional language with the
Enceladosaurus who should be on the
line
are you hi everyone yeah she is all
right so
uh we took a little break we had the
holiday
last week but we're back at it and we're
not programming in python
because we want to manage our own memory
that
that is what we want to do because it's
painful and
we're going to enjoy doing it so um
i have already told don't even bother to
say it i've already told her that
you really don't want to manage memory
but tonight we are going to manage
memory we're going to do it boldly
like colors of blue
that are sentient okay and hello what's
up lophone and fafi how are all of you
as long as seven one five
um yeah well let's get into it we have a
lot of material to get through tonight
so i think
um i'm gonna cut over to
whiteboard mode and i oh
and i need the glove too because we're
going to be drawing
all right oh yeah so
where are we we are let's see we're
c-a-a-l
lots of a's c has an additional language
topic seven and this we're going to do
structs and pointers so
structs will be like the relaxing part
of the programming and pointers will be
the very not relaxing part of the
programming
so it took a couple rolls to get that
eight sorry about that but
maybe that was the dye knew what what
jesse had in store for
i don't know we're gonna find out so um
great what is a struct you have them in
python
but you might never have used them have
you
i don't think so okay they're they're
kind of obscure in python so i'm just
gonna
pretend that you didn't use it it does
exist but it's kind of it's kind of
weird it's mostly for c binding anyway
it's not
generally used in the language so struct
is short for
structure and it's a
structure of data so this is the
beginning of you can think of this as
kind of like
aggregated types
so if you have like an int
and you have like a care pointer
and they happen to be sort of related to
each other
maybe it helps to think of them as the
same thing
that's very descriptive so let's let's
group these so like if we have kind of
like grouped data
that's kind of what a struct is like at
a high level so um
let's say for example that this is like
uh
i don't know this is height and
this is which i don't know why that'd be
an int but
and then maybe this is like a name so
this would be like
oops a person's name and a height
all right so we want to group these
together because they're related they're
they're the same record of data
and we don't want to treat them
separately like we don't want to keep a
table of names and a table of heights
because we want to know that when we're
talking about one person
we have a height and a name
all right well c gives us a convenient
way to do that it's called a struct
so we can say struct and we can define
what the
type is so in this case i'm building a
person type
and it uses a brace and that's a really
sloppy brace let me
find objects it's very much like an
object in fact
objects directly come from structures uh
a object is quite like in the c plus
sense is quite literally
a structure that has methods as well
so i can think of this i can let's say
that we just build like a care
we're going to pretend we're going to
break all the rules of names
and say that they're always 10
characters long
which is absolutely not true and there's
a great post that i'll show you later
about it
but um and then we're gonna say that
like you know this this height here
is uh also going to be related
so i've named these fields just sort of
like using my standard like declaration
syntax
except this is not actually allocating
memory this is just saying
these are the parts of a person struct
makes sense um and so yeah so somebody
in chat actually brings up a question
that i had as well which is
can you implement methods on structs in
c or no
so the problem i mean first of all c
plus is implemented in c
so yes is the answer because anything
you can build in c plus plus you could
have built in c in the first place
um the way they are generally built the
first thing you need is you need
something called a function pointer
um which is basically a
pointer to a function so if i have like
a function that's like you know like
in you know thing
and you know it's let's say it takes no
arguments
then i can build a pointer to that
and it's going to be the same type so
this is going to be like uh
this this is getting kind of screwed
let's not get i don't want to
screw jesse up that much yes but yeah
yes is the answer
so um once you have the notion of a
function pointer then you can just stick
a function pointer in here and that's
exactly how a method is generally
implemented it uses something a
technique called the v
table in c plus plus but basically in c
absolutely you could do that um so
we're not going to do that now that's
that's getting a little advanced we're
just we're just going with the basic
stuff
okay so i define this struct person and
then what i want to do is i want to
actually like
allocate one so this so far all i've
really done
is define oh yeah let's get our palette
back here
all i've done is define what goes in it
so i haven't actually built one so say
that i'm inside like
main and i've got all my you know main
things and you know i'm i'm in here
and i'm doing it and i'm returning it in
because i'm a good person and
okay so now i in here i'm going to use
this as my
own data type so the data type for the
declaration is going to be struct
whatever i called it so if i called it
person then i got to call it person here
and then this is going to be person a
so i'm going to call this variable pa
all right not not too big a jump
basically i've just defined
this is a custom data type and it's made
up of
these two things so the compiler can
turn that into a chunk of memory it's
going to be 10 of these
and it's going to be whatever the size
of this thing is depending on my
platform so
this would be something like 32 bits and
this would be something like
10 times 8 bits
okay unfortunately and we're going to
get to that in a moment this isn't
exactly true but we start with the
simple model first
all right so does this make sense so far
before i
get to the real fun part
tentatively yes okay so basically i've
just defined
kind of a convenient way it's kind of
like a class without methods
you could think of it so so
then i go and i take my person and i
need to actually set up the variables
so in this case i've got height
so pa.height and
i use this this dot to refer to a part
of the structure
so this is just a structure variable and
this is saying okay i want to talk about
like
this some part of the structure variable
and then this is the part that i'm going
to refer to
so i have a pa dot height and i can set
it to
you know i don't know 10 or something
and
then i've got like a pa dot name
and i'm gonna need to copy something in
there because
it's a character array so i'm gonna have
to use which function
do we remember our c copy function
oh gosh i'm sorry it's okay it's been a
while it's
str copy so um
and i'm gonna basically pass along that
this is gonna be like
pa.name and
what i want to put in there so this is
this is j
j s
alright so that will set up my structure
with my two types now if i try to print
this thing out
c is going to be like i don't know how
to do that like this is a structure you
you gotta you gotta tell me it'll be
like what
so you're gonna have to unroll that
yourself
right i mean c really only knows how to
deal with these
kind of like basic types this sort of
like concatenated like this aggregated
type this is kind of up to you
but c does give you a way to refer to
the parts of it which is this dot
operator
okay so that makes sense oh
they're saying that uh you're about half
as loud i'm sorry i missed that
uh let me crank up your volume yeah no
worries
all right is that better chat
hi friends let us know can you hear me
already
coming through hopefully okay i am
projecting i promise
all right oh i'm just really loud okay
i'll turn mine down a little bit
maybe that'll be better all right
uh yeah just get oh and hey what's up
i'm not gonna pronounce his name because
it's
uh naughty but anyway um
it's naughty okay sorry
not what i expected tmd uh
i'll call him tmd there you go
all right um so uh it's naughty in other
languages all right so
we know everything about structs right
yes good now if i go and i
clear all of this fun stuff because
that's life um then and i want to go and
i want to create a new structure so
give me give me something else let's
let's how about we work with planets
yeah i like that all right so by the way
i'm including
pluto so if you have issues with it you
should
okay there's your official astronomer
endorsement that's good good good good
we're on the same page i was going to
say you're
you're getting a big fat f
okay um and uh
i prepared a little bit of a data
structure which i thought we could
probably use for like the previous uh
csv too i had to post on the website but
um
so let's say that we want to have uh a
name again
and we want to have the period so how
long
how many days earth days does it take
for it to go around the sun
and um i've got a handy table available
that i'll just drop and
you can see discord right oh
you don't yes i can yeah no i can pull
it up before yeah there we go yep i can
pull it up
i can also put it i don't know if it's
gonna work in
twitch but i'll just i'll just drop it
in the learn channel here
so this will be our data set for tonight
and i went and pulled this off
nasa because nasa
back in the space game again all right
whoops editorial there so
great uh we have our little fact sheet
the link by the way for chat just in
case you're not in the discord is
this um so
what do we need to represent this data
so we're gonna need um
a string right for the name and then
uh and well
end for the well except it's not in it
right
well we haven't really talked about
floats oh well let's talk about floats
okay all right the data type is float
all right well there you go we talked
about floats good job
see there was like sort of a hand wavy
like oh i i'm not going to get into the
bit pattern for them yet well that that
that's definitely like ieee 754 is
definitely later
but no worries if they exist they exist
and then yes we want floats
yeah the data all right so you would
define this how would you define this
walk me through this
what do you mean how would i define it i
mean so what needs to go into this
struct
so we need i think that the two things
that i just specified right so yeah so
give me give me some c um oh lord i'm
not sure i feel
comfortable oh that's fine i'll do it
i'll walk you through it you're not
actually on the coding terminal yet so
you still get to
yeah there we go so all right i think i
need an example first that's totally
fine so i mean strings are going to be
what basic data type
care yeah exactly i'm i'm going to make
a nice big assumption that all planets
so that we're going to call this the
name i guess
this this record um i'm gonna make an
assumption that all of them are less
than 20 characters so let's
let's allocate 20 characters because
we're going to use a fixed size thing
and um oh and hey what's up nathaniel oh
and by the way nathaniel we need we do
need to talk about
enterprise um season four i i came
around to your
way of looking at things but we'll we'll
get back
oh i was definitely i was definitely
have you gotten to season four
it's already on the list because i had a
feeling that nathaniel might come by
okay um so that's going to be our name
well what else do we need in a planet
um so we just talked about orbit uh yeah
so what do you want to call that the
float thing
float yeah you mean the name of the
float
yeah yeah we have to give the record
each each of these like elements needs a
name
the period is generally what it's called
uh so
that's good let's call it period i would
worry about that just because it might
be semantically confusing with an actual
period since we're going to or o like o
period yeah at the orbital here is that
is that okay
good um i i just want to make it because
we're going to
literally use that in syntax so it might
be confusing
all right so we're going to define that
as well so now we've named the parts of
the struct
and we're gonna close it and we're gonna
throw a line terminator on it
this terminator is a little bit weird
but it has to be there
so okay it's okay you're gonna forget i
forget all the time it's just one of
those things it's not a function
like normally when you're using these
braces it's kind of like in a function
context or some sort of block context
no this is actually just a multi-line
statement so you really need to have
this
this okay terminator at the end okay
now if i want to define one of these so
i want to declare one so
again i'm inside my main
and i defined it correctly and i
the data type again is going to be the
name of the struct
so it's struct i have to use that word
planet
and then we have to give it the variable
name so let's just call this planet
x
okay and if i want to refer to the parts
of this struct
like for example set i want say i want
to set up mercury here in x
i need to do the the name copy which is
going to be str
copy and i need to say this is
x and i'm using this period which is why
i didn't want to use that word
dot name
and then i do the thing that i'm copying
over which is
in this case mercury
so these work pretty much the same as
normal variables i just have to use this
dot to refer to the right
part of it if that makes sense
yes i was just copying down with your
yes okay
and how do i i mean that's that's
familiar like that
makes sense to me because with classes
and stuff that's exactly where this is
exactly where they got the syntax
and then you know to set the o period
i'm going to do
x dot o period
and i'm just going to set it to whatever
the value is that i want in the case of
mercury i've got 87
points just round it off
nine seven all right so
that makes sense right okay and i can't
just print this thing out like because
i'd have to have
it would have to know how to print out
like the parts of it okay
um going back to our
oh and uh our uri ui
says c is not weird it's just print uh i
mean
weird i guess has a perspective
associated with it so if you're
not used to programming c it's probably
weird you could still do every
oh yeah and totally you can definitely
do anything you want with c
in fact most things are implemented in c
okay um so
you feel good about like you could set
up a few planets kind of this way
yeah i think so all right now do you
remember our operator to figure out how
big
a data structure is any any data type
size of you got it and
that applies to structs as well so i can
do a size of and i can say
struct planet
and this will actually tell me how big
this thing is
in memory and the thing that i'm going
to discover when i do that is
occasionally the size is not going to be
what i think
so sometimes i'm going to get like say
for example i do something which is like
a coordinate pair so let's say i make a
struct pair
and it's going to be like you know int x
and into y and i'll just write them out
as separate lines for
clarity how big do you think this
would be um
what's the size of one of these ins
what like is it 32 yeah typically it's
32 yeah
and so 64 is what i would expect 32 bits
so we would expect this to be 64 bits
which in bytes because size of returns
bytes
would be eight yeah eight so we would
expect to see eight
in this case you are gonna see eight
okay all right i was like oh god where's
the twist
now you remember how big like this is
right
sorry this being uh this being oh i'm
waiting to catch up with stream sorry
um sorry oh lord it's
care is smaller than int
so that one is 16. uh that's a short
you're thinking no it's eight it's eight
yeah there we go yeah this
this is this is eight bits and this is
typically 16. so
um these if i go and i put this
next to each other let's let's say we
did um
weird so let's let's make weird over
here
and it's going to have a care
for x and an inch
for y how big do you think this is so if
the types are different
something weird is going to happen not
always but for reason
i'm going to guess
because i'm guessing the answer is not
what i think it is no i'm going to start
with 30 things
well we think that this this interior is
what size
32 this is 32 what how big do we think
the care is
eight all right so theoretically 40
right
yes but i'm guessing that's not true
yeah well why don't we find out let's go
code
okay
all right well let's let's let's get a
size on that
okay um
all right so we're going to first want
to make
ah these are normally defined in global
scope
so i'm just going to grab this and put
it up here
alrighty and then oops
let me wait a thing yeah okay sorry
okay there we go and so then we're gonna
have what care
x xy
and then in maine we can go um
let's see yeah
nice you're labeling it
oopsies the flash all right um
my windows goofed on there we go all
right um
okay so there's a little warning here
because this is actually oh because it
needs to be long
yeah that's okay
okay what what what that's not 40 bits
and that's not even like i guess that it
might just like default to the larger
one
so i was like okay i mean now if we did
this
is that bigger or smaller than what we
just specified
wait i've what did you just change i
changed online form
so that should be larger yeah it should
be larger right but if i run this i get
it's eight again
wait but you said before it would be 64.
if we had two ends
are you lying well no it's 64. these are
bytes yeah yeah these are bytes
so no i didn't didn't lie so it's just
going to take whatever the largest type
is
and then give you that money for as many
variables as you have all right so if i
throw another one on right
like if i make this a three right and i
execute it
i got the expected 12 right right so
change that to care
yeah let's make that a character and i
think it's going to be the same it's
still
12 why okay is something wrong with the
size of a
character
no
unless you've been lying i have not been
lying
a care is definitely one yeah so this is
a good
wtf right here this is this is you want
to manage memory directly you got to
know about memory
so so this is kind of a screwy thing um
okay the the size may not be
exactly the size that it needs due to
alignment
so the way the processor works is it it
fetches
like typically it's organized like in
memory to
fetch on a certain size certain size
boundaries
it's optimized in this case to
fetch on four byte values so if you
give it like four bytes plus one more
byte for a care
that'd be like a fifth byte it can't
read
like one of those cares you know like it
can't store
like it's misaligned basically
okay so what seal do what gcc will do by
default is it'll just pad it out to sort
of whatever the alignment is on your
processor
okay so you'll see that show up in a
bunch of like weird places like for
example if i do like
if i do character you know name and i
make it
four how big should this be
so four characters yeah it should be the
size of four characters
yeah which is four
yeah four so it does what we think but
if i make it
three characters
it's three okay
this is kind of screwy isn't it
yeah this is a little bit confusing
honestly it's very confusing okay if i
make it three and then i throw
an int in there under it
so it was three a moment ago
now it pads it up to eight but if i make
this four then it's still gonna stay
eight so be careful
it's what i'm trying to work any sense
okay no it
it does make sense from a hardware
perspective it does not make sense from
a code perspective
like so from from this code it it looks
like
it should be whatever the minimum size
is to
capture this data but that's not exactly
what happens on the stack
when or well in general memory access
like it
be on guard basically okay
um so should strucks not be defined
closest to where they're used i realize
that's normally global scope but
sometimes
yeah i mean are you re
that is um you could uh
it's most of the structs that she's
going to be seeing are going to be in
global scope so
it i figured let's just start with that
um
so yeah and this is bytes okay they all
caught up greetings from australia
what's up australia
a tippidian a tippity ian i don't know
um yeah project this is about learning c
this is c as an additional really
it's like 8 a.m in australia yeah i know
that's crazy
i think my aunt lives in brisbane so
brisbane nice
nice pronunciation there too that's
i i think the australians are gonna be
happy about that
do people pronounce it differently and
marie you're jumping ahead stop giving
it away that's what's coming next
um oh i was off okay so it's oh because
of daylight savings
my bad yep i always forget to take that
into it so
as um
our uri ui i think is probably closer
um as mentioning there is a way that we
can actually tell c
no i don't want you to go screwing
around with the actual memory usage
and you know make the alignment be all
happy and good i want you to allocate
exactly what i'm telling you to allocate
and in the simple case here which i had
kind of before where we sort of have
like a one byte and a four byte and we
would think it's five bytes
but it's eight bytes because it's
padding it up to the data type
we can actually add in a little bit of
syntactic sugar
because underneath um
this this interpreter is actually gcc
and
this is a non-standard c extension
so it's although it's non-standard in
the sense that everybody's got it
nowadays but
um and this packed so this is a little
bit outside the language and that's why
it doesn't really look like it
fits you know with this yeah i was like
what yeah whatever
one of these things is not like the
other yeah
this is this is your first compiler
directive so this is the first time
you're speaking directly to a particular
type of compiler so this is not
c standard but it is important because
when we run it
what happened to the size of the struct
it became more
i would say like accurate yes it
basically said
okay fine i will misalign memory all you
want
if you specify that it's going to be
packed so this is kind of important in
the case where you're like writing to
disk like if you're if you're
serializing a structure
you might want it to be exactly a
certain number of bytes
for whatever it is maybe you don't want
it to be fast for memory access
gcc by default is making it fast for
memory access so it's aligning it on
the memory like kind of criteria so
um it is a type of syntactic
salt there's sugar but yeah this is your
first
compiler directive so this is not a c
standard thing
all right but it all makes sense though
right
and good on you crazy tech starting the
journey on programming you might want
the
uh the stream that's usually in the
afternoons which would be in the middle
of the night for you which is
python from scratch but this this is the
the more advanced c stream for today so
if you're into c
then good luck um
okay so language wars and chat aside
let's um how do we access individual
members of this so
in the example that i gave you of why
don't you set up another planet like
jupiter okay so first we need to
change
so to declare this and by the way i'm
going to get rid of this syntactic
nonsense that we don't want right now oh
sorry
no go i'm messing you up i forgot about
that selection
issue
so this is the one that we had before
i know 715. just add it to the long list
of features i got implemented
he's working on it i'm working on it
it's working progress
okay so yeah we run this and hopefully
it runs right
let's find out no
no it's okay oh we need we need um
[Music]
yes thing yeah we need that thing
okay there we go
um and yeah by the way just to crazy
text that
dot eno file is c like but it takes away
some of the nastiness from
c and c plus plus but still stays very
tight for embedded systems
um so yeah so we run this and we're good
to go
now how would we print out the o period
let's say that we write a function to
print this thing out
so let's call it you can't just call it
you can't just go like x dot
o period and it's not going well i mean
give us give us a function that actually
produces a record on output that shows
like these different fields like because
i can't
what i can't do anymore is i can't say
printf
you know magical type
and then pass along x right because this
is not going to work
there is no format specifier no but you
can give it like a
float or whatever and then pass it you
can give it so give me a function
called display planet
um that just returns the period
uh no that'll print out all the records
all the all the
parts of the record
i forgot and see what's the convention
for functions naming them
yeah yeah no you're good oh like camel
case yeah
yeah yeah okay all right um okay
so then we're given let's see here
i can just call this
well no because we called it planets so
i don't know do i need to specify
specifically that it's a planet
structure in here
okay but that's just a data type so you
also need a variable name
um we'll just call it we'll just call it
x yep
um
maxie's loving the camel case
oh brain fart um
i'll go piece by piece what do you want
first the the name right
the name yeah yeah i know i just had a
brain part about what thank you yeah
string like sitting here like
it is string right and just measuring
this band a weirdo all right
oh well hold on
it is float how would you do that uh
yeah just making sure we haven't really
dealt a lot with glutes
yep
i figured if we were going to get into
astrophysical phenomena we should
start using some sort of decimal math
and for let me see i'm trying to
remember for all functions do you
no no we're good this should this should
work and then in here
um we can call display
x that should work right
yeah let's find out hey
not bad i like it
now do you remember a little bit about
the stack when i was telling you about
function calls and how it would wind up
the stack and unwind it
it's one of those things i feel a little
fuzzier on i remember you talking about
like
bottom up and top down right yes yes
it is very much that um so the way it's
written right now
it's actually going to copy all of the
parameters
to the other function so when it's
setting up the the function call
it's going to copy the name which is 20
characters and it's going to copy the o
period which is 4 bytes on a float
now that's not that bad but you know
when you start dealing instructs you
might have something like you know
like geometry
land masses and you know
you might start having like structures
that start to get really big
okay so i told you before there is one
of the first uses of pointers
is to not
copy all of the data on the stack
basically says like okay i'm going to
allow you to step outside of the stack
frame for a moment
and look back at the original data
structure as opposed to
just directly copying everything i mean
so the copy version you just did works
and it's okay because this data
structure is still pretty small but as
you can
imagine structs are going to start
getting very large
so how do we use a pointer to do this
also
i'm not sure where the star goes but we
would want to specify i think
like right that's where it goes
and then it's not going nice joke are
you
riui i like what you did there
that says it's like a risky profession
some math geekery
sorry i i got this that's a good one
that was that was good love me some
punnery
all right yeah go ahead with um so so
that's pretty good that'll turn it into
a pointer
now the problem is now it's actually a
pointer so
i can't dereference it with dot anymore
because dot
directly acts it assumes that like the
structure it's operating on
is right there so okay in the case here
it's saying that like
name is the first part of the record and
then there's 20 bytes of that
and then o period is the next part of
the record there's four bytes of that
but in this case if i'm passing it in as
a pointer
how big is the pointer however big the
memory address is
yes or like the however big the chunk of
memory that the memory address points to
is
so probably 64. it's it's it's the first
one
qp operator no
strophium don't don't don't stop that
don't look over there
we don't get the pew pew operator that's
we're gonna do it the hard way first he
was being sad no don't don't he is being
sassy but don't don't
don't don't look at that and don't look
at what nathaniel just wrote either
so you're banned from chat for a moment
um okay uh so we can't do what nathaniel
said
because that makes sense that's that
that's what we're gonna do i mean he's
okay all right giving it to you
i mean thank you nathaniel i'm gonna i'm
gonna give you negative
how does this work at a high level
because we're not always going to have
nathaniel with us to tell us
you know the correct syntax but um
what do we although that might be
convenient you know nathaniel knows what
he's doing
and hello cam how are you so
yeah at a high level this is basically
um this this pointer if i'm dealing with
just a regular
ordinary data type i de-reference it by
using the pointer again
so this says give me the piece of
take x as a pointer and go find the
memory that it refers to
okay so this is now like jumping to the
actual
block of memory where the concrete thing
is so this is like
i had the pointer which told me the
memory address and now this star says
jump to that part of memory and that's
the base of what i'm going to do next
okay and then after that i'm going to
put that in parentheses because it's
actually really low precedence for
reasons that are
obscure um okay so we have we have to
put in parentheses to say like no i
really want you to dereference it first
okay so like order of operations yeah
it's an order of operations thing
and now that i'm pointed at the concrete
memory not the pointer itself
i can use the dot operator okay
that makes sense which and now i can
refer to name or i can refer to oh
period
but if i just did it straight up if i
did this
which is kind of what i want to do when
i first do c
then the way c starts applying to the
whole thing yeah that's
exactly what's going to happen which is
which is bad voodoo because
this is not the thing this is a point
yeah no that doesn't make sense so it's
going to try to dereference
in a pointer and you're going to get to
keep both pieces now a modern compiler
is going to tell you no that's not what
you want to do
but but older composers get in trouble
will not tell you that
and you'll still see those on embedded
devices like
some of the ones that are getting
mentioned but um
fortunately this has been around long
enough that you know that
it'll it'll warn you nowadays but you
know that's basically does that make
sense for like
how we're dereferencing it first and
then we're looking at the part of the
record
okay well it turns out that's it there's
syntactic sugar for that which is the
pew pew operator
okay and the pew pew operator means
exactly what we just wrote it means
dereference first
and then go and pull you know that part
of the record so
this is just syntactic sugar that means
exactly the same thing
maybe if i cannot believe that is
actually a real thing
it's not called pew pew but
what is it actually called uh the
the pointer dereference operator i i
okay stropheam says it's actually the
drill down operator
um but i i just call it the pointer
dereference operator
site i like pew pew pew pew is good too
i like pu
we're going to call it pp i like p2
makes me think of lasers
arrow it's it's not really going
anywhere 75 but i guess you could call
it the arrow operator i'm not i'm not
sure what the correct
like you know terminology is it totally
looks like an arrow though
christopher aurelius i'm sorry this is
uh
topic seven pointers and dynamic memory
allocation is a very hard place to come
in
if you haven't been doing a lot of
scenes my new friends it's okay
it's confusing for me too don't worry
because we only have
uh let's see 30 minutes more of this and
then we get to the scotch so
okay scotch and star trek um yeah we're
gonna get to the star trek thing so we
have to get through pew pew
all right um so using the pew pew
operator how can we fix
our code to deal with pointers
so i think here well
so can i just kind of go
does that work i'm not really sure how
to use this thing
so yeah you're really trying to use this
thing that's exactly it yeah go ahead
try it
i like pew pew now it's angry at pew pew
it's kind of angry
because we didn't change it when we
passed it in so
we changed it on line nine so we changed
our definition to accept that it was
going to be a pointer
but we didn't specify that it's a
pointer no we
the opposite so the opposite of the
pointer operator this is d
reference which means take the value as
a memory address
and look into it but here it's the
concrete thing
so right we don't want the concrete
thing we want what place it is in memory
so that's like get a reference as
opposed to d reference
yes and that's this operator so this
would be like the opposite
direction kind of no wonder my visual
code was cranking at me for that line
it's like doing it right stop getting
angry at me it's like no you're really
wrong there
yeah modern things will warn you but you
know we're learning fundamentals here so
we're going to do it without the ide
alright so we run this and what do we
get
hey except this time we didn't copy
yes so we're saving memory so we used
our pointer and i'm so glad we did
because that's the second part of our
lecture
all right so um structs make perfect
sense right
aside from this really weird patting
thing
as we move into pointers would you let
me go get a slice of pizza you can go
get a slice of pizza that's
yay okay and that'll give a chance to
answer chat questions very quickly
i'm sorry my food no please go right
ahead all right
oh nathaniel that's the proper name huh
that
i will mention that uh when she gets
back that that sounds like the correct
um
way to refer to pew pew just
barney wanting to sound as pretentious
as possible that may be
too once you use it for legit drill down
it makes so much sense to call it drill
down operate yeah
i'm fine with it joel john i like your
earlier ppu operator
what ide is that this is not an ide it's
um
this is multi-user pad which is
something that i created
for these sessions quite literally so
i've got it
on github which i don't know if i got
the bot to oh come on bot
um so i will fix the bot later
so fortunately we can remember this
because it's not too hard it's it's this
is all under coding with some guy
and if we go here go to
coding with some guy and this is
multi-user pad
so i've been slowly trying to totally
alpha unstable i've been creating this
actually live on stream for uh as a
learning tool and
and now i'm about to start the pro mode
which is
for hack sessions so you know this this
was initial focus was really on
like building enough that we could do c
on the web or we could do python or
other languages and
um now the part that i want to get to is
the goldeneye mode i want like the like
goldeneye 64. like
split the screen four ways you have four
separate editors and everyone's hacking
and you know just
imagine like hack day hack session is
kind of where it's going next
um and by the way that i will drop a
link to and chat
since i need to add it yeah i made the
platform though crazy doug and i do
stream when i work on multi-user pad
learning stuff i always do on mondays so
oh and she has pizza and she's back
so nathaniel mentioned while you were
gone that uh it's called the structure
dereference operator
is the correct name for pew pew
why would we call it that one we can
call it pew pew uh
i'm fine with pew pew but
but it might you know it might be
awkward in like a meeting one day you're
like well there's a problem on line 17
with the pew operator
i mean is that not like like is that i
would love it if you did that at work
but you know i mean different people
different styles different companies i
mean
maybe if you're in like doing a defense
contract you don't want to call it the
pew pew operator i mean it might
might have different connotation i'm the
only person on my contract who codes
so i'm pretty sure i could probably call
it like the you know
martian operator and they'd just be like
wow okay
so okay so you write exploits
wow they really started a movement nice
job strophium i like what you did
so you literally made it up that's what
i thought okay
okay the the isis standard nathaniel is
pulling up the standard and
seems to say it's referred to just as
the ship dash
greater than operator so strophium
that's how much i'm not getting into
that
how much do you dare me what do i get
and i i will like i wish i could record
it but i obviously can't record work
meetings
i will totally say that in a work
meeting because like i said i'm the only
person who codes you know
accidentally leave something open on
discord so we can hear it nobody would
know
no i will say the spaceship operator and
they will be like i love it okay yeah
clearly
i see that all right well listen
fun time is over because we're into
pointers and i'm putting the glove on
because this is
i have i have pizza you got pizza i hope
that brings you to a happy place
i'm gonna i'm gonna always brings me to
a happy place i'm gonna come back to
multi-user pad and i'm gonna switch back
to whiteboard mode
all right so we did a lot really quick
i'm so sorry is this spaceship operator
real thing
yeah i'm not getting into it
[Laughter]
i can't tell who's joking with me and
who's being serious
trophies brought it up i will let
strophium talk about it but
um i i think you're gonna get confused
enough with part two which is really
pointers okay and just because this is
such a big topic i'm switching colors
because what am i thinking
twitter has told me to be afraid of
these things um twitter
is not wrong so that's all i'm gonna say
if i need something stronger than pizza
okay pointers
at their core are very simple
okay it's just an
address of memory
now that's really important to like
memorize because
they're gonna get scary they're gonna
get confusing but
literally at the end of the day it's
just an address of memory so just
like channel that and ohm with it and go
an address of memory home like it's just
just
that is all a pointer is so do not be
afraid of them just
realize that they will get you into
trouble very often and that's why we
love languages like python
so yes generally we don't have to use
them
but at the end of the day it's just an
address of memory so if i have
eight bits of memory
so if i'm on an 8-bit chip like back in
the day or if i'm programming the
nintendo like we're going to be doing a
little bit later
then maximum number of memory locations
i can have is 2 to the 8th which is 256.
i don't want to put you on the spot
while you're eating pizza
so i can view memory as top down bottom
up it depends what kind of place i am
but if we look at it this is memory
location 0 1
two three four five all this stuff
up to memory location 256.
yep okay that's it
so that's all i'm gonna get
now if i've got 256 different memory
locations
and i want to refer to memory location 5
i just call it
location 5. a pointer to that
is just location five
so if i create a pointer now if i have
memory and it's organized as
eight bit blocks so let's just pretend
that really quick though so if we had
something stored let's say like an
integer well an integer wouldn't make
sense here
let's say we had poop um
let's say we had just something let's
call it x i'm not going to specify what
it is
stored at memory location 5.
the pointer for memory location five is
just memory location five
yes same as the pointer for x is memory
location five
yes okay ish i think
oh i actually put the letter x in here
so
that might not be exactly what you mean
but like let's say that like
i had a character the pointer to the
item that we've stored there
is the memory location okay i'm just
gonna
write that out so that we all don't get
confused let's say that there's
character
x and it's assigned to the letter a
and that's i don't see how that's not
confusing so let's let's not use
characters at all let's just use
um let's use numbers that are eight bits
wide
so we're going to use the new style
types okay so this is an eight now
forget the fact that this is actually a
care but
um this is just an unsigned integer
that's well actually it's really an
unsigned care so i guess int 8 would be
a care
so if i have a variable there and it's
called x and i assign it the value
well let's let's pick a different number
let's assign it the value 42.
so if i print out x
you get 42. and if i can
write correctly then and i remember this
thing
and then i say this is x then i'm going
to see the number
42 show up when this thing prints out
right right
cool all right where is this in memory
i mean this is a variable so it's it's
stored somewhere in memory
yeah memory location what we're calling
memory location five okay so in this
case we said the compiler assigned this
to memory location five
it's a little bit more complicated than
that it there's something called elf and
there's dynamic loading but
let's assume like like like santa's
elbows
yes all right we're gonna get making
sure i heard you right yes
you definitely heard me right um we'll
get into elves there are elves hiding in
this computer
all right so um okay
that other guy was saying c wasn't weird
all right
so yeah spaceship operators and elves
okay
now yeah you were going to get the elf
is the literal term for it
actually but we'll get to the elf tools
a little bit later but
so let's say that the compiler assigned
definite static location five this is an
old school compiler and it literally
assigns all of memory
and it chose location five for
variable x okay
all right now it knows that this is
aligned properly i'm ignoring alignment
stuff which we kind of covered briefly
when we talked about strokes
so how do i find out in c
where this thing is stored
the pointer yeah i would just say give
me
the reference to x this is 5.
right yes so
if i if i say like this if i use this
ampersand
that's saying give me i don't want the
variable itself which has 42 in it
i want the location in memory of where
it is
and this this says tell me where it is
in memory
and it's gonna say oh you mean this
thing you mean five
i'm assuming you're going to do some
pointer arithmetic but compilers didn't
do that until later
so ampersand x returns a pointer
yeah i mean what is a pointer the memory
location
right stress of memory you have to say
this like a thousand times
so it's just an address of memory so
the address of memory of x is what
five yeah and i think that this was
originally called the address of
operator so
well in the case of five big mama i
chose the number five intentionally so
that we could avoid the hexadecimal but
but yes um this would be if you wrote
five
from decimal in hexadecimal it would be
zero x
five and if you wrote it in octal it
would be
still five because fortunately five is
less than
the size of those number bases now if
you did it in binary you'd actually have
to do a little bit of arithmetic but
we're not we're not worried about
different number representations we got
into that in like the first stream
so um we're not worried about this we're
not worried about this it's just
it's it's we're just we're used to
dealing in decimal numbers so we're
going to deal in decimal numbers so
we want the thing that's in memory
address 5. now
that is how i get a reference to it so
this will this is a c
way of saying look compiler tell me
where you're going to put this in memory
you know and that's important so
then in order to store it and say i want
to refer to this thing
as a location like let's let's assign
the variable
y i can say this is going to get
the address of i read the ampersand as
address
because it's it's the address and memory
give me the address of x and that's
going to say well in this case it's
going to evaluate to five
now if i actually knew the memory layout
of my computer i could
literally say care y
is five and this would be interpreted as
a memory address that would give you
whatever happens to be in memory
location five
don't try that this is the kind of thing
that
wins the obfuscated c contest so we're
not gonna do that
um but it's also pretty much illegal
these days because nowadays that would
be a segmentation fault because you are
reading
past the part of memory which is related
to you that might
five might be there or it might not
depending on where it gets loaded
okay great so pointer you mean like
something might not be there or
what do you mean it might be the memory
is definitely there
no it's there but it might not be yours
right because if i have like a bunch of
programs running on this computer
because nowadays we have multi-processor
you know well multi-tasking
operating systems with multiple
processors and stuff like that
your program might be here like this
might be like the chunk of memory that's
like
jesse land
okay but over here might be the chunk of
memory that's assigned to like some
system daemon or something you know
so if i just refer to an address
like who knows who has five right this
this
is probably owned by this we're talking
about like permissions right
yeah yeah that well that's exactly these
are all called segments
right this is a these are all segments
so
in our modern memory model we say that
memory is segmented so
like you get this chunk jesse gets this
chunk system
gets this chunk you know whatever
happens to be running on the system at
the time
okay so we don't typically write out the
exact address anymore we just say to the
compiler
you tell me what the address is of this
variable
that makes sense okay also just because
there are so many addresses at this
point that
i imagine that that becomes unwieldy as
well well again it depends on the kind
of
processor you're on like if you're if
you're dealing on like an nes
then you are going to refer to memory
directly like
like we'll be doing later on because
there's there's yeah you just mentioned
like modern yeah most modern machines
are
position independent so you don't know
where you're going to land in memory
until execution time
so see but anyway that's getting into
elf i don't want to get into elf that's
it's an advanced topic
so no elves for me okay um
moral story like you're doing a bad
a bad thing and that's actually what'll
happen when you
this is where you start going wrong with
pointers like for example
say that you said that you know in our
earlier example of struct planet
let's say that we had a struct planet
which was
a pointer to a planet and we just
referred to it as
x and i say that x is going to be
referring to the mercury
planet you know i said let's say i set
up a variable called mercury
then this is fine
right this is all valid but if i go and
i say like
you know x dot name
well in my structure that doesn't make
sense yeah i mean
that's right because this is an address
it's a memory address
yeah so if i jump the actual this is
really an offset right this this
ends up being like how many bytes from
the start of this record
is it and that's what the struct is
defining so
i got to be really careful here right so
this this this whole
offset thing if i try to do an offset
from a pointer
i'm going to get another data type which
is not related to the data
so and then c is just going to keep on
trying to go if it can right as long as
you don't get a segment violation so
like i said you're lucky if it crashes
okay so that's why we have to start
being really careful about
pointers now one last little bit what if
i have
like now in the example that i had i had
like you know say this was memory
location 5 and i had stored 42 in here
and then i had another variable which
was x
and well actually i think we called this
x and
then i had another variable y and i'm
just going to define
y as actually in our we were calling
this an
int a t and i'm going to make this a
pointer and i'm going to say
y is going to point to a reference of x
so what am i storing in y
let's say this landed in memory location
12.
you are storing the memory location of x
yes
where is it in y uh five
yeah so what what values in here five
because a pointer is what
memory address yes it's an address in
memory okay we just have to say that a
million times if we're not going to get
screwed up
with pointers all right what if i
create a new thing which i call z
and i have it point to y
z is 12. yeah if z lands over here
because it's got to stay actually this
is already illegal because this is
a segment violation but um let's uh
see it's that easy all right
so z is going to end up with the number
12 in it so this is a
pointer to a pointer
to a memory address and that's typically
shown like
this which is pointer to pointer but i
could actually
just treat it as a single pointer and
this is where pointer math starts to get
a little bit hard
okay all right now you've intuitively
been doing this with strings
so when you had something like name
and you said like you know this is gonna
be
like and we use this syntax and we say
like it's mercury
this is valid it's gonna allocate enough
room to store this plus it's gonna have
that invisible null byte that we talked
about before
and it's going to make this that size
okay now if i have like you know
a pointer to it like say i have x and it
points to
the location of i want to say name
except
name itself is already that but
where would be the first part of memory
of name what would be the first part of
that array
it'd be name sub zero right yeah
and i could take a reference to that so
this is valid
yes that makes sense okay um there's a
shorthand to that which this you can
just sort of refer to using pointer
notation it already is a pointer in a
sense
because this is an array so this you can
actually just do the assignment as carex
is name and this will work
it might throw a warning that this is
not the same type as this but it'll work
all right so does all that make sense
um so i think yes
all of it has seemed really really
intuitive until the very last thing you
said where you just kind of said we
don't need to have this notation
and then you lost me first i apologize
um that's syntactic sugar
so when i did something like name and i
said that let's say that this thing is
like 10 big
and i put in here like jessie
[Music]
okay this is going to be jesse is five
characters
this is five and i always have that
invisible
null terminator at the end of the string
so this is really going to be six
characters wide that's smaller than 10
so we're okay the compiler's not going
to throw anything about that
so we have a few extra blocks of memory
afterwards that we're not using but we
could use it later
all right that part you're good with
okay yes now what's the first character
of this j yeah i mean and how do i refer
to it with this variable
name uh indexed zero
yeah okay now where is that in memory
well wherever i mean we could point to
it and where we could use that
and how do i find out how do i yeah i
use the ampersand
which says give me a reference to this
in memory like
like tell me tell me the address and i
can assign that to another
variable yeah like if i have a pointer
to the name
like x so x is now pointing to that
right
well it turns out that actually this is
typically implemented
as a pointer itself but what is sorry
what is typically
i might just be a little bit behind on
this no that's okay uh this this name
in the character declaration which
refers
you know name itself is not right that
makes sense it would be a pointer though
right because that's when you're
declaring a variable you're kind of
setting you're allocating the memory
yeah
it's not actually it's like a pointer
that's set to the start of where that
chunk
of memory is so i can use this type of
syntax which
actually works with pointers as well so
um and that's pretty much what you're
doing here it's syntactic salt as
nathaniel it's not syntactic sugar it's
just painful
but name by itself
is really just an address in memory it's
really the address of the first element
gotcha so i don't need the ampersand if
i
don't de-reference it so in this case i
dereference it i get the j but
i want the address of where j is using
this ampersand
then that's going to be that that'll
work but otherwise i could actually just
drop all that and just say care pointer
x is but we would have to dereference it
explicitly if we wanted to say the
memory
or the the pointer of like s the first
steps
yeah right yep yeah okay so we couldn't
use the syntactic salt or whatever
well no i could because where where is
that s what location is that
relative to the start
plus two yeah and i can actually do
exactly that i can say
x plus two
now this is a little weird because
you've probably never done that
[Music]
um all right that is um
so basically i'm taking if i assign this
to the first
address of the array which is kind of
like the memory location here
i can say two locations past that
assuming that they're all
eight bits wide is going to be two bytes
past that
is going to be where the s is so right
so i guess my point was
let's pretend that you aren't
let's say that you're not doing that
care pointer x
line right and let's say that instead
you wanted just care x to be the
the memory location of s without that
first declaration
then you would have to use the ampersand
because you would not be able to use
that shorthand
of saying carex start like you can't say
care
star x equals name indexed in any way
you would would first have to create
that point right
i want to i want to actually walk
through this like with real code like
let's just let's try it so if i do like
care
name 10 this is the example that i was
writing
and this thing is jesse yeah all right
so my normal way of printing this out i
can just say like
okay do this and you know
do name right so this should work right
okay so i execute this it shows jesse
now if i do name location zero what do i
expect to see
j well not quite
this is uh no longer a string it's now a
character
okay so i change the specifier to
character and we're good it shows up as
a j
all right if i get the s that you were
referring to before and i
change the subscript to and i run that
we see the s
right but the s can also be gotten a
different way
so in the earlier example i was telling
you you could do this right
yes okay so if i under i do understand
why
we could do like x plus two and whatnot
my point
that my question was if
we don't have this line we cannot say
say like y i don't know
no well that's not a memory location
exactly that was just my point
is that like you couldn't do this you
couldn't have that experience but you
can do this
yes exactly that was that was my point
was that
if you're going to reference an explicit
letter without having first line 16
you have to use the ampersand
to have that like explicit reference
you do to get the memory location the
ampersand is about getting the memory
location but you don't
actually have to have it because what i
could do is i could write this
like this and i could say plus two
yeah and that's totally fine it was it
was about sorry it was about the
indexing oh okay
okay gotcha yeah yeah yeah the indexing
like
actually gives you the concrete like
memory location of that s
but it's not sorry it's not the memory
location it's the thing that's at that
memory location right so you want the
memory location you have to use the
ampersand
but we we have a little bit of syntactic
salt in that
referring to name with nothing just
inherently refers to a pointer to the
first location the first location
exactly yeah so i'm saying you can't use
that
syntactic salt for anything other than
the first memory location unless you
either have plus three
or you put the ampersand and the index
right
yes okay yep that's fair okay
so that's actually your first bit of
pointer arithmetic
so i can set this to you know name
plus two because name is actually a
pointer on its own
and then if i clear the output and i
print this out
what do i see
oh okay yeah that makes sense then
because it's going to keep reading
yeah it's going to say name is really
just the first location in memory
it's a pointer on its own so i can
actually look at name directly if i want
to
and say like what is what is name like
well it's and it's going to be yeah
we're on a 64-bit machine so i'm not
using the right format specifier but
moral story is a big number so
okay so that does make sense right
yes okay good um sorry i've been sort of
missing what's going on chad
but um no no lots of help like lots of
people explaining things in different
ways and giving examples
and it's helpful it's cool um chad is
awesome great
now we can get to that was the easy part
okay i'm sorry so
now earlier on we were dealing with some
examples where we always knew
the size of whatever we're doing and you
were starting to say
like well what happens if we don't know
the size and we need to
dynamically say that it could be this
big or it could be that big you know get
more of the python kind of like
i don't know how big it is just you know
give me enough memory to do it
yeah well how do we do that well we have
to work with pointers right
because first thing we need to know is
we need a place in memory to work with
right and that that place is
probably going to move around so i need
a way to refer to that piece of memory
and that's typically done as a pointer
or a pointer to a pointer which is
classically called a handle but anyway
so let's do that and i'm going to remove
the struck stuff because we're not going
to need it for now
but good job on that by the way
all right so there's two operators that
we're gonna see
and i'm actually just gonna quickly
throw this on the whiteboard because
you're gonna need to know both of them
um okay the first is how do we get a
chunk of memory
dynamically well your basic operator for
this
in c is malloc and
that's a memory version of the alloc
routine alloc used to be implemented as
a much lower level primitive that we're
not going to be using in here i don't
want to see that kind of stuff in here
miss
so we're not doing that um but malloc
was a memory allocation based on a
particular size and so
it takes one argument which is how much
and you tell it how much you want
and it always comes back because there's
an infinite amount of memory and there's
never any problems getting it
and you assign it to a pointer thing
and if you don't know what it is you
could even use a void pointer if you're
being really naughty but we're not going
to do that
so we're going to assume that like we
want to do a string and
i want in the earlier example of name
let's say we don't know the size of the
name
then what we could do is we could do
something like this we could say name
which is a pointer
is going to get a nice chunk of memory
and we're going to specify how much we
want
but the nice thing is we could change
this
later and there's actually a call that's
related to that which is called reallock
and we can also there's a few variants
of this but i'm really only going to
cover malloc in here
all right so malloc basically is give me
a chunk of memory
this big right i mean don't we still get
back to the
problem of like you need to know how
much you need in the first place well
that's always the problem i mean it's
just that now you have a way of
allocating new memory which you didn't
have before
okay i mean is that not what we're doing
when we say like care
you know one thousand but that's at
compile time
right what happens at run time like
let's say that we're
pulling in some json and we don't know
how much it is well we can pull in like
you know i mean i don't really work a
lot with compiling so sometimes i think
that distinction's a little fuzzy for me
okay um we'll we'll get to that uh
basically in python
compile time and run time are sort of
the same thing so it's it's an integer
yeah
so it's um in in c once you've compiled
your program that's it you can't change
it
so if you specify that a name is 20
bytes that's it it'll only ever be gav
have 20 bytes of memory if you want to
dynamically size that then you're going
to need this malloc call and that's how
you're going to say like
give me 20 bytes okay you know what give
me 20 more you know what give me 20 more
after that
and that's how you can start dealing
with like memory dynamically
the matching call to this is free
and that is this needs to refer to
the pointer and this is how you tell the
system okay i'm done with it
so malloc this is how you get it free
this is how you get rid of it
and everything will be fine right
and this is where all the pain always
shows up that you're reading about
twitter by the way because you can you
can free
after you there's all sorts of free
errors because you might have forgotten
to free something
your memory leaking you might have
double freed something because you freed
something that you had accidentally
already freed or another caller had
freed and you didn't realize it
like this is kind of where things get a
little crazy is ownership who owns this
memory who's responsible for allocating
it and who's responsible for
deallocating it
and that's where that's where pointers
start to get hard
okay that makes sense okay so
armed with this information let's do it
let's
let's set ourselves up with a name so go
ahead
and um give me name this time
dynamically allocate a chunk five
characters
well let's i'll give you 10 characters
hey what's up chats it up
five characters like
meaning 40 bits oh
this is bytes this takes bites as a oh
sorry yeah
okay so
so you could just say five yeah
okay all right so if we print out name
what do we expect to see what
what is name itself it's a memory
location
yes it's a pointer right so which is a
memory location so if i printed it out
it would c would just tell me okay this
is where you landed in memory yeah
now there's an important distinction
with malloc malloc
does not so far we've mainly been
dealing with stack memory which is i
explained before with the winding and
the unwinding so
that's where like you know this
particular function gets this much
memory and that's it
right malloc breaks that rule malloc
comes from heap
memory which is sort of this other pie
yeah okay
um and it's typically managed um
using something called s-break which
we're not going to get into but
um it's uh it it it's a little bit
um i don't want to go too far in it like
right off the bat but
basically it's it comes from a different
segment of memory that we
briefly covered in the other class so
let's let's stay more on practical we'll
get more to theory in the future
all right so how do i put something into
name
i mean how do we put your name in there
give yourself 10 bytes so that we have
plenty of room
matlock is an old marvel villain i like
that it's trophy
um
[Music]
i'm actually so is this like a buffer
kind of like a number that we were using
previously so we have to use like string
copy
or that's exactly right string copy
would be the easy way to do it
the harder way to do it would be like
one bite at a time you know you could
put j in the first address and e
in the second dress if you don't want to
do that i don't recommend it use string
copy
okay so let me remember
oh shit i remember like the syntax with
the copy ones
through me last time yep it always does
gets me too
so i was trying to find my notes on it
the phone looped in the heap
[Laughter]
oh goodness um let's see let's look at
my
i think it was the week before that let
me look at this um
[Music]
now string compare so we went over that
ah here we go string copy yep okay so
um oh wait hold on
oh you need a new include for that
oh my bad sorry oh sorry you should do
it i do what am i doing
yeah now that comes from the standard
library gotcha
okay okay what did you not do
oh no you ran you you ma you dynamically
allocated your memory you copied jesse
into it
you printed it out and you returned zero
first time
ever you dynamically allocated memory
you leaked it so your program has a
memory leak congratulations you're now
part of the club
now you can leak memory with the best of
us
no it's not for shame you're cool now
you've actually leaked memory you can't
leak memory really in these other
languages like javascript it's still
known by the interpreter you're like you
don't
really leak it you know it's this you
just
leaked it that on an old operating
system that just sort of disappeared
from the heap forever so congratulations
chat let's let's give her an applause
come on you know let's
actually get a badge i'm gonna i'm gonna
give you like a real applause
i think i think chat should give you an
applause you just you did it that that
is your first dynamic allocation
you have leaked memory and you've now
properly corrected it and i feel good
about it
we're going to get into advanced memory
topics later uh because we're going to
be kind of shorter and i think we need
to have a quick scotch
if we that sounds good okay