(language, compilation and debugging) david 09/16/2011
TRANSCRIPT
(language, compilation and debugging)
David
09/16/2011
Content
C language overview
Compilation
debugging
Basic data type
Integer int, short, long, long long and unsigned
Char a byte has the same number of bits of char
_Bool Integer with 1 bit in memory
Floating point float, double, and long double
Other data type
Pointer type *p; store the address of memory location
array type name[dim];
struct struct ex{
int a;char b;};
const int * const p; /*a const pointer to a int*/ const int * p; /*a pointer to a const int*/
Statement
conditionals if switch-case
loops while (do-while)
while(1){} for
break/continue change the flow of execution
Functions functions written by yourself
return-type function-name ( argument-list-if-necessary ){...local-declarations......statements...return return-value;}
library functions include header files in your code
libraries stdio.h string.h stdlib.h
Example - 1
int max(int a, int b) {
if(a > b)
return a;
else
return b;
}
int factorial(int a) {
int ret = 1;
if(a < 0)
return -1;
while(a > 0)
ret *= a--;
return ret;
}
If and while statements
Example - 2int even_sum (int m) {
int sum = 0;
while (m > 0) {
switch (m%2) {
case 1:
m--;
break;
case 0:
sum += m--;
}
}
return sum;
}
switch statement
Example - 3
float thres_sum(float *array, int size, float thres){
int i;
float sum = 0;
for(i = 0; i < size; i++) {
if(array[i] <= thres)
continue;
else
sum += array[i];
}
return sum; }
for statement and pointer
Example - 4
#define PI 3.1415926
typedef struct cylinder Cy;
Cy max_volumn (Cy cy1, Cy cy2) {
float vol1, vol2;
vol1 = PI * cy1.r * cy1.r * cy1.H;
vol2 = PI * cy2.r * cy2.r * cy2.H;
if(vol1 > vol2)
return cy1;
else
return cy2;
}
Struct and typedef
struct cylinder {
float r;
float H;
};
Recursive function call
a function that contains a call to itself. Divide complex problem into identical
simple cases. Must have at least one exit condition that
can be satisfied. i.e. Towers of Hanoi
int factorial (int n) {
if(n == 1)
return 1;
else
return n * factorial(n-1);
}
Pass by value vs. pass by address
How are parameters passed to a function? pass by value: make a copy of the variable
Simple Can not change the passed variable
pass by address: address of the variable is passed Efficient Can change the variable, though the address of the
variable is not changed
void swap1(int a, int b) {
int c;
c = b;
b = a;
a = c;
}
void swap2(int *a, int *b) {
int c;
c = *b;
*b = *a;
*a = c;
}
Example - 5
Pass by value and pass by reference
int main() {
int a = 3, b = 5;
swap1(a,b);
printf(“a is %d, b is %d\n”, a, b);
swap2(&a, &b);
printf(“a is %d, b is %d\n”, a, b);
return 0;
}
Example - 5 (continued)
Pass by value and pass by reference
Dynamic memory allocation
Typical variable definition (known size) Statically defined in program Allocated in the stack i.e., int a[10]
Define a variable that has a varying size? Allocated at run-time, in the heap Use malloc() function call to allocate Use free(*pointer) to free the memory of a
variable
int main() {
int *array, i, size;
printf(“Input the array size:”);
scanf(“%d”, &size);
array = (int *)malloc(size, sizeof(int));
for(i=0; i < size; i++)
array[i] = i;
free(array); //otherwise, memory leak
return 0; }
Example - 6
Dynamic memory allocation
Preprocessing directives
#define Defines a macro. #include Inserts text from another source file. #ifdef Conditionally includes source text if a macro name
is defined. #ifndef Conditionally includes source text if a macro
name is not defined. #ifndef FILENAME_H
#define FILENAME_H…#endif
#else Conditionally includes source text if the previous #ifdef, #ifndef test fails.
#endif Ends conditional text.
I/O capability
FILE *fp; fp = fopen(name, mode); fclose(fp); fprintf(fp, "format string", variable list); fgets(str_name, length, fp); feof(fp);
while(!feof(fp)) {fgets(line, max_length, fp);…
}
Example - 7
int print_triangle(char style, int base, char *filename) {
int i,j;
FILE *fp = fopen(filename, “w+”); // open file
if(fp == NULL)
return -1;
if(lines % 2 == 0) base--;
for(i = 1; i <= base; i=i+2) {
for(j = 0; j < (base – 1) / 2; j++)
fprintf(fp, ‘ ‘);
for(j = 0; j < i; j++)
fprintf(fp, “%c”,style);
fprintf(fp, “\n”);
}
fclose(fp); return 0; }
****
************
**********
File operations
Command-line arguments
main(int argc, char** argv) a.out -i 2 -g -x 3 argc = 6
argv[0] = "a.out"argv[1] = "-i"argv[2] = "2"argv[3] = "-g"argv[4] = "-x"argv[5] = "3"
Compilation
gcc codename –o programname make prog = cc –o prog prog.c options
-o filename -g produce extra debugging information -O(0/1/2/3) optimization level -I(i)directory add header file directory -l(L)libraryname link the library
Example - 8
#include <stdio.h>
#include <math.h>
int main() {
double s3 = sqrt(3);
printf(“Square root of 3 is:%lf\n”, s3);
return 0;
}
Library UsageSource code: example.c
Compilation gcc –g –o example example.c –lm
Makefile
Multiple files compilation files
main.c factorial.c hello.c
Run make
#Makefile_example
all: hello
hello: main.o factorial.o hello.o
gcc main.o factorial.o hello.o -o hello
main.o: main.c
gcc -c main.c
factorial.o: factorial.c
gcc -c factorial.c
hello.o: hello.c
gcc -c hello.c
clean:
rm -rf *o hello
Another makefile
CC=gcc CFLAGS=-c -Wall SOURCES=main.c hello.c factorial.cOBJECTS=$(SOURCES:.c=.o)EXECUTABLE=helloall: $(SOURCES) $(EXECUTABLE)$(EXECUTABLE): $(OBJECTS)
$(CC) $(OBJECTS) -o $@ .c.o:
$(CC) $(CFLAGS) $< -o $@
http://mrbook.org/tutorials/make/
Common compilation errors
Typos (C language is case sensitive) Try to use a variable without definition Some statements are not ended by ; Brackets are not paired ( ), { }, [ ] Number or type of parameters passed to a
function does not match the function definition.
Still have errors? copy the error message, Google it.
gdb for debugging
-g in compilation to enable gdb gdb obj //start gdb with obj program (gdb) run (arg <arguments>) (gdb) s for step execution (gdb) p for print variables (gdb) br to set breakpoint (gdb) set to modify registers or memory
set $eax=10 (gdb) help
Most powerful debug tool
printf function print out check points near vulnerable
codes to trace down where bug appears i.e, File operation, control statements...
print out value of variables to trace down when bug happens
i.e. invalid array operation, loop does not stop, and etc.
For more information
http://www.ic.sunysb.edu/Stu/xdeng/c.htm Email: [email protected] Google