programming in c · 10/6/2016 · global variables defined outside functions visible to all...
Post on 07-Oct-2020
11 Views
Preview:
TRANSCRIPT
www.cs.helsinki.fi
Programming in CWeek 5
6.10.2016Tiina Niklander
6.10.2016Faculty of ScienceDepartment of Computer Science 1
www.cs.helsinki.fi
• Previous week• Discussion• Some issues /revisits
• This week• Stack, heap, scope of variables• generic type void• function pointers• generic functions• structs and unions
Structure
6 October 2016Liisa Marttinen & Tiina Niklander 2
www.cs.helsinki.fi
•17 students got all 16 points (30 students previousweek )
• almost 30 reached 13 points(40 previous week reached 11)
• almost 50 students got at least 10 points (same asprevous week (but then 10 / 12 points)• over 80 students got at least 6 points (same asprevious week)• c. 100 students got at least 1 point (same as previousweek)
Exercise feedback
6.10.2016Faculty of ScienceDepartment of Computer Science 3
www.cs.helsinki.fi
Exercise feedback
6.10.2016Faculty of ScienceDepartment of Computer Science 4
www.cs.helsinki.fi
• Why three asterisk characters in the signature?
• How to use in the call:char **linearray == NULL;int linecount = 0;read_line (&linearray, & linecount);
read_lines (***array, *size);
6.10.2016Faculty of ScienceDepartment of Computer Science 5
Need to make linearray to point tothe allocated array – need to
change the value of the pointer ->pass the address of the pointer
www.cs.helsinki.fi
The three asterisks:char *** array
6.10.2016Faculty of ScienceDepartment of Computer Science 6
January
February
main
read_lines
linearray
array
Single char is***array
*array **array
Because you need to change valueof arr (in main), you need topass its address to the function
www.cs.helsinki.fi
Stack and heap,scope of variable
6.10.2016Faculty of ScienceDepartment of Computer Science 7
www.cs.helsinki.fi
Process structureStallings:Operating systems
Process control block(PCB):
Program codeHeap for dataStack (grows from largermemory addressestowards smaller)
Memory addresses ofprocess’s own memoryarea
www.cs.helsinki.fi
Implicit usage (functionarguments and local variables)Advantage: No explicitmemory allocation or release.Disadvantage: Elements in thestack can be used only whenin the stack (= during functionexecution)Error case: dangling reference
Pointer that points to areleased memory location(in stack or in heap)
Stack (function call stack)
push
pop
top
www.cs.helsinki.fi
Explicit usage (malloc etc)Programmer is responsible formemory allocation in CMemory fragmentation ispossible
Improper or careless usagemight leak memory
Memory is a resource (like files)and it must also be managed
Heap (dynamic memory allocations)
free
www.cs.helsinki.fi
Variable definitions andscope of names
Global variablesDefined outside functionsVisible to all functions that defined laterin the codeMaintains value through the wholeprogram execution
Internal variables (block, function)Defined at the beginning of the blockVisible only within the block, not outsideMaintains value only during singleexecution of block, not whole programBlock anything within { } , also function
#include <stdio.h>/* global variables here*/double tulos;int main(int argc, char**argv){/* Function’s variables */int i;for (i=0; i < 20; i++) {/* Block’s variables*/int j;j = i*4;}
return 0;}
www.cs.helsinki.fi
Definition is in- Some other file or library- Same file, but later
Avoid usage!!
Define variables as local aspossible and at least withinone file(if necessary, use the headerfile with macro protection)
Refer to a name definedsomewhere else –> extern
int i;static int si;extern int ei;void f() {
int fi;static int sif;extern int eif; /* Avoid!*/
}void g();static void h();int eif; /* Definition here */
www.cs.helsinki.fi
Function’s internal variablepersistent and ’hidden’ -> static
With ’static’ definition thevariable maintains its valuefrom one execution time tothe other.On the other hand, ’static’limits the scope of thefunction (or variable). Itcannot be used byfunctions in other files.(like Java’s private )
int i;static int si;extern int ei;void f() {
int fi;static int sif;extern int eif; /* Avoid! */
}void g();static void h();int eif; /* Definition here */
www.cs.helsinki.fi
Extern informs compiler: ’Do not allocate space now.’ Theallocation happes, where the definition is.Static informs compiler: ’Allocate space in heap insteadof stack’. The value has to be maintained, and that canonly happen in heap area.Register informs compiler: ’Reserve a register for this ifpossible’. The variable is used so much that memoryaccess might slow down the execution.No specification = automatic allocation:
global variables in heap during compilation,local variables in stack during execution
Variable location in the memory /process’s address space
www.cs.helsinki.fi
• The figure shows the stackwhen main has calledfunction foo and the functionhas not yet returned• Frame contains
• Function arguments• Value of simple type• Address of complex type
• Local variables
Function call stack
6.10.2016Faculty of ScienceDepartment of Computer Science 15
https://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Mips/stack.html
SP – stack pointerFP – frame pointer
www.cs.helsinki.fi
void andvoid pointers
6.10.2016Faculty of ScienceDepartment of Computer Science 16
www.cs.helsinki.fi
• Generic type• No specific size• No specific interpretation• For usage: must be cast to another type
• Usually used as a pointer or argument to a function:• generic pointer (void *p) can be cast to a specific typepointer (double *)p• e.g. malloc returns a pointer of type void
Void type
6.10.2016Faculty of ScienceDepartment of Computer Science 17
www.cs.helsinki.fi
• Used when you do not want to define the type• in Struct• of Function argument
• Must know the actual size of the target element• In structs: another field to indicate the type• In functions: another argument to indicate the type or thesize
Generic pointer: void *
6.10.2016Faculty of ScienceDepartment of Computer Science 18
www.cs.helsinki.fi
void swap(void* a, void* b, size_t elem_size){char* ca = a;char* cb = b;for (size_t i = 0; i < elem_size; i++){char temp = ca[i];ca[i] = cb[i];cb[i] = temp;}return;}
Example: generic swap function
6.10.2016Faculty of ScienceDepartment of Computer Science 19
Or (using more memory):- allocate space for the whole tempelement malloc(elem_size) and- copy using memcpy(void *str1,const void *str2, size_t n) thatcopies n characters from memoryarea str2 to memory area str1.
www.cs.helsinki.fi
Function pointers
6.10.2016Faculty of ScienceDepartment of Computer Science 20
www.cs.helsinki.fi
Function pointers
• Functions also have an address and we can use thataddress as a value of a function pointer.
int (*lfptr) (char[], int);lfptr = count_letters; /* when int count_letters(char s[], int len);*/
• Function pointers can be• passed to other functions, returned from functions• stored in arrays,• assigned to other function pointers
• stdlib.h has function qsort, whose one argument is thesorting function
6.10.2016 21Faculty of ScienceDepartment of Computer Science
www.cs.helsinki.fi
/* Operations which must be implemented by each quota format */struct quota_format_ops {
int (*check_quota_file)(struct super_block *sb, int type);/* Detect whether file is in our format */
int (*read_file_info)(struct super_block *sb, int type);/* Read main info about file - called on quotaon() */
int (*write_file_info)(struct super_block *sb, int type);/* Write main info about file */
int (*free_file_info)(struct super_block *sb, int type);/* Called on quotaoff() */
int (*read_dqblk)(struct dquot *dquot);/* Read structure for one user */
int (*commit_dqblk)(struct dquot *dquot);/* Write structure for one user */
int (*release_dqblk)(struct dquot *dquot);/* Called when last reference to dquot is being dropped */
};
Function array frominclude/linux/quota.h
6.10.2016 22Faculty of ScienceDepartment of Computer Science
void main (void) {
int choice; double x, fx;
funcptr fp;
………..
funcprt function[7] = {NULL, sin, cos, tan, log , log_2, exp}; /*defined functions*/
/* print the function menu, for the use to make a selection*/
….
scanf (”%i”, &choice);
/* check that the user given value is valid*/
…
if (choice ==0) break;
printf(”Enter x: ”); scanf(”%lg”, &x);
fp = function[choice];
fx = fp(x);
printf(”\n (%g) = %g\n”, x, fx);
}
}
typedef double (*funcptr) (double );
function [0] [1] [2] [3] [4] [5] [6]
NULL sin cos tan log log_2 exp
6.10.2016 23Faculty of Science Department of ComputerScience
www.cs.helsinki.fiC-ohjelmointi
Kevät 2006Liisa Marttinen 24
Complexity of expressions?
[] has higher precedence than *
double *f[2];
double (*f2[2])()
double (*f3())[]
double *(f4[])() INCORRECT! Cannot have the functionarray, only individual function addresses.
Doublevariables
f
f2 Functionsreturningdouble value
f3 – function returns apointer to array of doubles
www.cs.helsinki.fi 6.10.2016Faculty of Science Department of ComputerScience 25
Example:function pointer as argumentFunction that can change the sort algorithms during theexecution based on number of elements
Java: ”overriding”
int (*fp) (void);int *fp() Function returns
pointer to int!Function pointer
int fname(); /* function must have same prototype */
fp = fname; /* fp() means now same function as fname()
void qsort(*line[], int left, int right, int (*comp)(void *, void*));
www.cs.helsinki.fi
Example: Function search
/* Search a block of double values */int search( const double *block , size_t size,
double value) {double *p;
if(block == NULL)return 0;
for(p = block; p < block+size; p++)if(*p == value)
return 1;
return 0;}
Go through thestructure
Pointer ascall by value
6.10.2016 26Faculty of ScienceDepartment of Computer Science
www.cs.helsinki.fi
C has no polymorfism, but we can emulate it withgeneric pointers (of type void*).
Function prototype can have all arguments and returnvalue of generic (undefined) type void
Example: Generic functionsearch
int searchGen(const void *block,size_t size, void *value);
/* Not enough *//* Must have more information and arguments */
int searchGen(const void *block,size_t size, void *value, size_t elSizeint (*compare)(const void *, const void *));
Correctprototype:
structure
Size ofelementNumber of
elementsComparison function
6.10.2016 27Faculty of ScienceDepartment of Computer Science
www.cs.helsinki.fi
Calling routine must define a Call back function
Using typed arguments in the call back function prototype
With undefined arguments the prototype must also useundefined arguments
Call back function
int comp(const double *x, const double *y) {return *x == *y;
}
int comp(const void *x, const void *y) {return *(double*)x == *(double*)y;
}
6.10.2016 28Faculty of ScienceDepartment of Computer Science
www.cs.helsinki.fi
Generic search - calling routine/* Application of a generic search */#define SIZE 10double *b; double v = 123.6; int i;int main (void) {if(MALLOC(b, double, SIZE))exit(EXIT_FAILURE);
for(i = 0; i < SIZE; i++) /* initialize */if(scanf("%lf", &b[i]) != 1) {
free(b);exit(EXIT_FAILURE);
}printf("%.2f was %s one of the values\n",v, searchGen(b, SIZE, &v, sizeof(double), comp)
== 1 ? "" : "not");return 0; /* tai exit(EXIT_SUCCESS); */}
6.10.2016 29Faculty of ScienceDepartment of Computer Science
www.cs.helsinki.fi
Generic search function
int searchGen(const void *block,size_t size, void *value, size_t elSize,int (*compare)(const void *, const void *)) {void *p;if(block == NULL)
return 0;for(p = (void*)block; p< block+size*elsize;
p = p+elsize)if(compare(p, value))
return 1;return 0;
} NOTE: Pointer operations must usethe size of the element!
6.10.2016 30Faculty of ScienceDepartment of Computer Science
www.cs.helsinki.fi
Enum
6.10.2016Faculty of ScienceDepartment of Computer Science 31
www.cs.helsinki.fi
enum – enumerated type
Enumerated constants usually represent integervalues 0,1,2,…Can start from different value than 0.
typedef enum opcodes {lvalue, rvalue,push, plus
} OpcodesT;
enum opcodes e;OpcodesT f;
int i = (int)rvalue; /*i=1*/
enum opcodes {lvalue = 1, rvalue,push, plus
};
enum opcodes e = lvalue;if(e == push) …
int i = (int)rvalue;/*i=2*/
www.cs.helsinki.fi
Enum as return value
Using enum as return valuefrom a function.Error messages in a stringtable, enum value used toindex the array.
FoperT process();
printf("result of calling process() is %s\n",Messages[TOINT(process())];
typedef enum {FOPEN, FCLOSE, FOK
} FoperT;
#define TOINT(f) ((int)(f))
char *Messages[] = {"File can not be opened","File can not be closed","Successful operation","This can not happen"
};
www.cs.helsinki.fi
Struct
6.10.2016Faculty of ScienceDepartment of Computer Science 34
www.cs.helsinki.fi
Struct
Access to struct field with ‘.’notation struct.field:
p1.age = 18;printf("%s\n", i2.firstName);
typedef struct InfoT {char firstName[20];char lastName[20];int age;
} InfoT;
InfoT p1;
struct info {char firstName[20];char lastName[20];int age;
};struct info i1, i2;
struct info {char firstName[20];char lastName[20];int age;
} k1, k2;
Preferable alternative!
www.cs.helsinki.fi
Struct within struct
typedef struct {char firstName[20];char lastName[20];int age;
} InfoT;typedef struct {
InfoT info;double salary;
} EmployeeT;EmployeeT e1;
e1:info:
salary:
firstName:
lastName:
age:
e1.info.age = 21;e1.salary = 125.6;
www.cs.helsinki.fi
Pointer to struct
Access to struct field:(*p).x or p->x
typedef struct pair {double x;double y;
} PairT, *PairTP;PairT x;PairTP p;
PairT w;PairTP q;PairTP p = &w;
if((q = malloc(sizeof(PairT))) == NULL) …if((q = malloc(sizeof(struct pair))) == NULL) …
w.x = 2;p->x = 1; (*p).x = 1; *p.x = 1;q->y = 3.5;
www.cs.helsinki.fi
Array of structs
Array items can be of any type.Access to stuct fields as withindividual structs.PairTP rectangle;PairTP aux;double x, y;
if((rectangle= malloc(4*sizeof(PairT)))==NULL)error;for(aux = rectangle; aux < rectangle + 4; aux++) {
printf("Enter two double values:");if(scanf("%lf%lf", &x, &y) != 2) /* error */
break;constructorP(aux, x, y);
}
rectangle pair
pair
pair
pair
www.cs.helsinki.fi
int i;PairTP *prectangle;for(i = 0; i < 4; i++) {
printf("Enter two double values:");if(scanf("%lf%lf", &x, &y) != 2)
error;if((prectangle[i] = constructor(x, y))
== NULL)error;
}for(i = 0; i < 4; i++)printf("vertex %d = (%f %f)\n", i,prectangle[i][0].x, prectangle[i][0].y);
Array of structsAccess to fields:
prectangle[1][0].xprectangle[1]->x(prectangle+1)->x
prectangle pair
pair
pair
1 x y
www.cs.helsinki.fi
Structs in functions:
void constructorP(PairTP this,double x, double y) {
this->x = x;this->y = y;
}
PairT w;PairTP p;
constructorP(&w, 1, 2);
if((p = malloc(sizeof(PairT))) == NULL)error;
constructorP(p, 1, 2);
Struct passed as address tothe allocated memory area.
www.cs.helsinki.fi
Structs in functions:struct as a return value
PairT constructorFunc(double x, double y) {PairT p;p.x = x;p.y = y;return p;
}PairT w = constructorFunc(1, 2.2); /*copy*/
Function can return a whole struct as return value in stack.Caller must make copy immediately, because the stack is clearedas part of the function return process.
Returned in stack -Must be copiesimmediately
Use only for small structs
www.cs.helsinki.fi
Structs in functions:return address of struct
Function must allocate space from heap for the struct and returnthe address to it.Now it is the callers responsibility to free the allocated space.
PairTP constructor(double x, double y) {/* client responsible for deallocation */
PairTP p;if((p = malloc(sizeof(PairT))) == NULL)
return NULL;p->x = x;p->y = y;return p;
}PairTP p1 = constructor(1, 2);free(p1);
Function allocates memoryand returns the address to it.Caller frees the memory later.
www.cs.helsinki.fi
Structs and functions:return address of struct
int compare(const PairTP p, const PairTP q) {return p->x == q->x && p->y == q->y;
}PairTP p2 = constructor(1, 3);PairTP p3 = constructor(2, 6);int i = compare(p3, p2);free(p2); free (p3);
Using as argument to another function:
n Avoid memory leak!
i = compare(p1, constructor(3.5, 7));
Here we loose thelocation of the structand cannot freethe memory area.
www.cs.helsinki.fi
Union
6.10.2016Faculty of ScienceDepartment of Computer Science 44
www.cs.helsinki.fi
StructFields continuous,both can have a value
UnionFields overlapping,either one can have value
union
doubleint
intAndDouble intOrDouble
double
int
struct intAndDouble {int i;double d;};
union intOrDouble {int i;double d;};
www.cs.helsinki.fi
Usually as part of structSpecial tag field indicate how tointerprete the unionUsed to save spacee.g. in communication protocolsReference to union fields using thepoint ’.’ notation
union - usage?typedef enum {
integer, real} TagTypeT;
typedef struct {TagTypeT tag;union {
int i;double d;
} value;} TaggedValueT;TaggedValueT v;
if(v.tag == integer)…v.value.i…;
else…v.value.d…;
top related