Академический Документы
Профессиональный Документы
Культура Документы
Algol (1958)
LISP (1957)
Performance
Typically 50x faster than interpreted Java
x = y + 2;
if ( (x > 0) || ( (y-x) <= z) )
x = y + z;
MOV DX, BX
SUB DX, AX
CMP DX, CX
JLE SET_X
JMP DONE
DONE: MOV x, AX
RET
The C Programming Language
C is a higher-level language compared to
assembly
Much more human-readable
Many fewer lines of code for same task
C is a lower-level language compared to
others (like Java)
Direct control over memory allocation and cleanup
(as we will see)
Compiled vs. Interpreted
For a program to run, the source code must
be translated into the assembly language of
the machine the program will run on
Compiled language: the source code is translated
once and the executable is run. (C, Pascal)
Interpreted language: an interpreter runs each
time the program is started, and it does the
translation on-the-fly (Perl, Tcl, other scripting
languages)
Procedural vs. Object-Oriented
Procedural Programming
A program viewed as a series of instructions to
the computer
Instructions are organized into functions and
libraries of functions
Object-Oriented Programming
A program viewed as a set of objects that interact
with each other
An object encapsulates functions with the data
that the object’s functions operate on
Procedural vs. Object-Oriented
OOP good for complex systems – nice to break down
into small independent objects
int main()
{
printf(“Hello, World!\n”);
return 0;
}
Programming Style
Put brackets on separate lines by themselves
(some variations on this)
After each open bracket, increase indentation
level by one tab
After each close bracket, decrease
indentation level by one tab
Leave blank lines between sections
Don’t let lines get too long
Control Flow
Two basic types of control flow commands
Conditionals
Execute some commands if a given condition is
true/false
if, switch, ?: operator
Loops
Execute some commands repeatedly until a given
condition for stopping is met
while, for, do-while
Functions
Basic idea of functions is to divide code up
into small, manageable chunks
One way to get started designing funcs:
Write out the entire program with no functions
Look for sections of code that are almost exactly
duplicated
Create one function for each repeated section and
replace each repetition with the function call
Function Design
Look for places where several lines of
code are used to accomplish a single
task and move the code into a function
No function too small
Rule of thumb: no function (including
main) should be longer than a page
Goal: be able to see entire function at once
when editing program
So,What is a function?
Types of function
Std
UDF
Nature of function
Stack use
Recursive
Chained
…
Using variable number of arguments
•Macros that implement variable argument lists
• Declaration:
void va_start(va_list ap, lastfix);
type va_arg(va_list ap, type);
void va_end(va_list ap);
• Remarks:
•Some C functions, such as vfprintf and vprintf, take variable argument lists
in addition to taking a number of fixed (known) parameters.
•The va_arg, va_end, and va_start macros provide a portable way to access
these argument lists.
•va_start sets ap to point to the first of the variable arguments being passed
to the function.
•va_arg expands to an expression that has the same type and value as the
next argument being passed (one of the variable arguments). Because of
default promotions, you can't use char, unsigned char, or float types with
va_arg.va_end helps the called function perform a normal return.
Using variable number of arguments…
• ap
•Points to the va_list variable argument list being passed to the function.
The ap to va_arg should be the same ap that va_start initialized.
• lastfix
•The name of the last fixed parameter being passed to the called
function.
• type
•Used by va_arg to perform the dereference and to locate the following
item.
•You use these macros to step through a list of arguments when the called
function does not know the number and types of the arguments being
passed.
Using variable number of arguments…
• Calling Order
•va_start must be used before the first call to va_arg or va_end.
•va_end should be called after va_arg has read all the arguments.
•Failure to do so might cause strange, undefined behavior in your program.
return 0;
}
Limitations
Can’t assign or compare arrays
int numArray[10];
int numArrayCopy[10];
// Not OK:
numArrayCopy = numArray;
if (numArray == numArrayCopy)
// OK:
for(I=0; I<10; I++)
numArrayCopy[I] = numArray[I];
What is a pointer?
Pointer Syntax
Complicated – uses reference (&) and
dereference (*) operators
&<variable>
returns the memory address of that variable
result is a pointer to whatever type the variable is
*<pointer variable>
accesses the memory address stored in the
pointer
result is of whatever type the original variable was
Pointers
If all pointers are same size, why do we need to
specify pointer type?
Two reasons
Pointers
Pointer to basic data types
Pointers and arrays
Pointers to function
Function returning pointers
Overview
Pointers
Pointers and arrays
Pointers and strings
Null pointer
Generic pointer (void *)
Dynamic Memory Allocation
malloc()
free()
NULL – the null pointer
A pointer with the address 0x0 is a null
pointer
NULL: a constant equivalent to the address
0x0
Must NEVER dereference the null pointer –
crash will occur
Major source of bugs (common cause of ‘the
blue screen of death’)
Protecting against null pointers
Always initialize all pointers to NULL when
declaring them
NULL and the null character both equivalent
to false
Can be used in conditional statements
Typical technique:
if (str) {
// dereference str somehow
} else {
// str is NULL!
}
Generic Pointer
Pointers are all the same size underneath
Sometimes convenient to treat all pointer
types interchangeably
Generic pointer type: void *
Function that expects void * can take any
pointer as argument
Important in malloc/free
Dynamic Memory Allocation
Declaring variables of fixed size allocates
memory in a static way – variables do not
change as program runs
Potential problems
dangling pointers
memory leaks
Dangling Pointer
Pointer that is not pointing to a valid,
allocated chunk of memory
Shrink even!!
Dynamic Memory Allocation & Functions
Often want to dynamically allocate memory within a
function
CAN be done
Memory allocated with malloc() doesn’t get freed up
automatically when a function exits the way local variables
do