Upload
drusilla-carpenter
View
213
Download
0
Embed Size (px)
Citation preview
Pointers and Arrays Beyond Chapter 16
16-2
Pointers and ArraysWhat 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
16-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.
16-4
Pointers vs Array – Compiler’s knowledge helpschar 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
Arrays, and PointersPointer 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];
16-5
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);
16-6
16-7
Arrays of Pointerschar *names[] = {“hello”,”how”,”are”,”you?”};
What is it? What does this look like?
16-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)
16-9
multidimensional arrayint myInts[3][2] = {{0,1},{5,7},{-1,-3}};
Allocated as one contiguous chunk of memory. Looks like:
myInts[2][1] – compiler translates to:
16-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];
16-11
Returning arrays from functionsAssume 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;
}
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);
16-12
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);
16-13
Memory Layout• Dynamically allocated memory
is placed on the heap• Dynamically allocated memory
is not automatically freed.
Code (instructions)
Global/Static Data
Heap
Run-time stack
16-14
16-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;
}
How To Dynamically Allocatechar *names[] = {“hello”,”how”,”are”,”you?”};
What does this look like?
16-16
How To Dynamically Allocateint myInts[][2] = {{0,1},{5,7},{-1,-3}};
What does this look like?
16-17