Docsity

Pointers, Arrays and the Stack - Lab III | CSCI 1200, Lab Reports of Data Structures and Algorithms

Toggle sidebar
bg1
CSCI-1200 Computer Science II — Fall 2008
Lab 3 — Pointers, Arrays, and the Stack
This lab explores the use of pointer arithmetic, allocation of single value and array variables on the stack,
passing arguments by reference and by value, and the C calling convention. Please have your notes from
Lecture 5 available for this lab.
Checkpoint 1
Write a function compute squares that takes 3 arguments: two arrays, aand b, of integers, and an integer,
n, representing the size of each of the arrays. The function should square each element in the first array, a,
and write each result into the corresponding slot in the second array, b. You may not use the subscripting
operator ( a[i] ) in writing this function. Also, write a main function and a couple of test cases with
output to the screen to verify that your function is working correctly.
To complete this checkpoint: Show a TA your function, the test cases, and the corresponding output.
Checkpoint 2
What will happen if the length of the arrays is not the same as n? What will happen if nis too small? If
nis too big? What if the aarray is bigger than the barray? Or vice versa? How might the order that the
variables were declared in the main function impact the situation? First think about all of these questions
and draw pencil & paper pictures of the memory. Jot down your hypotheses before testing.
Now let’s print out the contents of memory and see what’s going on. In the file “lab3 code.cpp” we provide
the definition of the function print stack that will help us see how variables and arrays are allocated on
the stack. For example, the code on the left will result in output similar to that shown on the right (the
exact memory addresses will vary).
int x = 72;
int a[5] = {10, 11, 12, 13, 14};
int *y = &x;
int z = 98;
cout << "x address: " << &x << endl;
cout << "a address: " << &a << endl;
cout << "y address: " << &y << endl;
cout << "z address: " << &z << endl;
print_stack(&x,&z);
x address: 0xbfbfe95c
a address: 0xbfbfe930
y address: 0xbfbfe92c
z address: 0xbfbfe928
-----------------------------------------
location: 0xbfbfe95c VALUE: 72
location: 0xbfbfe958 garbage?
location: 0xbfbfe954 garbage?
location: 0xbfbfe950 garbage?
location: 0xbfbfe94c garbage?
location: 0xbfbfe948 garbage?
location: 0xbfbfe944 garbage?
location: 0xbfbfe940 VALUE: 14
location: 0xbfbfe93c VALUE: 13
location: 0xbfbfe938 VALUE: 12
location: 0xbfbfe934 VALUE: 11
location: 0xbfbfe930 VALUE: 10
location: 0xbfbfe92c POINTER: 0xbfbfe95c
location: 0xbfbfe928 VALUE: 98
-----------------------------------------
Typically the local variables will be allocated on the stack in order (note that the stack on x86 architectures
is in descending order). You can see the elements of the array, but since the first element of the array
is stored in the smallest memory location the array looks upside down. Also you might see extra space
between the variables due to temporary variables or padding inserted by the compiler to improve alignment.
pf2

Partial preview of the text

Download Pointers, Arrays and the Stack - Lab III | CSCI 1200 and more Lab Reports Data Structures and Algorithms in PDF only on Docsity!

CSCI-1200 Computer Science II — Fall 2008

Lab 3 — Pointers, Arrays, and the Stack

This lab explores the use of pointer arithmetic, allocation of single value and array variables on the stack, passing arguments by reference and by value, and the C calling convention. Please have your notes from Lecture 5 available for this lab.

Checkpoint 1

Write a function compute squares that takes 3 arguments: two arrays, a and b, of integers, and an integer, n, representing the size of each of the arrays. The function should square each element in the first array, a, and write each result into the corresponding slot in the second array, b. You may not use the subscripting operator ( a[i] ) in writing this function. Also, write a main function and a couple of test cases with output to the screen to verify that your function is working correctly.

To complete this checkpoint: Show a TA your function, the test cases, and the corresponding output.

Checkpoint 2

What will happen if the length of the arrays is not the same as n? What will happen if n is too small? If n is too big? What if the a array is bigger than the b array? Or vice versa? How might the order that the variables were declared in the main function impact the situation? First think about all of these questions and draw pencil & paper pictures of the memory. Jot down your hypotheses before testing.

Now let’s print out the contents of memory and see what’s going on. In the file “lab3 code.cpp” we provide the definition of the function print stack that will help us see how variables and arrays are allocated on the stack. For example, the code on the left will result in output similar to that shown on the right (the exact memory addresses will vary).

int x = 72; int a[5] = {10, 11, 12, 13, 14}; int *y = &x; int z = 98; cout << "x address: " << &x << endl; cout << "a address: " << &a << endl; cout << "y address: " << &y << endl; cout << "z address: " << &z << endl; print_stack(&x,&z);

x address: 0xbfbfe95c a address: 0xbfbfe y address: 0xbfbfe92c z address: 0xbfbfe


location: 0xbfbfe95c VALUE: 72 location: 0xbfbfe958 garbage? location: 0xbfbfe954 garbage? location: 0xbfbfe950 garbage? location: 0xbfbfe94c garbage? location: 0xbfbfe948 garbage? location: 0xbfbfe944 garbage? location: 0xbfbfe940 VALUE: 14 location: 0xbfbfe93c VALUE: 13 location: 0xbfbfe938 VALUE: 12 location: 0xbfbfe934 VALUE: 11 location: 0xbfbfe930 VALUE: 10 location: 0xbfbfe92c POINTER: 0xbfbfe95c location: 0xbfbfe928 VALUE: 98


Typically the local variables will be allocated on the stack in order (note that the stack on x86 architectures is in descending order). You can see the elements of the array, but since the first element of the array is stored in the smallest memory location the array looks upside down. Also you might see extra space between the variables due to temporary variables or padding inserted by the compiler to improve alignment.

This extra space may be correctly labeled as “garbage” by the print stack function (as shown above), or it might contain old data values or addresses that appear to be legal.

Using the print stack command before and after a call to your compute squares function should help you understand how the compiler is organizing the memory for your local variables and function arguments. First try this on a correct test case to make sure you can correctly interpret the output. Then try it on one of the bad cases described at the beginning of this checkpoint that has incorrect behavior. Make sure you understand how the memory error occurs.

NOTE: Do not compile with optimizations enabled. By default g++ does not use optimizations.

To complete this checkpoint: Show a TA the output of both your correct and incorrect test cases and describe how the print stack output corresponds with your predicted behavior.