Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pointers and Arrays Beyond Chapter 16

Similar presentations


Presentation on theme: "Pointers and Arrays Beyond Chapter 16"— Presentation transcript:

1 Pointers and Arrays Beyond Chapter 16

2 Pointers and Arrays What are the real differences? Pointer Array
Holds the address of a variable Can be pointed anywhere in memory Array A contiguous chunk of allocated RAM 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)‏ 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”; or char *charPtr = “hello”; What do they look like? R0 = charArray[4] – translate into LC-3 ADD R0, R5, #-6 ; get address of start of array (no memory access) LDR R0, R0, #4 ; get fourth element of array. R0 = charPtr[4] – translate into LC-3 LDR R0, R5, #0 ; get ptr address LDR R0, R0, #4 ; dereference pointer AND R0, R5, #-6 ; get address of start of array (no memory access) LDR R0, R6, #4 ; get fourth element of array. LDR R0, R5, #0 LDR R0, R0, #4

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]; C Style Arrays Fixed at declaration (compile time)

6 Pointer Arithmetic int x[10] = {10,9,8,7,6,5,4,3,2,1}; int *y;
y = x + 1; /* y => &x[1] */ ++y; /* y => &x[2] */ ++x; /* can’t do */ *y = ++x[3]; /* *y = ++x[3] = 8 */ *(y+1) = x[5]++; /* *(y+1) = x[3] = 5*/ *x = y[6]; y[4] = *x; y = &*(x+4) *y = *(x + 9); x[10]=? Pointer Arithmetic takes into account the type of pointer it is. That’s one of the reasons why we must declare pointers to be a certain type Pointer to integer will (on most systems) increase the address stored in y by 4 bytes Can point to any native type or user defined type such as structures * = follow the pointer & = take the address of [] = follow the pointer to the correct location Difference between array name and pointer Prefix = increment first, then use Postfix = use first, then increment

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

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: ADD R4, R5, #-6 ;assuming myInts is the only variable LDR R4,R4,#5 ; row*numberofcols + col col + row*numcols

10 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 (sizeof(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, sizeof(char)); . free(line);

14 Memory Layout Code (instructions) Global/Static Data Heap
Dynamically allocated memory is placed on the heap Dynamically allocated memory is not automatically freed. Code (instructions) Global/Static Data Heap In C, whatever you put on the heap stays there until you free it Run-time stack

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

16 How To Dynamically Allocate
char *names[] = {“hello”,”how”,”are”,”you?”}; char **dynStrings = (char**) malloc (sizeof(char*)*4); dynStrings [0] = (char*) malloc(sizeof(char)*6); strcpy(dynStrings[0], “hello”); etc… What does this look like? char **dynStrings = (char**) malloc (sizeof(char*)*4); dynStrings [0] = (char*) malloc(sizeof(char)*6); strcpy(dynStrings[0], “hello”); etc…

17 How To Dynamically Allocate
int myInts[][2] = {{0,3},{5,7},{-1,-3}}; int **dynArray = (int**) malloc (sizeof(int*)*3); dynArray[0] = (int*) malloc(sizeof(int)*2); dynArray[0][0] = 0; dynArray[0][1] = 3; dynArray[1] = (int*) malloc(sizeof(int)*2); Etc. What does this look like? int **dynArray = (int**) malloc (sizeof(int*)*3); dynArray[0] = (int*) malloc(sizeof(int)*2); dynArray[0][0] = 0; etc.


Download ppt "Pointers and Arrays Beyond Chapter 16"

Similar presentations


Ads by Google