Presentation is loading. Please wait.

Presentation is loading. Please wait.

Some examples.

Similar presentations


Presentation on theme: "Some examples."— Presentation transcript:

1 Some examples

2 Pointer to function Why? Example: qsort function in <stdlib.h>
To develop general functions To change function operation in run-time Example: qsort function in <stdlib.h> void qsort(void *arr, int num, int element_size, int (*compare)(void *, void *)) To sort array arr with num elements of size element_size. The order between elements is specified by the “compare” function Example1 2

3 #include <stdio.h> #include <stdlib.h>
int int_cmp_asc(void *i1, void *i2){ int a = *((int *)i1); int b = *((int *)i2); return (a > b) ? 1 : (a == b) ? 0 : -1; } int int_cmp_dsc(void *i1, void *i2){ return (a > b) ? -1 : (a == b) ? 0 : 1; Example1 3

4 qsort(arr, 5, sizeof(int), int_cmp_asc);
int main(void){ int i; int arr[] = {1, 7, 3, 11, 9}; qsort(arr, 5, sizeof(int), int_cmp_asc); for(i = 0; i < 5; i++) printf("%d \n", arr[i]); qsort(arr, 5, sizeof(int), int_cmp_dsc); return 0; } Example1 4

5 Multi-Dimensional Arrays
scanf(" %d ", &a[0][0])  scanf(" %d ", a[0]) printf (" %d ", a[0][0])  printf(" %d ", *a[0]) scanf(" %d ", &a[2][2])  scanf(" %d ", a[2]+ 2) printf (" %d ", a[2][2])  printf(" %d ", *(a[2] + 2)) int a[row][col]; a[row][col]  *(*(a + row) + col) a[row][col]  *(a[row] + col) &a[row][col]  a[row] + col a  a[0][0]  a[0] a[0] + 2 [0][9] [0][8] [0][7] [0][6] [0][5] [0][4] [0][3] [0][2] [0][1] [0][0] a[0] [1][9] [1][8] [1][7] [1][6] [1][5] [1][4] [1][3] [1][2] [1][1] [1][0] a[1] [2][9] [2][8] [2][7] [2][6] [2][5] [2][4] [2][3] [2][2] [2][1] [2][0] a[2] [3][9] [3][8] [3][7] [3][6] [3][5] [3][4] [3][3] [3][2] [3][1] [3][0] a[3] [4][9] [4][8] [4][7] [4][6] [4][5] [4][4] [4][3] [4][2] [4][1] [4][0] a[4]

6 #include <stdio.h>
#include <stdlib.h> int main(void){ int i, j, n, m; int **arr; printf("Enter n, m: "); scanf("%d%d", &n, &m); arr = (int **)malloc(n * sizeof(int *)); for(i = 0; i < n; i++) arr[i] = (int *)malloc(m * sizeof(int)); for(i = 0; i < n; i++){ for(j = 0; j < m; j++){ arr[i][j] = i * j; printf("%d ", arr[i][j]); } printf("\n"); free(arr[i]); free(arr); return 0; برنامه‌اي كه n و m را مي‌گيرد، ماتريس nxm را توليد و بعد حافظه را آزاد مي‌كند Example2 6 6 6

7 Stack Example typedef int T;
#define MAX_STACK 100 /* const int MAX_STACK = 100; */ T stack[MAX_STACK]; int top = 0; T item = 10; stack[top++] = item; // push ... item = stack[--top]; // pop Obviously not very abstract...

8 Data Hiding Implementation in C
/* File stack.c */ #include "stack.h" static int top, size; /* Hidden within this file. */ static T *stack; /* Hidden within this file. */ int create (int len) { top = 0; size = len; stack = (T*)malloc (size * sizeof (T)); return stack == 0 ? -1 : 0; } void destroy (void) { free ((void *) stack); } void push (T item) { stack[top++] = item;} void pop (T *item) { *item = stack[--top]; } Void Top (T *item) { *item = stack[top - 1]; } int is_empty (void) { return top == 0; } int is_full (void) { return top == size; }

9 Data Hiding Implementation in C
Use case Main problems: The programmer must call create() first & destroy() last! There is only one stack & only one type of stack /* File main.c */ #include "stack.h" void main(void) { T i; create (5) ; push (10); /* Oops, forgot to call create! */ push (20); pop (&i); destroy (); }

10 Data Abstraction Implementation in C
/* File stack.h*/ typedef int T; typedef struct { size_t top, size; T *stack; } Stack; int Stack_create (Stack *s, size_t len); void Stack_destroy (Stack *s); void Stack_push (Stack *s, T item); void Stack_pop (Stack *, T *item); /* Must call before pop’ing */ int Stack_is_empty (Stack *); /* Must call before push’ing */ int Stack_is_full (Stack *); /* ... */

11 Data Abstraction Implementation in C
/* File stack.c */ #include "stack.h" int Stack_create (Stack *s, size_t len) { s->top = 0; s->size = len; s->stack = malloc (size * sizeof (T)); return s->stack == 0 ? -1 : 0; } void Stack_destroy (Stack *s) free ((void *) s->stack); s->top = 0; s->size = 0; s->stack = 0; void Stack_push (Stack *s, T item) { s->stack[s->top++] = item; } void Stack_pop (Stack *s, T *item) { *item = s->stack[--s->top]; } int Stack_is_empty (Stack *s) { return s->top == 0; }

12 Data Abstraction Implementation in C
Use case /* File main.c */ #include "stack.h" void main(void) { Stack s1, s2, s3; /* Multiple stacks! */ T item; Stack_pop (&s2, &item); /* Pop from empty stack */ /* Forgot to call Stack_create! */ Stack_push (&s3, 10); s2 = s3; /* Disaster due to aliasing!!! */ /* Destroy uninitialized stacks! */ Stack_destroy (&s1); Stack_destroy (&s2); }

13 calloc Memory allocation by calloc
#include <stdlib.h> void * calloc(int num, int size); void * is generic pointer, it can be converted to every pointer type Initializes allocated memory to zero If memory is not available calloc returns NULL

14 Example of malloc and calloc
int n = 6, m = 4; double *x; int *p; /* Allocate memory for 6 doubles. */ x = (double *)malloc(n*sizeof(double)); /* Allocate memory for 4 integers. */ p = (int *)calloc(m,sizeof(int)); X p

15 Example int *pi; /*allocate memory, convert it to int * */ pi = (int *) malloc(sizeof(int)); if(pi == NULL){ printf("cannot allocate\n"); return -1; } double *pd; pd = (double *) calloc(1,sizeof(double));

16 malloc and calloc Both functions return a pointer to the newly allocated memory If memory can not be allocated, the value returned will be a NULL value The pointer returned by these functions is declared to be a void pointer A cast operator should be used with the returned pointer value to coerce it to the proper pointer type Dynamically allocated memory created with either calloc() or malloc() doesn't get freed on its own. You must explicitly use free() to release the space.

17 malloc vs. calloc The number of arguments:
malloc() takes a single argument (memory required in bytes), while calloc() needs two arguments. Initialization: malloc() does not initialize the memory allocated, while calloc() initializes the allocated memory to ZERO.

18 Reallocation If we need to change the size of allocated memory
Expand or Shrink it void * realloc(void *p, int newsize); Allocate newsize bytes for pointer p Previous data of p does not change

19 realloc Example float *nums; int I;
nums = (float *) calloc(5, sizeof(float)); /* nums is an array of 5 floating point values */ for (I = 0; I < 5; I++) nums[I] = 2.0 * I; /* nums[0]=0.0, nums[1]=2.0, nums[2]=4.0, etc. */ nums = (float *) realloc(nums,10 * sizeof(float)); /* An array of 10 floating point values is allocated, the first 5 floats from the old nums are copied as the first 5 floats of the new nums, then the old nums is released */

20 500 100 printf("%d\n", *(p+1)); *(p + 1) = 100; int *p;
p = (int *)calloc(2, sizeof(int)); printf("%d\n", *p); *p = 500; printf("%d\n", *(p+1)); *(p + 1) = 100; p = (int *)realloc(p, sizeof(int) * 4); printf("%d\n", *p); p++; 500 100


Download ppt "Some examples."

Similar presentations


Ads by Google