c programming 2
play

C Programming 2 2 Lab Schedule Activities Deadlines This Week - PowerPoint PPT Presentation

Computer Systems and Networks ECPE 170 Jeff Shafer University of the Pacific C Programming 2 2 Lab Schedule Activities Deadlines This Week Lab 4 Due by Feb 19 th 5:00am Intro to C 2 Lab 4 C Programming


  1. ì Computer Systems and Networks ECPE 170 – Jeff Shafer – University of the Pacific C Programming 2

  2. 2 Lab Schedule Activities Deadlines This Week Lab 4 ì ì Due by Feb 19 th 5:00am Intro to C 2 ì ì Lab 4 – C Programming ì Lab 5 ì Project Due by Feb 26 th 5:00am ì Computer Systems and Networks Spring 2019

  3. 3 Pointer Arithmetic ì Only addition and subtraction are allowed with pointers ì All pointers increase and decrease by the length of the data-type they point to ì Example If an integer pointer, iptr holds address 32, then ì after the expression iptr++ , iptr will hold 36 (assuming integer is 4 bytes). Computer Systems and Networks Spring 2019

  4. 4 Problem 1 The name of the array is actually a pointer pointing to the first element of the array. Subscript [0] [1] [2] [3] [4] Value 5 6 4 8 2 Address 65528 65532 65536 65540 65544 Consider an integer array named array . printf(“\n %u:”, array); //prints 65528 printf(“\n %u:”, array+2); //prints 65536 printf(“\n %u:”, *(array+1)); //literally translates to array[1]. Prints 6 printf(“\n”, %u:”, array+3); //prints?______ P1 printf(“\n”, %u:”, *(array+3)); //prints?______ Computer Systems and Networks Spring 2019

  5. 5 Pointers and Functions: Call by value vs. Call by reference Call by value Call by reference (pointer) main(){ main(){ a=5,b=6; a=5,b=6; update(a,b); update( & a, & b); printf(“%d”,a); printf(“%d”,a); } } update(int a, int b) { update(int * a,int * b) { a=a-b; * a= * a- * b; } } Modification to actual variable These are just copies. No change to original variables Computer Systems and Networks Spring 2019

  6. 6 ì Dynamic Memory Management Computer Systems and Networks Spring 2019

  7. 7 Memory Allocation with malloc() ì #include <stdlib.h> ì void * malloc (int size) Allocate region in memory (aka “new”) ì Argument: Size of region in bytes to allocate ì Return value: Pointer to the region ì ì void free (void * ptr) De-allocate region in memory (aka “delete”) ì Argument: Pointer to the region ì Computer Systems and Networks Spring 2019

  8. 8 Memory Allocation with malloc() ì void * calloc (int count, int size) Basically the same as malloc! ì ì Imagine you want an array of elements… Argument 1: # of elements to allocate ì Argument 2: Size of each element in bytes ì Return value: Pointer to the region ì Computer Systems and Networks Spring 2019

  9. 9 Memory Allocation with malloc() ì void * realloc (void *ptr, int size); Resize a dynamic region of memory ì ì Note that it might move to a new address! Argument: Pointer to the original region ì Argument 2: Desired size in bytes of new region ì Return value: Pointer to the new region ì ì It might be at the same address if you made it smaller ì It might be at a new address if you made it larger Computer Systems and Networks Spring 2019

  10. 10 Malloc – 1D int *array; //array of integers array (pointer variable) value: ???? 60 pointer addr: 32 array = (int *)malloc(sizeof(int)*5); address: 60 64 68 72 76 value: array[0] array[1] array[2] array[3] array[4] Computer Systems and Networks Spring 2019

  11. 11 Malloc – 2D Allocate 4x5 integers (Hint for lab 4) int **array; //a double pointer array = (int **)malloc(sizeof(int *)*4); for(i=0;i<4;i++) array[i] = (int *)malloc(sizeof(int)*5); array of ints array of ints array of ints array of ints an array of integer pointers Computer Systems and Networks Spring 2019

  12. 12 Malloc – 3D int ***array; //a triple pointer a ‘cuboid’ of integers an array of a matrix of double pointers single pointers Computer Systems and Networks Spring 2019

  13. 13 Problem 2 ì Dynamically allocate space for a 3-D color image of width, w; height, h; color channel, c. Any pixel is accessed as image[height][width][c] . P2 Computer Systems and Networks Spring 2019

  14. 14 ì Memory Management Internals Computer Systems and Networks Spring 2019

  15. 15 Memory Management ì Who implemented malloc() ? ì C Standard Library: #include <stdlib.h> ì There are different C Standard Library implementations! Android: Bionic ì Apple: BSD-based / Proprietary ì Microsoft: Proprietary C Runtime Library ì Linux: GNU C Library (glibc) ì http://www.gnu.org/software/libc/ Computer Systems and Networks Spring 2019

  16. 16 Memory Management ì Where does the malloc() memory come from? ì The Heap: A region of memory for dynamic memory allocation ì Per-process – each program gets its own heap ì Managed by malloc() and related functions ì Different from the stack , which is for static variables ì (known at compile-time) Computer Systems and Networks Spring 2019

  17. 17 Memory Management malloc() outline: ì Call malloc() and request memory 1. 2. malloc() checks existing heap size Sufficient? Update bookkeeping to mark space as ì “used” and return address to your program Insufficient? ì Call operating system via brk()/nmap() to grow 1. the heap (plus a little extra for future requests) Update bookkeeping and return address to your 2. program Computer Systems and Networks Spring 2019

  18. 18 Memory Management ì Why do we need to call free() after calling malloc() ? Memory leak ì malloc() cannot re-use that space ever, because ì its internal bookkeeping still thinks that region is used Will only be recovered upon terminating program ì ì Operating system wipes out all the memory allocated to your process (stack, heap, etc…) Computer Systems and Networks Spring 2019

  19. 19 Memory Management 0xFFFFFFFFFFFFFFFF (32 or 64 bit) OS creates virtual ì memory space for process when started Region is huge (full 32 ì Virtual Memory Space or 64 bit space) for new process Not fully mapped to ì physical memory Otherwise you ì could only fit 1 program in memory 0x0000000000000000 Computer Systems and Networks Spring 2019

  20. 20 Memory Management 0xFFFFFFFFFFFFFFFF (32 or 64 bit) OS loads in the ì program from disk “Text” region ì Program code ì “Data” region ì Program fixed ì data Data (Program data) Text (Program code) 0x0000000000000000 Computer Systems and Networks Spring 2019

  21. 21 Memory Management 0xFFFFFFFFFFFFFFFF (32 or 64 bit) Stack created to ì Stack track program function calls and local variables Data (Program data) Text (Program code) 0x0000000000000000 Computer Systems and Networks Spring 2019

  22. 22 Memory Management 0xFFFFFFFFFFFFFFFF (32 or 64 bit) Heap created to ì Stack store dynamic memory from malloc() and (Unused / unmapped virtual memory) related functions Not to scale – ì this unused Heap region is huge! Data (Program data) Text (Program code) 0x0000000000000000 Computer Systems and Networks Spring 2019

  23. 23 Memory Management 0xFFFFFFFFFFFFFFFF (32 or 64 bit) Program starts ì Stack running ì malloc() (Unused / unmapped virtual memory) allocates some memory Heap Data (Program data) Text (Program code) 0x0000000000000000 Computer Systems and Networks Spring 2019

  24. 24 Memory Management 0xFFFFFFFFFFFFFFFF (32 or 64 bit) Original heap ì Stack space eventually fills up (Unused / unmapped virtual memory) malloc() ì requests New space additional space Heap from the kernel by using brk() system call Data (Program data) Text (Program code) 0x0000000000000000 Computer Systems and Networks Spring 2019

  25. 25 Memory Management 0xFFFFFFFFFFFFFFFF (32 or 64 bit) ì free() Stack deallocates blocks from the heap (Unused / unmapped virtual memory) Heap Data (Program data) Text (Program code) 0x0000000000000000 Computer Systems and Networks Spring 2019

  26. 26 Memory Management 0xFFFFFFFFFFFFFFFF (32 or 64 bit) Program ì Stack terminates OS expunges ì (Unused / unmapped virtual memory) entire virtual address space Everything is ì Heap deleted Data (Program data) Text (Program code) 0x0000000000000000 Computer Systems and Networks Spring 2019

  27. 27 Buffer Overflow Vulnerability ì What is a buffer overflow bug? ì char buf1[8]=""; char buf2[8]=""; strcat(buf1, "excessive"); ì End up overwriting two characters beyond buf1 ! Computer Systems and Networks Spring 2019

  28. 28 Buffer Overflow Vulnerability ì Why is a buffer overflow bug dangerous? ì What is beyond my buffer in memory? Other variables and data? (probably buf2 ) ì The stack? (further out) ì The return address to jump to after my function ì finishes? ì If app is running as administrator, attacker now has full access! Computer Systems and Networks Spring 2019

  29. 29 Memory Management Limitless opportunities in C for errors regarding memory ì L Forgetting to free() some dynamic memory ì Trying to free() dynamic memory more than once ì Losing a pointer to dynamic memory (memory is “lost”) ì Accessing array elements past the end of the array ì Mis-calculating array pointers that miss their desired ì target Will learn a tool ( Valgrind ) in Lab 5 to analyze your ì program and detect / trace errors Computer Systems and Networks Spring 2019

  30. 30 What’s the Error? char *a = malloc(128*sizeof(char)); char *b = malloc(128*sizeof(char)); b = a; free(a); free(b); http://www.yolinux.com/TUTORIALS/C++MemoryCorruptionAndMemoryLeaks.html Computer Systems and Networks Spring 2019

Recommend


More recommend