1 / 17

Plab – Tirgul 5 pointers to pointers, libraries

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.

roza
Download Presentation

Plab – Tirgul 5 pointers to pointers, libraries

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Plab – Tirgul 5pointers to pointers,libraries .

  2. 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;

  3. 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. 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;

  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: • 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

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

  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: • 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 );

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

  13. 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

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

  15. 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

  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 • Keywords: register, volatile • Pointer casting • and the void* pointer

More Related