1 / 17

Pointers and Arrays Beyond Chapter 16

Explore the differences and similarities between pointers and arrays in programming. Learn about compiler's knowledge, pointer arithmetic, and the equivalence between pointers and arrays. Dive into dynamic memory allocation and memory layout management.

bobbymoore
Download Presentation

Pointers and Arrays Beyond Chapter 16

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. Pointers and Arrays Beyond Chapter 16

  2. Pointers and Arrays What are the real differences? Pointer Holds the address of a variable Can be pointed anywhere in memory Array A contiguous chunk of allocated ROM The Array Name is the address of where the data starts The address in the Array Name may NOT be changed E.g. It cannot point anywhere else Similarities Both passed as a reference to a function call

  3. Passing arguments to functions int function(int *intArray, int size); intArray passed in stack May perform arithmetic on intArray (e.g. intArray++)‏ int function(int intArray[], int size); Too slow to copy array into stack as argument intArray reference passed on stack (not array)‏ May perform arithmetic on intArray (e.g. intArray++)‏ Since just a pointer is passed, the function does not know how big the array is, so the size is usually passed in separately.

  4. Pointers vs Array – Compiler’s knowledge helps char charArray[6] = “world”; char *charPtr = “hello”; What do they look like? R0 = charArray[4] – translate into LC-3 R0 = charPtr[4] – translate into LC-3

  5. Arrays, and Pointers • Pointer and Array equivalence • Array name is “pointer” to first element in array int x[] = {1,2,3,4,5,6,7,8,9,10}; int *y; y = x; *y = x[3]; *x = y[6]; y[4] = *x; y = &x[4]; *y = x[9];

  6. Pointer Arithmetic int x[10] = {1,2,3,4,5,6,7,8,9,10}; int *y; y = x + 1; ++y; ++x; /* can’t do */ *y = ++x[3]; *(y+1) = x[5]++; *x = y[6]; y[4] = *x; y = &*(x+4) *y = *(x + 9);

  7. Arrays of Pointers char *names[] = {“hello”,”how”,”are”,”you?”}; What is it? What does this look like?

  8. char *names[] = {“hello”,”how”,”are”,”you?”}; char *word = names[1]; char **all = names+1; What are the values of the following? names[3][1] **names *(names[0]+3)‏ *(++word)‏ *(*(++all)+1)‏

  9. multidimensional array int myInts[3][2] = {{0,1},{5,7},{-1,-3}}; Allocated as one contiguous chunk of memory. Looks like: myInts[2][1] – compiler translates to:

  10. On your own (or with a partner): Draw the memory for the following declarations int intArray[][3] = {{2,4,6},{1,4,9},{1,3,5},{5,7,9}}; int *squares = &intArray[3][1]; int *odd = intArray[2];

  11. Returning arrays from functions Assume that str is never more than 128 characters What is wrong with this function? char *copyString(char *str)‏ { char buffer[128]; int index = 0; while ((str[index] != ‘\0’) && (index < 128))‏ { buffer[index] = str[index]; index++; } buffer[index] = ‘\0’; return buffer; }

  12. Dynamic Memory Allocation • void *malloc(size_t size) • malloc allocates size number of bytes in memory and returns a pointer to it. The memory is not cleared. • Use: char *line; int *x; line = (char *) malloc (sizof(char) * 80); x = (int *) malloc(sizeof(int) * 10);

  13. Dynamic Memory Allocation • void free(void *ptr) • free releases the memory pointed to by ptr. The memory must have been created by malloc or one of its kind. • Use: char *line; line = (char *) calloc (80, sizof(char)); . . . free(line);

  14. Memory Layout • Dynamically allocated memoryis placed on the heap • Dynamically allocated memoryis not automatically freed. Code (instructions) Global/Static Data Heap Run-time stack

  15. Fixing the code: char *copyString(char *str)‏ { int len = strlen(str); char *buffer = ________________________; int index = 0; while (str[index] != ‘\0’)‏ { buffer[index] = str[index]; index++; } buffer[index] = ‘\0’; return buffer; }

  16. How To Dynamically Allocate • char *names[] = {“hello”,”how”,”are”,”you?”}; • What does this look like?

  17. How To Dynamically Allocate • int myInts[][2] = {{0,1},{5,7},{-1,-3}}; • What does this look like?

More Related