170 likes | 287 Views
Plab – Tirgul 5 pointers to pointers, libraries. 4. 3. j:. 5. i:. k:. ip1:. ip2:. (4) int *ip1 = &i; (5) int *ip2 = &j;. ipp:. (6) int **ipp = &ip1;. Pointers to pointers. (1) int i=3 (2) int j=4; (3) int k=5;. 4. 3. j:. 5. i:. k:. ip1:. ip2:. Pointers to pointers.
E N D
4 3 j: 5 i: k: ip1: ip2: (4) int *ip1 = &i; (5) int *ip2 = &j; ipp: (6) int **ipp = &ip1; Pointers to pointers (1) int i=3 (2) int j=4; (3) int k=5;
4 3 j: 5 i: k: ip1: ip2: Pointers to pointers (1) int i=3 (2) int j=4; (3) int k=5; (4) int *ip1 = &i; (5) int *ip2 = &j; (6) int **ipp = &ip1; (7) ipp = &ip2; ipp:
4 3 j: 5 i: k: Pointers to pointers (1) int i=3 (2) int j=4; (3) int k=5; (4) int *ip1 = &i; (5) int *ip2 = &j; ip1: ip2: (6) int **ipp = &ip1; (7) ipp = &ip2; ipp: (8) *ipp = &k;
pointers to pointers: example //put pointer to an allocated string in pp (1) int allocString(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 “allocString” (8) void main() { (9) char *s = “example”; (10) char *copy = NULL; (11) allocString( strlen(s), © ); (12) strcpy( copy, string); (13) }
Multi-dimensional arrays Can be created in few ways: 1. Automatically: • int arr[5][7]; • 3 rows, 5 columns • continuous memory (divided to 5 blocks) • access: arr[row][col] = 0; • When sending ‘arr’ as an argument to a function, only the 1st index can be omitted: • void func( int x[5][7] ) //ok • void func( int x[][7] ) //ok • void func( int x[][] ) //error (always) • void func( int * x[] ) //error • void func( int ** x ) //error
Multi-dimensional arrays 2. Semi-dynamic: • 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 amemory 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: • Memory not continuous • Each pointer can be with different size • Access: arr[ i ][ j ] • Don’t forget to free all the memory for (i=0; i<nrows; i++ ) { free( array[i] ); array[i] = NULL;}free( array );
pointers to pointers to … We also have pointers to pointers to pointers, etc. double ** mat1 = getMatrix(); double ** mat2 = getMatrix(); //allocate an array of matrices double *** matrices = (double***) malloc( n*sizeof( double**) ); matrices[0] = mat1; matrices[1] = mat2;
Libraries • Library is a collection of functions, written and compiled by someone else, that you may want to use • Examples: • C’s standard libraries • Math library • Graphic libraries • Libraries may be composed of many different object files
Libraries 2 kinds of libraries: • Static libraries: • linked with your executable at compilation time • standard unix suffix: .a • Shared libraries: • loaded by the executable at run-time • standard unix suffix: .so
Static libraries Using the static library libdata.a: g++ -o prog object1.o object2.o –ldata Creating the library data.a (2 commands): ar rcu libdata.a data.o stack.o list.o ranlib libdata.a • ar is like tar – archive of object files • ranlib builds a symbol table for the library • to be used by the linker
static vs. shared Static libraries pros: • Independent of the presence/location of the libraries • Less linking overhead on run-time Shared libraries pros: • Smaller executables • No need to re-compile executable when libraries are changed • The same executable can run with different libraries • 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 • Keywords: register, volatile • Pointer casting • and the void* pointer