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

Topic 5 - stdio: String Functions with a little Binary

Outline:

  • manual (man) pages - careful about parameter orders
  • a first look at pointers:
    • pointers as a way to avoid copying everything
    • pointers as a way to modify outside the stack frame
  • initial string operations:
    • strlen - string length by counting to the null byte
    • strcmp - comparing strings, but equality is 0
    • strcpy - copying strings, but dest before src
  • watching bounds, how far is too far
    • snprintf - a safer way to print to a string
  • binary using string-like operations with sizes:
    • the sysv vs. bsd worlds - parameter swaps
    • memset
    • memcpy
    • bzero and bcopy

Transcript:

hello everybody
how are you all doing we have the
enceladus horus here
maybe are you there yeah she's there
okay cool
um and we're gonna be trying out some
fun stuff for y'all
tonight we have a uh this is c
as an additional language so uh the
enceladosaurus
who's normally an astrophysicist by day
has decided to learn some c by night
so we're going to be doing uh this is
we didn't have the python session
earlier we were doing some editor stuff
it was acting a little unstable so i
just cut back to the old version
but we're going to be focused today on
learning tools aside
the stdio so this is a standard input
and output
in c we're gonna go over some of the
basics relating to that so
i'm gonna just open that up uh thank you
everybody for dropping by
and i hope that uh people with haircut
and
yard needs are getting everything sorted
out like
nathaniel here and i can't even imagine
what that's like with a mask that
doesn't sound fun at all
so all right um you might notice a
little more audio noise
we were just seeing in the little
warm-up that uh there's
might be some skype nastiness so we'll
um
we'll try to be careful with that
if it gets really bad we'll cut back
over the discord so
what are we doing we have a
whiteboard screen all right now the
editor that we're back in is kind of the
normal
one this is actually this is all sorts
of craziness here
we're in learn mode yeah okay so here we
actually have
the enceladus horus up so hi guys
uh hopefully and chat keep us honest in
the event stuff
stops working so i'm just gonna i threw
the glove on for
whiteboard goodness so i think we're
gonna cut over to that
um yeah yeah we're getting fancy tonight
sonny
okay um i'm gonna cut to our
whiteboard where's our whiteboard what
did i do oh here it is
all right so we're in topic five
and i guess we have green is that
what how about this t5 this is about
stdio
so this is the standard input and output
uh going standard being not so standard
but
it's been around for a while a long time
so we're going to cover
two parts of this today like our first
part is going to be
around handling strings so
this is some of the stuff like uh you
know str
len and all that comp and
etc and then later on depending on how
we go with topics and this might be a
little too much
and that's part of why we're debugging
the lessons is file handling
so this is anti-style file handling
and we're aiming for i should mute your
typing sounds
um so this has uh
this is this is ansi style so this is
stuff like f open
and you know f close and all that stuff
so this is kind of a reach goal so we
might just end up staying over here
all right well what are these things
before we can really talk about
any of this stuff we need a new color
i don't know about that color let's
let's come over here
to see programmers not pronounce std as
stood standard
i guess would be the the right way to
say it nathaniel i'm not really sure
no we're not doing live uv sunny
discord let's we'll get to that later
okay
um great so and lib uv is i guess more
standard actually to her point so
all right what do we have down here we
in order to be able to talk about
strings we have to recap some of the
stuff that i'm sure
the enceladus source has forgotten so
how do we represent a string in memory
what do you mean
okay well in python we just call it str
and we say
stuff right it's a series of characters
yeah it's a series of characters so
series means
array in c um so and if we're gonna go
with a series of characters then it
would look something like this as a type
so this has a bunch of characters this
we learned
are really this is a this is a signed
type that i'm using but if we used our
new fancy types this would be a
uint eight um
because we want the unsigned type which
in the old classic world would be
unsigned because we don't want to talk
about negative characters right
negative what's a negative character it
just means that you're looping back
around
yeah right it doesn't it's just doesn't
make any sense
but yes it doesn't make any sense but
like the way that i think you described
it to me is that
the reason why we want the unsigned and
the signed is we want to be able to say
that you know with the same amount of
memory
we want to represent a s the same
mathematical um absolute value if you
will like same difference
so like you know from negative 10 to 10
is 20.
and so either we can go from 0 to 20 or
we can go from negative 10 to 10. yep
and so being really specific about
whether or not we want unsigned or
signed matters because of the
range of numbers that we allow ourselves
in memory
that is correct that's just good memory
yes
um yes that's no you don't
[Laughter]
victory dance no okay that's good c
victory chance is good
um so this is normally the largest
number that we would represent this is
all one so
uh a character or you went eight in this
case because really we're only worried
about the
unsigned case um is eight bits wide
so here's eight bits i have them all set
to one
what number does this turn into you
might you may remember you may not
if you can give me a second yes so i
don't want to
don't it's not worth researching okay
yeah but um
if we if we go back a little bit to our
bitmap this is basically going to be
2 to the 8th minus 1 which is 255.
so this is a 255 with all ones
um and with all zeros
zero zero zero it's still zero
so our range is from zero to two
fifty-five if we had signed it we would
end up in this sort of like one
twenty seven two zero
we don't wanna have a negative zero this
is bad on down to
a negative 120 we stick the extra number
here um
i have to double check that i forget
yeah i think yeah all ones
is a is a negative number so it's
actually a negative
that sounds right yeah because we did
our two compliment stuff
so right so it's it's a negative if it's
signed because the first
the first bit coming from the left at
least
indicates sign that is correct
yeah you're remembering uh that's that's
good this
uh this is usually that sign bit um
we do a two's complement because we want
to avoid this problem and some other
stuff we talked about
but all we need to worry about is that
typically when we're talking about a
character we really mean an 8-bit
thing we're not going to get into the
higher code planes yet or the characters
that are in
sunny's screen name because they would
not fit
in 255 values actually
she's actually this is korean so it
would actually fit if we
encoded just those alphabetic symbols
but then there wouldn't be room for
other stuff so they're
anyway we're not getting into that we're
worried about simple cases of characters
so
the original 7-bit ascii plane and then
some extra stuff
so that's one character this means it's
an array of characters it works pretty
much the same way as a python array
except we have to manually specify the
memory
so if i want a string that's going to be
like
by the way this is sort of java syntax
when you put this right next to here so
we're not going to
we're going to put our variable name in
we're going to say something like care
you know some string so we'll just call
it str for the moment
and it's gonna be however many
characters wide we want so say like it's
five characters wide
so in memory i'm gonna get five
eight-bit things next to each other
so how many bits
sorry i was on mute um so for
that would be that's just fine
yeah and then oh well you saying we have
a length
of five or we're only specifying that
one character because if it's that one
character it's eight
that's correct one of these is eight
string
it's 40. right but this whole chunk if i
take five of them consecutive
i'm going to have 40 bits so this is
basically shorthand for saying 40 bits
and i plan on accessing it in eight bit
chunks
right which is kind of what this is
about so you know here's chunk zero
one two three four all right
this is actually the beginning of how we
represent a string we did cover this
really briefly in c i told you there's a
little bit of an oddity
with classic c strings and
that was what character goes here at the
end
if i have a four letter like say i have
like
a good four-letter word the null
character
c dec
see dec l doesn't fit so let's let's
let's
pick a a word that fits into four
like if sunny had two
didn't have a y it would be like sun
okay so we can represent five characters
because of the you know little character
right exactly there's an invisible
null character which is implicit in this
string so
this last thing is going to be all zeros
right because all zeros is the same
thing as the number zero
okay um this is kind of where a lot of c
programmers get into trouble is
forgetting about this
null byte so i'm going to tell you about
it 40 000 times
um so what we're going to do
with that is we're going to start laying
out some memory so
for all of these c functions we need to
start saying like
some simple stuff like so if we have
like two strings say
why don't you pick two strings
two strings um hello world okay so hello
and world
hello and world
okay so memory representation of these
how many bytes do they need
so they're both five letters right
and each one of those would be eight
bits which is one
byte and so the first one would need
six bytes and the seventh second one
would need oh
six as well yeah they're both they're
both five so it's a convenient example
you just chose
um so we need six bytes
because these eight bit chunks
because we have to worry about this null
thing at the end
so in memory this looks like if if i
just
expanded one of these
and each one of these lines is eight
bits then
i would have a w an o
an r an l a d and then
this null thing
and same story with hello
you know i would have h-e-l-l-o
and this null thing
okay so if we're scanning through this
if we had to write a for loop to kind of
go through this and i know
these c style for loops are new but
um how do we so we would want to sort of
like start
here and we would want to proceed down
how do we know when we're at the end of
the string
you reach the null byte yeah so we would
go
here and then we would go here and then
we would go here
and then we would go here and then we
would go here and we'd look and we'd say
uh oh
done stop
and i'll i'll even try to give you a
i was going really well to that last
line but
i was feeling it there all right so this
is a stop
okay so we have to really be careful
about this because we don't know the
size of the
of the string when we're scanning
through it um in pascal we used to store
like the string length up here in the
first byte so we would say
five and then these are the five
bytes so pascal could actually set up a
proper loop but
the problem with that was that limited
your strings to being a maximum
of however big one byte was which in
this case was
255 right assuming that you allowed a
zero length string as well
so c got away from that
c strings don't work the same way as
pascal strings they adopted this null
byte convention
so we basically have to kind of scan
through the whole thing and we have to
look for a byte and that's how we know
we're done
which between a for loop and a while
loop what does that sound more like
i feel like it sounds more like a while
lou they both work well you know while
the character isn't a null character
yeah
yeah do this action yeah except you're
also going to want to set it up
as start here
okay and each time you're going to want
to say advance by one bite
so then that leans you more towards the
four loops yeah so four is a i mean
to be honest they're both good i didn't
really mean to ask a trick question but
um it would be i would i would think of
it exactly
the way you just did i would start with
the wild because you're more worried
about a complicated terminal condition
so you're going to say while it's not
this thing
right you know while we're not in this
null byte
kind of keep going so and then we're
going to say like okay start here
and then keep going and as long as it's
not this thing
and then you know just keep on
incrementing
and that's actually how a lot of these
things get implemented so
if we were gonna do this
actually why don't we do this before we
go any further let's
let's warm up with that just to kind of
get ourselves into the
right code mode so i'm gonna switch over
to our learning
screen oh and just so you know this is
the part that we're
talking about so we're in topic five
from our coding with some guys stuff
okay you're up you're driving okay
all right so am i making a function
to print each of the letters of a given
string or am i just yeah writing okay
you gave the example
so we have two strings what are they
so i was oh okay so i was thinking more
like
i like print okay we can write an
arbitrary one that's fine
except string's not a type because it's
c
okay hold on um
you're going to get yourself into a
little bit of trouble with the function
so let's just work in main for the
moment
i'll tell you why in a second
[Music]
so hey what's up alt f4 how you doing
hi new friend um okay so here's where
i'm where i'm stuck
like i have an intuition about how to
approach this yeah
because strings aren't types in c
like my python intuition here would just
be to say you know
like i have some
some string right and so i can say for
i in range length of strength yeah i
understand that's not how things work
and see
yeah but my issue here is because we
have to implement the string length we
haven't we haven't gotten there yet
yeah and i want i want to use kind of
that null
null character logic instead of instead
of relying on kind of a length yeah
and so but
see that's good
we like declare everything and so for me
to even
think about this loop i feel like that
the thing that i am looping over needs
to be declared but i can't declare it
because it's not a type
let me let me start you off um so i'm
just going to take your example here
which is
i'm going to i'm going to call this such
a problem c
no that's you want to learn c right okay
so
here's string a and we're going to call
that one hello
exactly as we have in our example and we
can do that
sure we can't have it be a character
because it's not
what what but but it's not a character
it's a string it's a string in c is an
array of characters
and the brackets there are a cheaty way
of saying basically string
well it's actually just a ray of
characters right
okay all right so
that's all
minor friendly stream until we reach the
after hours
yes yes which case the
all right so
okay
all right so
by the way i'm just gonna i shouldn't be
showing you this super cheatison
syntax yet so we're gonna we're gonna
call this
we're gonna size these okay so that's
that's throwing me a little i'll be
honest because
to me the brackets with
the number with it with the integer
indicate
indexing but here they indicate size ah
so that's so like i would expect you're
right in both
six no they're both right um in
in the case of a declaration it
specifies a size
so this is a declaration because this is
like you're declaring variable
string a you know okay
so
[Music]
so if i did this like if i just said
remember put care
all right okay um yes strophe already
knows what i'm going to type
so if if i did put care of
string a zero
then what what do you think i'm gonna
get
out so i'm just gonna comment this out
so we can actually run
this program h yeah and
we we figure that out because you know
if we did this right
yeah we did we got our h um this
these are both senses here so this is a
like which memory address in the array
you know this is like dereferencing the
array
and so that basically says start at
string a and go zero
of these over so we know that these are
eight bits a piece
so it's going to jump forward zero times
eight which is
zero and
and that does what you think it's gonna
do if i did this
[Music]
what am i gonna get
e yeah right it's gonna it knows the
size of
one of these strings elements the size
is eight bits because they're all care
so it's just gonna jump over by that
much if i did like an
array of integers like if i did like you
know some numbers and i said okay these
are three
how big is int typically
for our case i told you you only have to
worry about yes you only have to worry
about ins being 32 from now on
so in this case it's gonna do the right
thing if i tried to like
i mean i can't use put care on it but
like it would it would know that the
size of that thing is 32 each and so it
would jump by
chunks of 32 because the declaration
gives it all that information
so it's storing like 96. yeah okay all
right
yeah um this declaration says we're
going to allocate 96 bits worth of
memory
that makes sense okay and this type just
sort of tells you
how big each one is that's kind of like
for convenience it's like a view into
memory like we talked about
and this this says there's three of them
and they should be consecutive
[Music]
consecutive oh uh in an array in what
sense
they're guaranteed to always be
consecutive memory locations
consecutive memory locations right
because so that's why when i'm drawing
on my
model here which i kind of had but this
isn't like
abstract like this is like if these
things are
like when i just did my example here
with numbers
and numbers was each one of these is an
int
and there were three of them then
each one of these lines is going to be
32 bits wide
so actually if i was going to draw this
to scale
then
this is like the int numbers array
yeah that makes sense so this first one
is 32 bits why
all of these are 32 bits wide
okay so but these are eight bits wide
because they're characters
so this is telling me the width of each
one of those positions and then this is
sort of telling me like how many
but you're guaranteed that they're going
to be right next to each other in memory
so cool that's important for later
okay no that makes sense and sorry
strafium is that
not a normal word contiguous have i been
too close to programming for too long i
thought
contiguous got used outside approach no
i think
i think so it made sense to me
consecutive versus contiguous was
actually where i was getting
mixed up so for me when you say
consecutive numbers i'm thinking that
mathematically and i thought you were
telling me
if i put numbers into an array and see
they had to be consecutive
in terms of numerically no no just in
contiguous
pieces and memories which makes sense my
bad
okay so you're fine no that's that's the
math brain taking over the programming
brain
okay so let's get the programming brain
on the four so you were back here on
your loop yes
okay right how how what what is the like
symbol
or the way to reference the null alt f4
is like i need to like make a channel
thing
for that like calm down senior engineer
like i'm just gonna have to make that
like cash in points and like
it'll just hit me or something like all
right yeah you should have like a little
like a little
you know animated thing pop up and
people have to like watch for
calm down bits or something and then
they like confuse them
i'm gonna make a note of that i like
that calm down
senior engineer
which also by the way as as a
not senior engineer person um
it is very strange for me to be viewed
in that senior position now a lot in
work so having people come to me with
python problems or data science models
and need help
that's like new new to me and it's cool
it's a cool feeling but
i mean i'm still kind of getting over
that imposter syndrome of like why are
you coming to me i don't know anything
like wait a minute i actually do know
how to solve this okay and it's a neat
feeling but
it's it's gonna take getting used to i
think
yeah ah totally um but so
back to this for loop how do i reference
um how do i how do i reference the null
character like is there sunny actually
just gave it to you in chat so
the null character can be specified like
this
sunny sunny's on your honey yeah i know
sunny's here to help this is awesome
okay so
don't read chat she says yeah it is
cheating
no she could read chat we chat a little
bit
all right so while this does not equal
cool
i like my spaces but you see people
don't like spaces between things so i'm
trying to all be proper and make it all
smushed together so no one can read it
but people who think they read see
all right um
so let's think about this start
with i zero
and then
chat is already kind of preempting where
i'm going to be taking you
i'm not watching no that's good don't
don't don't watch because they're
i'm trying to think this through their
hand this is going to be terrible
oh hi there it worked it works
so i need to well i don't think i can do
that with
print i can't do that with put care so i
might need to go print
print f for that to do the new line
because i think that's where you're
going
right oh no no you can you can put care
a new line if you want i wasn't going to
do that i was actually just going to fix
a little spacing
nothing nothing
that makes me way happier but everybody
was following last stream about how i
needed not spaces between things
trying to smush everything together yeah
now to look proper
hands hands are off the keyboard
all right i don't know if that'll work
let's see
um no okay let me clear the output
now nothing's happening okay so it's
very angry at me
wait is this because of this whole like
single thing
double thing happening in c yes i need
this to be
single quotes there we go okay i didn't
touch it
i took my hands away for a second though
so i told you
okay c reminds me of like like like the
picky auntie when you're setting the
table
it's like um excuse me
excuse me your salad plate goes on top
and you're like oh god what'd i do wrong
they just stare
with that like they don't give you any
helpful advice there's no like you know
air or anything they just kind of stare
with a condescending look of like
are you sure you're done setting the
table and then you're like oh god what i
mess up and you go back and you look
through and you find
it all right we're gonna we're gonna get
is that a nasa sweater you have on
yep that's pretty awesome
working from home i'm like comfy clothes
oh i just i've never seen
sweater knit nasa with that a lambda
thing
it's very cool okay um so we're gonna
get unsafe
because that's what chad is dying for
here um
so what we have oh hey what's up lophone
how you doing
so we have some put cares and these are
working
and you correctly nailed the we are
looking
until we hit this null terminator
and that's the right invocation now i
did warn you away from
uh yes sonny we we talked about escape
sequence i i should probably
recap it like sonny just mentioned but
um this this digraph
um being a little shouldn't be using
words
like that all right so this double
character sequence
i'm a linguist i know what a diagram i
know but it's like
i don't want to overload with technical
terms i feel like it makes the language
the language is hard enough you know
it's please if you're going to use
linguistics terms like then i'm like
i'm in the safe place okay all right
good um so this digraph
is an escape sequence which basically
says this backslash normally not
interpreted if we want to get an actual
backslash we put two of them uh
to denote that this is an essence this
is really oh this is real
really but you're only gonna get one
backslash and output so if
double backslash looks weird so um
anyway this we have a few that we worry
about we're using the digraph
for a new line like this backslash n
this is a carriage return which is a
little bit obscure but you know it
ends up getting used a bit and the one
that we really have to worry about
is this which is the signifier of the
null byte we could actually
do knowledge just zero because zero is
zero
you know like the number zero is going
to end up being the number zero yeah
like this this could be this if we
wanted to be silly we could
do this and you know it's just and then
nothing makes sense
it'll actually nothing makes sense yeah
it's nothing and it will make sense
so that's that's exactly correct so um
all right so just but sunny is right
that like this
this despite the fact it looks like two
characters the compiler's turning into
one character that's just
our representation of a single character
is the important part
so it still counts as a single 8-bit
memory location like we have up here in
the table
all right cool uh is the new line the
same way
same way yep any of these any of these
digraphs
they're they're just interpreted by the
compiler into their corresponding code
like you know t for tab um
you know carriage return you know and
any of these digraphs are
one character they're just represented
so that we can kind of read them like
because if we actually put the null by
in the middle of the editor
bad things would happen so
years of those kind of bugs so lord well
um yeah and uh
okay so let's let's go back to brass
tacks here so
we have um this this worked now before i
was sort of using this notation i just
took
this and i said i got rid of the
specifier here
and that's just a compiler shortcut that
basically says
i want because we're doing it in the
same line
as the declaration i'm telling the
compiler you count how many things are
in here
if that makes sense like that that that
just says like this is still a fixed
size it's fixed to however big this is
but this is illegal like if i had just
this alone the compiler wouldn't know
how big this
to make this array right that makes
sense yeah so like
i could either specify it i could say
it's going to be 6 and then i'm going to
assign it all like
str a sub zero is going to be
h and str a sub 1
is going to be e and you know so on
but that's annoying so we're using some
compiler syntax here
to shortcut us to just say you do that
um and the one we can go even further
with the shortcutting and basically say
look you count how many letters are
there
that's your problem so um
are you are you okay with that you're
comfortable with that yep that makes
sense it's not
random resizing though it's not like
python this does not mean that like it's
going to resize this thing like so if i
take hello and i add
world to it it's not going to resize
that memory that memory is fixed
that makes sense yes no so it's it's
it's still
fixed but it's still thick it's just
shorthand yeah that makes sense yeah
okay um cool well since we now
know how to kind of operate on this
array
if you were going to change this to to
print out how many characters there were
how would you do it
so if i were to change this to print out
how many characters there were
yeah like we want to say for example in
your example you're you're passing an
stra
so if we wanted to know how long stra
is like how many letters make it up
how would we calculate that i mean
so here you could just like you could
print i basically
yeah and it's going to iterate through
yeah print out and
print fi yeah print fi
gotta get you out of python mode uh you
need to give it a format specifier
though
oh that's right it's this thing that's
this you get to say you're
passing a number
okay frankie okay clear this output so
we'll just run that again
oh text file but yeah we both ran it at
the same time sorry
no no it's okay you you you should be
playing that's good i didn't know that
bug was there
so okay um
now it put them all in the same line for
you yes
because i mean i could i could i could
do
this because we didn't newline it right
okay
so all right cool um now the answer to
my question is actually the last line
there right
so how do you actually do that in code
because we didn't
we don't want it printed out we want to
like calculate what it is
and stick it in a number
so if we had a variable which we called
the result and we're going to set it to
zero
how do we get result to be that number
from that last line
we could build a computer vision system
to read the output
train a neural network that's that
seems way easier to me right now than
this does um
so
i guess what's throwing me is that
in a python loop there's a bit of
there isn't this kind of like while
this particular condition is not met you
continue looping
like that's that's a little confusing
for me because i guess if this were
a python loop you could just do some
kind of like you know
print the next to last one and that
would be
easy you didn't want python you wanted
to see i know
i'm just i'm like i'm going back to what
i know and trying to build from there so
like
here it's going to
break when a certain condition is met
and oh oh
my god hold on
wait getting close this is c we can't
overwrite things
sure how do they overwrite it as long as
the memory is the same yeah
all right
oh thank you for the new line i'm trying
all right oh no it's
angry oh well that makes sense that's
because i didn't do this
eye is only valid in that loop there we
go there you go
guess what you just built
this is the string length function okay
yay glad you asked guy where did the
string length
come from um if we took this thing and
we wrapped it up in a function we would
run into one quick little problem so if
we were gonna
like set this up how would you start
specifying like
i want so this is all in main so how do
i build this into a function this is
where i kind of stopped you earlier
okay so in this particular case we
actually want it to return an integer so
we can say end
um length and it's going to take a
just for fun let's call it string length
ah i wish we could do that um
i i wish no no go ahead leave it for now
um but
no if we can't do it i mean no no
just keep going keep rolling just
pretend i didn't say anything
because at a high level you're correct
oh shoot shoot thank you for saying
shoot
i'm working you know what it's been it's
monday you are like you are asking so
much of me
um
i kind of like what you did
i'm gonna tell you i'm bored
no i do okay no i i do i like what you
did
i kind of like it uh the only yeah
um just gonna get rid of some lines so
that it shows up on one screen
i could probably do that on one thing
but i wanted to be able to see it oh no
um oh my gosh
so i let me let me just save you on that
one because we're using
we're using the fancy like loop scoped
variable there
so i'm just going to bring your variable
up to not loop scope
so i'm just going to declare it outside
of this thing
which is something we didn't do in the
past number you can go back to
oh oh i see what you're doing okay sorry
my bad i'm good at it i don't think
oh because i need to do it here yeah i'm
gonna undo that
so okay so hold on i want to figure this
out so yeah
um so actually let me clear this so i
can see where even the
error begins okay function string length
error result undeclared
oh that's right thank you so i need to
go yep all right so
here we go we need to say that this is
going to be result
and that's okay so now it can't crank at
me because it's declared
i built it it worked look at it look at
its wonderfulness
um you're using a feature from newer c
but
i'm gonna translate it into older c and
that's important
for what we're about to see okay
kill me sorry um so the the issue here
is that
we're still counting on the compiler to
be smart about the sizes
um let's stop it all therefore
okay uh so the problem here is that
we're still kind of counting on
uh some sizes so this this works um
in the current compiler it's going to
allow this syntax so this
this is cool we return it we print it
out you have this right here
and we get the right number right it's
5. if we set this thing to run against
string b then it still probably works
hey not bad let's make these a little
bit different so hello world i see what
you're saying i think i think i'm
anticipating what you're saying is that
if it
if it eventually becomes a length that
requires more memory than that which
i've specified
we're going to have a problem because it
can't just adapt to whatever
this is gonna be something a little bit
different so you're close
um where this is gonna change is
this didn't this wasn't allowed in older
compilers
so we weren't allowed to pass that
around that
kind of stuff so the the modern compiler
can
sort of tell how big the zone is and
like help you so
instead we're going to be very
non-specific
about it and we're going to introduce
this star notation which you've seen
briefly before but i didn't really
explain it i said yeah we're going to
talk about that later
okay so you haven't seen this really in
other languages right
i mean it's a bit in python it exists in
python but it's
kind of weird in python isn't it
i don't know i never felt comfortable
with it in python but
um
yes terpium so i i'm going old school on
on the style here sorry about that uh it
does multiplication no
chat don't be a bad chat okay it does
not do multiplication
it does multiplication when it's like in
the middle of two numbers if you do like
two times three that does
the kind of star that you expect this is
part of why writing a c compiler is a
pain because all this
all these symbols are overloaded
depending on the context okay
so this alt f this not what all that f4
said
this star means that this is a pointer
so you're not familiar with that in
python right
i mean it exists but it's kind of weird
it's not really commonly used
okay then we're gonna just say really
fast
what what are our options for passing
this thing around
what i'm what do you mean by options for
passing this thing
oh so what we're doing is we're passing
i threw the glove on for you
we're passing this string in a sense
where
we're passing it as an argument right so
string length
string length and here it's being bound
to string right
yes okay so this
is how do we get that whole
string up there well we can copy the
whole thing we can say that okay this
only takes fixed length
string so we can say something like you
know i only deal with
strings of length six okay and it'll
it'll actually copy it right you know
it'll just say like alright here's your
six characters so when i was doing my
stack frame like we did in the other
week
then you would start saying like okay
here's my return address
and here's going to be my return value
and then i'm going to pass along my six
characters so
hello i'm having my stack grow up
because i'm just being like that today
so
except i forgot my backslash zero
digraph
so there's my null byte all right so
there's my stack frame
now in this case i had to copy the whole
thing
this is bad what if this like array
what if it was like a whole text
document this is a lot of copying right
yeah that would be okay
i mean because it's going to grow with
the size of however long
the string is so if the string is n then
this is going to
be n big
so if this thing is like 300k
that that's 300k of data i have to copy
onto the stack
that sucks so
what if we didn't want to copy all of
this frame what if instead we said okay
look
don't worry about it in the str this is
your
the function you just wrote right so
this is string length
what if instead i just said all right
you know what string length how about
here's my return this is the return
address
this is going to be my value for the
result
actually yours is defined as null so it
doesn't really here's his void so it
doesn't really matter this won't be here
um and then what if we just said all
right you know what
further down in memory not in your space
i've already allocated this thing
so rather than copying all of this stuff
onto the
other stack frame why don't i just say
look this is at memory location
7f 2b
8c 1-1
now it would never be aligned like that
actually would never be aligned like
that either
let's just say these two are zeros so
all right now now we're actually being
honest so what is this
yeah that's a scary number right
although i mean
it almost looks like i know it's not but
it almost looks like hex a little bit
it is not oh it is yeah it sucks um
we're writing in hacks because we're
writing computer memory but
you'll notice that it's made up of
four pairs of hex digits and two hex
digits we covered in the other
class represents a byte so there's four
bytes here
how many bytes is that how many what do
we have total
how many bits for four bytes yeah
32. yeah what does that remind you of
and yeah it does and actually originally
it was implemented as a name
so this is a memory address
now i know that this thing is going to
be pointing to characters so
in terms of my view i'm going to refer
to this as character because when i'm
looking at the individual things related
to it
i'm gonna de-reference them eight bits
at a time but
i'm gonna take this thing as characters
and i'm gonna say
this is the address where this starts so
why don't i just take this address and
send it to you
so instead i'm going to pass along 7f
2b 8c
0 0. oh that was okay so we can say
instead like
instead of having to know the length oh
no we still have to know the length
oh we don't you're right we don't need
to know the length of the character
right because it's it's still null
terminated here in memory right
did i lose you to chat no no i was
writing i was writing notes i'm sorry so
i'm trying to remember to mute myself so
that there's not a furious typing sound
when i'm
copying down stuff okay so
um
okay this let's go over this real quick
these 32 bits we're assuming that we
have a 32-bit memory model
in this example so this is nowadays you
would use a 64-bit model but it doesn't
really matter
it just matters if you're consistent on
your system so
this 32-bits
um refers to a particular address in
memory so this is the
address where this happened to be stored
in the other stack frame
so i want the other stack frame the one
that's in string length to be able to
access this memory without having to
copy it all over
so i can just say hey why don't i just
pass along
where it is and i'll tell you that this
is where it is i don't mean the thing
i mean where the thing is
okay like where to find it in memory
that's memory yeah okay
that's called a pointer okay
so it's just it's it's like a like a ip
address but for memory
yeah yeah that's actually they were the
same size too because
ipv4 addresses are 32 bits so
um it's it's it's it's not the memory
itself
it's where it is in memory right is the
importance
like you know your your house address
isn't
exactly your house it's where your house
is located
yeah okay yeah so we can pass that along
and say
look i'm not going to pass along the
data i don't want you to copy it right
so if i if i actually specify that this
thing was like
6 then c would actually come along and
copy the whole thing onto memory as long
as the size is matched
but i don't want it to do that i want to
instead say star is a pointer
i want to say just send a pointer in and
i'm going to know that that's a pointer
i'm going to do slightly different
syntax to remember that i'm working
with an address instead of the actual
thing
so when i feed it later when i when i
you know when in maine when i call that
function and i give it
the argument whatever argument i give it
string b
how does it know i mean this just it
like so far c has seemed very
hand-holding how does it know
the memory address how does it know
where to point
just because i'm feeding it something it
like is it is like a kind of almost like
a tag that's associated with it that it
can just read
it the pointer is the tag
the pointer literally is the location in
memory like
let's let's look so
um if we pass this thing along as a star
and by the way
if you pass it along as a care string c
allows you to de-reference it exactly
the way that we're just doing right now
so you can like you can still do string
sub-zero
it doesn't matter if it's a if it was
passed as an array like as a pointer or
if it was passed by copying it
onto the stack frame um both of these
syntaxes are valid so if i have it
defined this way
then this will copy it onto the stack
and i can refer to element zero
doing this or i could have referred to
element three but if i pass it along
as a
as a pointer then this is still valid
syntax
now the difference is we can actually
look at the pointer so just to give you
a little insight here
let's let's take a look at what this
thing is now
on our system it's probably going to be
an lld
for the actual size that we're running
so
let's just take a look at the raw
address
yeah this thing okay
that's okay i can i can cast it but um
you'll notice what it
so that that's just saying oh you might
have screwed up here
so it right now it knows that it's being
passed in the memory view is
care pointer access but i could actually
map that thing back out
right i could say
i could do the coercion right here all
right so i clear my output
i look at it boom now that's not quite
right because i have my output
here because i didn't newline it because
i'm naughty
so let's fix that all right what is this
doesn't make sense to me
just to say why not why is there
some weird parentheses stuff happening
at the beginning of the string
oh you mean this doesn't make sense this
part yeah
uh that's just when you said you were
casting is that what you meant oh yeah
that's casting that's the cast
i'm just saying i'm just matching the
types up so that they the
printf is trying to be defensive and
tell you like you might have screwed
something up
um that's just it was just a warning it
wasn't an error it still
still ran it um okay so this fits into
the right size if i if i go and i look
at this
number what is this number this this big
long number here
the first one that gets output
well from what you've been saying i
think that's just that's the memory
address
that's exactly it okay
and c's not being very handheld like
because the programmer doesn't need to
think about where in memory that thing
gets assigned
it it it's okay right it deals with it
for us
like we don't care where it's assigned
in memory we just care that it is
assigned in memory somewhere
okay so that's important
because what we're going to look at next
is
this is the the the enceladusaurus
version of this function
but i told you before i wanted to have
um manual pages available i didn't have
them
in the tool in time so instead we're
going to use
the linux manual that gets copied online
so this is a copy of typical linux
manual pages and these are normally
accessed by typing man for manual
and whatever page you want so the first
function we're going to look at
is called this actually sorry
wrong function let's call this so
strln string length
calculate the length of a string
[Music]
how's it sound for you
what do you mean how does it sound oh i
mean like it is the page
making some sense i mean so these all
have the same format it tells you first
where the thing is defined so in this
case it's pound include
string.h so this is a string.h function
so you remember before when we were in
the editor
and we i'm just going to move this tab
over here
the first line that i would always write
in the program was stdio.h
yeah and you said that that was kind of
like just some
shortcuts yeah that's where we defined
printf and putcare
and those functions okay well string
this
this particular function is a string
function so they're all included in
string.h
okay so the first thing we have to do is
we have to add that so at the top
oh uh it just means header
it's a for reasons that are historical
it's it's not no worries no it's not
important it's just
they're pretty much always going to be
dot h until you get to c plus plus
we'll get more into that when we get to
multiple files and how you write your
own routines for library
use but so this requires string.h
so if we just go looking for it it won't
necessarily be there within the compiler
okay and then we get access to a bunch
of functions one of them is this str len
and you'll notice it takes this const
thing don't worry about const that just
means that you're not allowed to
change it so um
it takes the rest of this specifier is a
care star
s the star being a pointer again
that's a pointer so that's why that was
the reason i went out of my way to show
you
the pointer syntax because all of the
classic functions are going to look like
this they're not going to use the
the brackets okay yeah strofium that's c
plus plus typically
[Music]
so what does this thing do it takes
it takes along one parameter which is
this s thing
and it tells you it calculates the
length of it looking for the null byte
which
is misrendered in this web page but
basically there's that backslash zero
hidden in the middle
um and so it returns return value
this thing returns the number of bytes
that are going to be in the string
okay so how do we use that instead of
the enceladosaurus version
so it returns a size t by the way so
that's the type you're supposed to use i
didn't know how old the standard was
that we'd be using
i mean so in this case you could just
say um
[Music]
so size t size t for our purposes is
going to be an int but
it's usually a long long size t is just
a
a number that's the size of whatever
your memory
address size is okay so it corresponds
to it
i mean you could do the same thing here
but instead of mine you could call
uh what was it string yeah let's let's
we don't need to cast it to end so
we could actually use size t so there's
no cast
okay and then what was the um the name
of it again sorry string
strllen okay
okay excuse me yeah no that's that's all
right that that's um
because the sizes don't exactly match
that's just a warning same kind of
warning that we had before
so right there is the actual size i
should have been using on this platform
it's a long unsigned int
is the size of one memory address that's
going to turn into we could actually see
do you remember the
operator to see how big a memory type
was
size of yeah so how would we know how
big
size of uh the size t type is
well you could just do size of length
this isn't this is this is not a
function even though i want it to be
um so
and it wants you to use ld for this
okay okay so it's eight
bytes which is two 32-bit values which
is 64 bits on a 64-bit computer so
that's not surprising
okay so uh that's
that's it right so we know that size t
is really just uh
you know a 64-bit number
okay so great that was surprisingly easy
once we knew what a pointer was right
pointers by the way this is a little bit
of a trap this is where c
starts to get hard i've i've
i've heard of these they weren't
pointers who warned you was it strophium
was it
the internet nathaniel was it the
internet one who's gonna do it
okay social media social media warranty
and hearing just
everybody complaining about pointers i'm
like i don't know what that is but
someday i'll be afraid of it and now it
happened and i'm like this seems
deceptively
understandable and if all these smart
people don't get it then i'm probably
missing something
now that's it's this is the concept's
not that complicated it's just like you
were saying it's the address
but the the part that gets a little
tricky is
like doing the math so we're gonna we're
gonna
we'll get more into pointer math in a
future session but we just need to know
enough about pointers to understand that
we're not copying everything on the
stock stack frame
it doesn't need to know the size it's
still going to work exactly the same way
you did it's going to keep going until
it looks for the the null byte
the actual implementation is a little
bit more complicated for speed reasons
um it typically reads depending on the
library it might read a byte
it might read 32 bits at a time it uses
an aligned access
which is kind of optimal for the
platform i've seen some pretty crazy
stuff for like arm or whatever
okay so we don't have to worry about it
but functionally it's pretty much
the same as what you wrote except it
returns a size t instead of an
int
but yeah same idea all right so now we
have pretty much the same function
signature
great um what else is there
now we know how to do the length of a
string what if we want to see if both of
these strings are equal
how would we do it
now obviously you know they're not equal
yeah
um i'm gonna get them to be the same
size so we can't use
str land we could say oh is the length
of this one the same as that one like
okay they're the same
but they're not the same string
i mean you you would have to check
character by character
right okay and we would stop
when we the null hit the null byte on
one of them
well i'm glad you mentioned that
algorithm because that's
pretty much exactly what we're going to
see it turns out it's called str compare
and like all functions it's string for
str
and the other part is shortened they
tried to make things really short back
in the day
they don't care anymore this is before
objective c
okay so this page is showing you i just
pulled up the uh
the die page here and this
this page is showing you that there's
two functions specified here
there's an str compare and there's strn
compare
we're going to get into the n variants a
lot of these functions have an n variant
which we'll we're going to get into
later so for now don't worry
about anything with the n but str
compare
takes string 1
s1 and it takes s2 and it compares the
two of them
and if they're the same what does it
return
an integer less than equal to or greater
than zero
uh
or the first n if if s2 is found
respectively to be less than
to match or greater than s2 s1
right so match is zero
exactly so this is a weird function
it's your first encounter with one it
returns zero to mean
yes
okay i warned you that there was no real
boolean type in c right you know this is
kind of
why i was warning you okay so how do we
compare these two
to be whether or not they're the same we
want to say they're the same
if they're the same and say they're not
the same if they're not the same
uh but last miles nothing is wrong with
it we're just we're not using
i was originally starting with c89
nothing nothing's wrong with bull that
was added i like how they implemented
bool as opposed to
kind of what happened in c plus plus but
commentary aside
so repeat the question sorry um let's
get
a uh let's write a simple program that's
going to compare stra and strv so i'm
going to delete this stuff
so let's just get rid of this let's get
rid of this
if stra and strb are the same
print out they're the same otherwise
print out they're not the same
so we're going to use an if else that
works kind of the same as
python okay
can i use my string length thing
oh you could write it yourself i was
going to let you use the
str compare oh yes please
yeah no that's that's great let's use
that no i showed you the man page for a
reason i i
they're like here's what it looks like
if you do it right now no no no no no
we already implemented one we
implemented str lens so
as an exercise to the reader you can
implement all the other ones if you want
but
that sounds like a physics textbook all
right
so returns an integer so result
oh little puffers upset about something
um okay i like using an intermediate
try and remember how to do it
all right um
i'm fudging this because i don't
remember how to do this
oh don't don't worry about the else just
start with the uh the true
the true case we'll we'll add the us in
a moment
we have to valid up that extra yeah
oh shoot that's my fault i'm sorry hold
on let's clear this
okay so it's mad about something or
other oh
well that's entirely my fault it's
totally fine
yeah we're looking at line numbers
though that's good
okay so they might okay in this case
they're not the same let's let's set
them to be the same let's just check
your
your true case first
okay cool now let's worry about our else
yeah there's no need to jump ahead into
a complicated construct we can we can
build it up piece by piece
looks pretty good to me
okay one stylistic thing typically if
we're if we're putting the
open brace on the same line as the if we
typically put the else on the same line
as the closed brace it's
common um it's ugly
well i mean you could be consistent if
you want to
not do that you could do older style
i mean it just pick one way or the other
i'm not
this is a little weird to read in modern
c like this is this is much more
common um
but anyway
what q command what
this alt f4 is uh telling me i'm being a
senior engineer again
so
he's he's got me
oh off f4
yeah it's it's now as last miles is
pointing out it's now become
pretty standard to write it this way so
you might as well just get in the habit
now
um okay so
str comp piece of cake right
i think so yeah that made sense okay um
the if i had a uh if i wanted to invert
this
i could torture myself and check all the
other different result types that i can
get because
if it's less than if string one is less
than string two then i get a less than
if it's greater than then i have all
those different
versions right in str compare
but if i really want to invert this
correctly
just do that
so um don't don't try to catch all the
different cases like you know
result is less than
whatever okay um
we're only really careful right now for
strings we only care about the quality
and non-equality inequality
so um we with the other versions of
string compare where it returns one is
less than or greater than the other
they're mostly used for sorting
so like when you're when you want to
compare these two and say is this one
less than or greater than the other one
that's typically what they're used for
okay
so um that's cool that was
uh and then we have one last one in that
vein which
is how do we copy a string from one
place to another
i'm guessing there's a function for that
yeah
there is it's called
str copy you starting to notice the
pattern yet
yep okay so strcpy
gives you this this is a naughty
function you can see it's already
warned right here beware of buffer
overruns but that's
that's why we're going to learn the n
versions of all these functions later
so don't worry about that right now
we're writing unsafe old style c
so str copy
now this is a common convention you have
to be really careful of it burns
basically everybody who gets into c
programming but which order is the
source
of the copy and the destination of the
copy
which one comes first
oh i see so this it's destination and
then source that's
always the case with these functions so
it's almost
always destination first then the source
that's confusing there's a couple that
will flip it later on
that we're gonna get to their kind of
edge cases but most of them follow
this whenever you have these two like
copies desks
kind of thing okay um so just be careful
about
about that the most common bug that
people run into with writing like str
copy this kind of stuff is like when
they do
they copy over they copy junk memory
which is where they want the thing to go
into the other thing because they write
it the way we write it in english and
you know reasons but
okay so if we had enough space so i'm
just going to give you i'm going to
change this up we're going to add string
c
and string c is going to be big enough
so let's just
let's just call this buff for buffer so
say we had this thing
like so if we wanted to copy string a
into buffer how do we do it so how would
you do it
your own if you didn't know about that
str copy
don't code it just talk me through it i
mean i think you would have to go
character by character again which is
kind of a
and what would you do character by
character this one's a lot easier than
compare actually
setting the i don't even know how you
could do that in c
actually sure you do you know what what
is what's at the end of the string
the null character you got it well i
know that you could like iterate through
that that makes sense but i mean can you
can you reset
characters in an array of characters as
your characters
yeah because that makes sense okay so
then yeah but we're not copying on top
of ourselves we're copying into the
destination
so um this is where it's important that
you
don't pass the things the way we did
before so before we were passing it
like first inclination was to kind of do
it sort of like this
you know so like say we had it sized
i'm editing string length yeah okay i
had to scroll up
um yes so we were doing it that way
that copied it on the stack frame which
is kind of what i showed you but then we
switched around and we said no we're
gonna
we're gonna pass the memory where this
thing is located
now that's handy because since we didn't
copy it into the stack frame
we can edit it directly from our other
function
[Music]
yes that makes sense yeah because now
we're passing the address of where it's
stored
we're not passing a copy because
normally when we were winding
up our stack like before we had like
main down here
and then we had you know string length
function and
and each of these frames only lives for
as long as the function call exists
but as soon as the the frame is done we
would throw this entire thing out
so that's not really good if we copy the
thing over
here because i'm gonna throw that memory
out you know so i
can't say like oh pass it back it's gone
from the stack but
in the case of we passed a memory
address then
now we can say okay this is actually the
memory from the other frame
so if we change memory over here we're
okay and that's part of the reason why
this thing
typically this in in the manual page
you'll see that it's being passed a
pointer
and that's so that it can kind of edit
the memory in the other stack frame
as opposed to editing its own memory
okay
yes that makes sense okay that's
important
that's really important that's actually
kind of the answer to the trick question
i asked you the other
at the end of the last stream where i
said what is how do we return more than
one value in this case we have an array
of numbers
so how do we return that well there were
a few different techniques we could copy
it around that would be using the stack
frame
but we could also do this edit thing
you know by using a pointer we can
access the original memory and we can
set it up the way we want
okay i think i think it makes sense
okay well that's okay because we could
actually
implement it ourselves would it help you
to implement it or would you rather just
like use the one that's there because i
think it's okay if you just
yeah i mean i think i think it's just
gonna it's gonna be the kind of thing
that's gonna take some reinforcement
um yeah we're gonna have to hit it a few
times i'm sure yeah
all right so using this buffer um
let's let's do it this time maybe we'll
warm up or actually that's really a good
homework kind of thing um
how do we copy string a into the buffer
um so let's see
give me one second um
buffer pointed to my destination
okay so i mean
i think we're definitely going to save
file io for next time
i just warned you about the order too
destination first
i told you it's unnatural yeah it's
definitely very weird
[Music]
i think that all right well
print out buff afterwards
okay i'm deleting some of your lines
because i don't want it to scroll off
for the viewers
oh we typed over that's a issue i have
with that
i think i have to tell it that it's
actually let me delete string length
because we already
we did that right you copied notes
uh yes i copied that okay
that's that's a lot of screen real
estate
but then doesn't this need like return
zero or something
and then it's little last one right
uh we need a format okay we can we can
use the the thing itself as the format
specifier but
um with typically when you're printing a
string out let's just
let's get in the habit of always doing a
specifier for now so
okay this will say um i am expecting you
to pass along a string parameter
and it's going to be buff
okay okay so yeah let's try it
okay so that worked yeah looks like it
was good
what if there wasn't enough memory
um then we would get that that error
that it mentions the overflow or
something if you're lucky you'll get an
error
i mean i'm not sure what else it would
do i guess maybe we would only get
a portion of it then uh by the way i i
want to reinforce i agree with last
miles on this one
actually i've agreed with a lot of what
he said but this is always be in the
habit of doing a format specifier don't
don't just
send naked strings out because if you
had like a percent
in there it would try to interpret it as
a format that's just bad
bad bad voodoo so force yourself into
the habit of always doing a
printf requires a format specifier
okay bad bad voodoo
okay um even though like in this case
it's a string it would treat the whole
thing as a literal it would work
don't do that
okay okay so right now we don't have
enough memory it's going to try to copy
it
what's going to happen i mean are we
only going to get a portion of it then
you get to keep both pieces go ahead run
it
that worked
see i told you right you're lucky if you
get a crash
that's what what you're doing is you're
actually riding into a different part of
the stack frame that you
may or may not own in this case you're
lucky there's enough room there
probably because of alignment stuff and
we're not that off but
this is like this is kind of the
beginning of danger in c and this is
part of why pointers are really kind of
nasty because
it's not safe okay you're used to like a
a language like
python which i'll check it'll say oh no
you don't have this memory so you're not
allowed to do this
like c does nothing of the sort it'll
it'll try that at runtime if you tell it
um there are pedantic modes you can put
the compiler into which will do bounds
checking
but it's not necessarily there and you
shouldn't count on it being there so
just because your code work
code works doesn't mean that it's right
and that's kind of a dangerous thing it
won't always crash
and so these kind of insidious bugs
could slip into systems all the time and
they do
so okay um
to tien oh
sorry miss warrior giggles gotta head
out see you later
and uh i see popular and what is it
mainly used for today um
lots of different stuff it's it's in
tons of libraries uh the linux kernels
and see
um there's a lot of use cases we're more
just kind of
exploring it because she wants to
learn it um
it is speed i want to be careful sonny
it is
known as being fast it's not that c is
fast it's that it's
you get exactly what you you know what
you're paying for in every
statement right so so you can kind of
see very quickly like this is
all it's going to do on this line this
is all it's going to do on this line
there's no magic
there's no like run time type whatever
it's like it's just going to run it
exactly as it is
other languages are trying to compete
for that type of performance
and level of abstraction and portability
but
anyway we're we're doing c because she
wants to learn c
all right um great
so i'm gonna leave this to you
because we took a bit of time getting
through pointers and i really should
have accounted for that properly
do you want to run into i think we know
enough about strings that we could start
working with a file um
do you want to work with files today or
should we save it for next time
um so i think just because
of this week i have more of those like
super early work meetings
okay today i'll probably say no but
let's see normally i would totally be up
for it
i got it i got it all prepped um
i've got a few system five functions
let's just take a quick look at the
system
at the berkeley function some of the
system five stuff just for
fun but it's in the same vein so we're
not going to touch the file stuff so i'm
going to
i'm going to delete it
no no no
no what no no this
um so i'm just editing it on the stream
oh okay we're not doing topic two okay
um so great let's
let's do a uh so
that was your first view into kind of
memory and copying and some of the
low-level stuff
this is all handy and it all requires
that we're working with strings
which are character rays that have one
special property in classic c
they're null terminated what if it
wasn't null terminated
we're using that null terminator to kind
of know where we're done right
we're scanning through the string and
we're saying okay here's this is here
what if i was like sending some data and
it happened to have the byte
zero in it like would my data just die
do i have to always encode if there's
zeros in my data into being safe
turn them into digraphs like it was here
i mean you said that that
that it's not equal to a zero that it's
kind of like
it's the number zero not not the numeric
ascii
digit for zero it's it's actually zero
all bits are set to zero that's the null
byte so it's zero in the
zero sense
okay so i could make that illegal memory
could never be zero if i followed the c
convention to the limit
or i could start how would i have to
change these functions that i'm running
all of them require that they're null
terminated right
yeah so what if i drop that what if i
relax that constraint it's not null
terminated
how would your code break earlier from
string length
how would your code break earlier from
string length
yeah your string length function it it
basically iterated right what did it do
well so i think the issue there is it
would never end
if you define what's it looking for it's
looking for that null byte right so what
would it have to actually
change it would need to know the length
yeah yeah so
string length would be meaningless
because it
you'd have to tell it the length for it
to know the length so but string
copy would not be meaningless right i
mean i could write a string copy that
doesn't use
a null byte at the end how would i do it
right you would need to know the length
i would have to pass along the length
and that's basically what mem copy is
so that's string copy but not
for null terminated stuff so i'm just
gonna
edit this url and i'm gonna look at mem
copy and look it's almost the same
dest and source same order
yeah we haven't gotten to memory
allocation we haven't touched the heap
yet um
chat
[Music]
so mem copy
looks almost exactly the same except one
more parameter
yep the size the size yeah and that was
same with
str like string and copy or whatever it
took a size
as well yes that's kind of the move to
non-null terminated it's sort of what
what was happening in the c library so
um
really it's really just mem copy at that
point like we're copying memory
um so they are warning you that this
stuff can't overlap right it'd be bad if
you copied like if
remember memory has to be contiguous it
has to be like a chunk so i can't
copy over myself right it would be like
weird so like
i'm copying this part but i'm also
writing this part and
there's gonna be bugs so um
there is a way to do it it basically
involves kind of like
copying it somewhere else and then
copying it back but anyway
not worrying about that so mem copy you
can think of it kind of like string copy
except it's
you know not it's not it's not it's
relaxing to constrain about null
termination
okay there's an older version this is at
the time
there were two competing standards there
was mem copy
there was also b copy which you
shouldn't use and actually i think it
tells you
don't use this um so b copy there's like
a bunch of these b
functions they kind of came out of
berkeley if i'm getting it right
um yeah they did show up in bsd so this
is berkeley had one version
of it they said okay well we're gonna do
b copy and b and all this stuff
um but don't use those
so i'm just showing you them not to show
you them the one thing you have to watch
out for what's in b
copy what order of the parameters
oh my gosh they're the opposite life
sucks on the west coast
so
so they wanted to like i guess try to
fix it and uh
att had already kind of locked it into
the standard so
moral of the story be very careful if
you're ever looking at a variant of this
like
because it could be all over the place
get in the habit of just always looking
in the manual
so it's like don't don't assume
unless you've been working with it
recently like don't assume that you know
you know what order it's gonna be
there's no
there's no consistency there's it's just
whatever someone felt like doing that
it's a convention in this case the
conventions were different so now
they're all in the standard
the great part about going off the
standard is you have a new standard
you just define a new standard so okay
um
that's that i think that's that's it for
lesson stuff so
any questions about that i don't think
so
i think i feel good about it