- The last video in this course will discuss
the development of portable code.
So, first let's see what do we mean by that.
Portability is a great issue with programming
because portable code can run on
more machines than non-portable code.
It is less dependent on one operating system or another,
less dependent on a style of programming,
less dependent on alphabets,
does code in the US, run the same as code in Russia,
or the same in China, and this character sets and so on.
So, let's look at some of the issues.
First is the size of data types.
In some languages, and in some computers,
same data types have different sizes.
So, we have to know that eight bit on a Mac is the same
as eight bit on Linux or Windows and so on.
The other one is increment vs. decrement variables.
Because we can assign a value to x
and then at the same time increment x with a ++ sign by one.
Are we using the right x in the next iteration?
What about signed vs. unsigned variables?
And we'll see something very interesting related
to that when we are performing right-shift on sign values.
The other one is Byte order.
So, when you define a struct with several fields,
it is not guaranteed that those fields will
form a contiguous array of memory locations.
So, it is our opportunity to detect that
and do something about it.
Finally, there's the Byte order because, well, again,
when you define struct it is not clear whether,
let's say you have two vectors or two data points,
x and y, it is not clear whether x is tor before y or after.
Let's see some of the things we
can discuss about this in code.
So, first we going to define a constant max character
to be equal ten and I will show you
immediately what is this for.
So, let's look at some declarations here.
First, I would like to print the size
of the types long, int, and float.
This is not a random thing.
When I showed you the previous window,
I said that one of the issues that we need
to discuss is the size of the various data types
and ensure that they are the same across
multiple operating systems and types of computer.
So, we are putting size of
long, size of int, and size of float.
Printing the line and then moving on.
Now, we have two arrays.
One has the letters a, b, c, d, e.
The other the numbers 1, 2, 3, 4, 5.
We know that if we assign letters[n] n,
the value at symbols[++n] it should be the same,
however in addition to that,
I am incrementing n before I'm using it,
and then I'm creating the values.
I'm repeating the process, line 32.
Now, I'm doing the same thing with letters
and symbols but a reverse addition.
So, in line 29, I had [++n].
Line 35, I had [n++], and I again print
the contents of letters without the one.
We have to be careful with signed and unsigned code.
Let's try now for what we have
and then we'll draw some conclusions.
So, first, we recall, we started with the sizes.
So, we have the size of long is 8, size of int is 4,
size of float is also 4, and we can move on.
Now, let's look at the letters.
So, n=1, because it used to be zero.
Now, they're apparently there's
some relationship between 1 and n.
It's not obvious where it's coming from.
I'm sure the debugging process
we discussed earlier will come in handy.
The other one is letters[0] and the third one is shifting,
and this I expect not have problems at all.
Let's focus on the shifting where we have 5
is shifted one bit to the left and it equals 2 now.
And -5 it's also shift the same amount
and it's now equals -3.
So, it's a big difference between 2 and -3,
so, let's see what this might be attributed to.
Notice, in both cases, I'm shifting the number 5,
except in the second case I am shifting
the opposite of the number.
If this is consistent and we can live with that,
but apparently it isn't.
Other things you can do in your code is
to notice different structs of memory layout.
So, it is known that bytes and chars
and so on takes a certain amount of space.
It's not clear whether the price
and title below in the struct book
they always take the same price of the struct book
with all those have the same price.
Second, you should look at
portability across operating system.
For example, the end of line in text file is \n.
However, on Windows it could be \n but it could be \r.
Now, ASCII is only used in English speaking countries.
What about Chinese, Japanese, where they
use a different notation and a char type is sixteen bits?
So, you can see we have quite a few things to think about.
We have some solution to them,
some food for thought, but the more mindful
we are of those things, even if we
don't have a solution right away,
the better collectively the programming community will be.