Homework

advertisement
Homework
• Turn in HW2 at start of next class.
• Starting Chapter 2 K&R. Read ahead.
• HW3 is on line.
– Due: class 9, but a lot to do!
– You may want to get a head start.
• Questions?
1
Call by Value vs Call by Reference
• Simple variables passed as function parameters
in C are actually only copies on the stack.
(Note: Stack pointer is a register)
foo(i, j);
Unused
Stack
Data
Stack Pointer
Before call and after return
Copy of Value
void foo(int i, int j)
Unused
{
Stack Pointer
}
Return
Data
After call and before return
i j
Stack
Data
2
Call by Value vs Call by Reference
• This is known as Call by Value.
• Unlike some languages, which have call by
reference
• You can't change arguments in original location
within the function -- just change the stack copy
• To make changes, you must pass pointers to
original variables. See next slide.
• You’re not responsible for pointers yet, but start
getting used to them!
3
Pointers as Arguments
• Pointer variables passed as function parameters
in C are still only value on the stack but can
be used to access original location indirectly
Stack
foo(&i, &j);
Unused
Data
Stack Pointer
Before call and after return
void foo(int *i, int *j)
{
Unused
}
Point to values
Return
&i &j
Data
Stack Pointer
After call and before return
Stack
Data
4
Pointers as Arguments
• The following doesn’t work!!!
void exchgint (int a, int b)
{
int dummy;
dummy = a;
a = b;
b = dummy;
}
5
Pointers as Arguments
• Must be done with pointers!!!
void exchgint (int *pa, int *pb)
{
int dummy;
dummy = *pa;
*pa = *pb;
*pb = dummy;
}
6
Pointers as Arguments
• An array name is automatically passed as a pointer
• You don't need to create a pointer yourself with the
“address of” operator (&)
int array1[256], array2[256];
foo(array1, array2);
• Unless you are passing a pointer to a specific array
element (Remember printf in visitype?)
foo(&array1[4*i], &array2[4*j]);
7
Scope of Variables – Local Automatic
• Each local variable of a function (inside { })
comes into existence only when it is called
disappears when a return is performed.
• Local variables are said to be automatic.
• Memory is allocated on the stack after the
calling sequence argument values
Unused
Local
Variables
Return
Data
i j
Stack
Data
Stack Pointer
After local variables are allocated
8
Scope of Variables – Local Static
• A static variable declared in a function is assigned a fixed
location in memory that is NOT on the stack.
• Still local - can only be used inside function { } but its value
is preserved between successive calls to the function
• E.g., the seed of a random number generator so it will have
memory from one invocation to the next and not always
give the same random number.
int rand( )
{
static int seed = 1;
}
/* initialize to 1 and remember
value between calls to rand */
9
Scope of Variables – Local Static
• Good for implementing a “Finite State Machine”
– They allow a function to preserve its state value(s)
• But, be careful using local static variables!!
– A function may behave differently when it is called
with different values preserved in local static variables
– Makes testing of a function more complex because you
need to test its behavior with all possible values of any
local static variables
– Complex sequence of calls to function may be required
to drive local static variables into desired test “state(s)”
10
Scope of Variables - External
• Alternative to local is known as external
• Look at maxline program on page 32 of K&R
• Variables declared ahead of/outside of all { }’s
are available to all functions within the program
• These are sometimes called “global variables”
• Can be used to pass data between functions
• Values are preserved like static local variables
• Not a good idea – Why not?
11
Scope of Variables - External
• Global variables are not a good idea because:
– They can be accessed from anywhere. If their value
gets corrupted, it is NOT easy to figure out what code
in which function caused the corruption.
– They make the functions dependent on their external
environment instead of being able to stand alone using
arguments to get their input values and a return value
to pass back an output value.
• Software architecture/design standards for most
projects will prohibit use of “global variables” or
severely restrict their use. Don’t use them!! 12
Scope of Variables – External Static
• Another alternative known as external static
• No good examples that I see in K&R
• Static variables declared ahead of/outside of all { }’s
are available to all functions within this file only
• These are very similar to OOP “class variables”
• Can be used to pass data between functions in file only
• Values are preserved like static local variables
• These are more acceptable than global variables
13
Data Types
• Finished K&R Chapter 1 / Now starting Chapter 2
• Variable Names
• Data Types:
–
–
–
–
–
–
char
int
short int
long int
float
double
14
Variable / Symbolic Constant Names
• Rules for generating names:
–
–
–
–
All letters and digits are allowed
Uppercase and lower case letters are different
First character must be a letter
Underscore _ counts as a letter
(useful for improving readability)
– But, don’t begin variable names with _
(reserved for library routines)
15
Variable / Symbolic Constant Names
• Rules for generating names:
–
–
–
–
Internal - at least 31 characters are significant
External - only count on 6 characters, single case
Avoid using C keywords (if, else, int, float, etc.)
Choose variable names that are related to the
purpose or use of the variable
16
Data Types
• char (character)
char has 8 bits (stored in one byte in memory)
unsigned:
0 ≤ value ≤ 28 -1
00000000 ≤ value ≤ 11111111
Overflow at 255
(255 + 1 = 0)
Underflow at 0
(0 – 1 = 255)
signed (if supported in the implementation):
-27 ≤ value ≤ 27-1
10000000 ≤ value ≤ 01111111
Overflow at 127
(127 + 1 = -128)
Underflow at –128 (-128 – 1 = 127)
17
Data Types
• int (integer on our machines)
int has 32 bits (stored in four sequential bytes in memory)
unsigned:
0 ≤ value ≤ 232 - 1
0x00000000 ≤ value ≤ 0xffffffff
Overflow at 4294967295
(4294967295 + 1 = 0)
Underflow at 0
(0 – 1 = 4294967295)
signed:
-231 ≤ value ≤ 231-1
0x80000000 ≤ value ≤ 0x7fffffff
Overflow at 2147483647
(2147483647 + 1 = –2147483648)
Underflow at –2147483648
(-2147483648 – 1 = 2147483647)
18
Data Types
• short int (short integer on our machines)
short int has 16 bits (stored in two sequential bytes in memory)
unsigned:
0 ≤ value ≤ 216 - 1
0x0000 ≤ value ≤ 0xffff
Overflow at 65535
Underflow at 0
(65535 + 1 = 0)
(0 – 1 = 65535)
signed:
-215 ≤ value ≤ 215-1
0x8000 ≤ value ≤ 0x7fff
Overflow at 32767
Underflow at –32768
(32767 + 1 = –32768)
(-32768 – 1 = 32767)
19
Data Types
• long int (long integer on our machines – same as int)
long int has 32 bits (stored in four sequential bytes in memory)
unsigned:
0 ≤ value ≤ 232 - 1
0x00000000 ≤ value ≤ 0xffffffff
Overflow at 4294967295
(4294967295 + 1 = 0)
Underflow at 0
(0 – 1 = 4294967295)
signed:
-231 ≤ value ≤ 231-1
0x80000000 ≤ value ≤ 0x7fffffff
Overflow at 2147483647
(2147483647 + 1 = –2147483648)
Underflow at –2147483648
(-2147483648 – 1 = 2147483647)
20
Data Types
• float, double
– Both represent numbers with fractional parts
– “double” is a “float” with more precision
– Don't do much with float or double in this course
• In absence of a co-processor to do floating
point computations, it takes a lot of compute
time to do them in software.
– Not often used in real time, embedded systems.
– A cost versus performance tradeoff!
21
Download