Plab Tirgul 5 pointers to pointers libraries Pointers

Plab – Tirgul 5 pointers to pointers, libraries .

Pointers to pointers (1) int i=3 (2) int j=4; (3) int k=5; i: 3 (4) int *ip 1 = &i; (5) int *ip 2 = &j; j: 4 ip 1: k: 5 ip 2: (6) int **ipp = &ip 1; ipp:

Pointers to pointers (1) int i=3 (2) int j=4; (3) int k=5; i: 3 (4) int *ip 1 = &i; (5) int *ip 2 = &j; j: 4 ip 1: ip 2: (6) int **ipp = &ip 1; (7) ipp = &ip 2; k: 5 ipp:

Pointers to pointers (1) int i=3 (2) int j=4; (3) int k=5; i: 3 (4) int *ip 1 = &i; (5) int *ip 2 = &j; j: 4 ip 1: ip 2: (6) int **ipp = &ip 1; (7) ipp = &ip 2; (8) *ipp = &k; k: 5 ipp:

pointers to pointers: example //put pointer to an allocated string in pp (1) int alloc. String(int len, char ** pp) { (2) char *str = malloc(len + 1); (3) if (str==NULL) (4) return -1; (5) *pp = str; (6) return 0; (7) } // copy a string using “alloc. String” (8) void main() { (9) char *s = “example”; (10) char *copy = NULL; (11) alloc. String( strlen(s), © ); (12) strcpy( copy, string); (13) }
![Multi-dimensional arrays Can be created in few ways: 1. Automatically: u int arr[5][7]; · Multi-dimensional arrays Can be created in few ways: 1. Automatically: u int arr[5][7]; ·](http://slidetodoc.com/presentation_image_h2/744f492f2e2283df0d11b56f4aef075d/image-6.jpg)
Multi-dimensional arrays Can be created in few ways: 1. Automatically: u int arr[5][7]; · 3 rows, 5 columns · continuous memory (divided to 5 blocks) · access: arr[row][col] = 0; u When sending ‘arr’ as an argument to a function, only the 1 st index can be omitted: · · · void void func( func( int int int x[5][7] ) x[][] ) * x[] ) ** x ) //ok //error (always) //error
![Multi-dimensional arrays 2. Semi-dynamic: u Define an array of pointers: int *pa[5]; // allocates Multi-dimensional arrays 2. Semi-dynamic: u Define an array of pointers: int *pa[5]; // allocates](http://slidetodoc.com/presentation_image_h2/744f492f2e2283df0d11b56f4aef075d/image-7.jpg)
Multi-dimensional arrays 2. Semi-dynamic: u Define an array of pointers: int *pa[5]; // allocates memory for 5 pointers for (i=0; i<5; i++) { pa[i] = (int*) malloc( 7*sizeof(int) ); // pa[i] now points to a memory for 7 ints }

Multi-dimensional arrays 3. Dynamically: (1) int ** array; (2) array = (int**) malloc( 5*sizeof(int*) ); (3) for (i=0; i<5; i++) { (4) arr[i] = malloc( 7*sizeof(int) ); (5) }

Multi-dimensional arrays Dynamically allocated multi-dimensional array: u Memory not continuous u Each pointer can be with different size u Access: arr[ i ][ j ] u Don’t forget to free all the memory for (i=0; i<nrows; i++ ) { free( array[i] ); array[i] = NULL; } free( array );

pointers to … We also have pointers to pointers, etc. double ** mat 1 = get. Matrix(); double ** mat 2 = get. Matrix(); //allocate an array of matrices double *** matrices = (double***) malloc( n*sizeof( double**) ); matrices[0] = mat 1; matrices[1] = mat 2;

Libraries .

Libraries u Library is a collection of functions, written and compiled by someone else, that you may want to use u Examples: · C’s standard libraries · Math library · Graphic libraries u Libraries may be composed of many different object files

Libraries 2 kinds of libraries: u Static libraries: · linked with your executable at compilation time · standard unix suffix: . a u Shared libraries: · loaded by the executable at run-time · standard unix suffix: . so

Static libraries Using the static library libdata. a: g++ -o prog object 1. o object 2. o –ldata Creating the library data. a (2 commands): ar rcu libdata. a data. o stack. o list. o ranlib libdata. a u ar is like tar – archive of object files u ranlib builds a symbol table for the library · to be used by the linker

static vs. shared Static libraries pros: u Independent of the presence/location of the libraries u Less linking overhead on run-time Shared libraries pros: u Smaller executables u No need to re-compile executable when libraries are changed u The same executable can run with different libraries u Dynamic Library Loading (dll) possible

Libraries in makefile libdata. a: ${LIBOBJECTS} ar rcu libdata. a ${LIBOBJECTS} ranlib libdata. a OBJECTS = foo. o bar. o CC = g++ prog: ${OBJECTS} libdata. a ${CC} ${OBJECTS} –ldata –o prog

Learn by yourselves u Keywords: register, volatile u Pointer casting · and the void* pointer
- Slides: 17