Beruflich Dokumente
Kultur Dokumente
Enrico Bini
University of Turin
3 C: Functions
4 Memory management
3 C: Functions
4 Memory management
3 C: Functions
4 Memory management
...
p_i = (int *)p;
Example of explicit conversion in expressions
if (3/2 == (int)(3/2.0)) {
printf("VERO :-)\n");
} else {
printf("FALSO :-(\n");
}
...
p_i = (int *)p;
Example of explicit conversion in expressions
if (3/2 == (int)(3/2.0)) {
printf("VERO :-)\n");
} else {
printf("FALSO :-(\n");
}
test-celsius.c
3 C: Functions
4 Memory management
The names of the input variables are not needed in the declaration
int min ( int , int ) ;
int main () {
int a ;
a = min (4 , -2) ;
}
int main () {
int a , b =4;
a = mul (b ,3) ;
}
Comments on recursion
I Every recursive algorithm can be implemented by standard iterations
I Recursive algorithms occupy a significant amount of memory (stack)
due to nested parameter passing
I Compilers are much better at optimizing for loops rather than
recursive functions
Since C focuses on efficiency, recursion is very rarely used
3 C: Functions
4 Memory management
A special care must be taken to free a memory area before the pointer
to the area is lost
p = malloc ( N * sizeof (* p ) ) ;
...
p = & v ; // the ref to the allocated mem is
lost !!