Presentation is loading. Please wait.

Presentation is loading. Please wait.

. Plab – Tirgul 5 pointers to pointers, libraries.

Similar presentations


Presentation on theme: ". Plab – Tirgul 5 pointers to pointers, libraries."— Presentation transcript:

1 . Plab – Tirgul 5 pointers to pointers, libraries

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

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

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

5 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), &copy ); (12)strcpy( copy, string); (13)}

6 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 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

7 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 }

8 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)}

9 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 );

10 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;

11 . Libraries

12 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

13 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

14 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 u ar is like tar – archive of object files u ranlib builds a symbol table for the library  to be used by the linker

15 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

16 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

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


Download ppt ". Plab – Tirgul 5 pointers to pointers, libraries."

Similar presentations


Ads by Google