Lab Manual for EEL 3801: Pointers, Dynamic Memory, Pass by Reference, and Arrays, Lab Reports of Computer Architecture and Organization

This laboratory manual for eel 3801, introduction to computer engineering, covers the topics of pointers, dynamic memory, pass by reference, and arrays. Students are required to complete lab assignments involving the use of pointers to allocate dynamic memory, fill arrays with characters, and pass arguments to functions by value and reference.

Typology: Lab Reports

Pre 2010

Uploaded on 11/08/2009

koofers-user-v8c
koofers-user-v8c 🇺🇸

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Laboratory Manual
EEL 3801
Lab #7
Laboratory Assignment #7
EEL 3801 Introduction to Computer Engineering
Topic: Pointers, Dynamic Memory, Pass by Reference, and Arrays.
Turn in: .CPP file with screen output.
Pointers, Dynamic Memory, Pass by Reference, and Arrays
A pointer variable has the capability of holding an address. A pointer is the same thing as an
address you have already seen in assembly language. It refers to the number of bytes the data is
offset from the beginning of the segment. It may also refer to the segment as well as the offset. It
depends on the model that you are using. Using an *, the dereference operator, in front of a
pointer means that you are referring to the contents pointed to by the pointer and not the pointer
itself. The &, called the address operator, in front of a variable means that you are referring to
the address of the variable not it's contents. This is much like the offset and seg directives in
assembly language.
Example
int X, *P; // X is declared as an integer and P as a pointer
// capable of holding the address of an integer.
P = &X; // P points to X or it contains the address of X
X = 5; // X gets 5.
*P = 6; // X gets 6. P stays with the address of X
P = 7; // P gets 7. X stays with 6
Pointers are used for a variety of functions. The next three sections are common examples of the
many uses of pointers.
1. Dynamic Memory
Some of the more popular computer platforms support dynamic memory allocation. This is
where the program asks the operating system for a block of memory during execution. The
following line of code allocates sufficient memory to implement an integer. Note the new
function returns the address of the newly allocated memory so it must be received by a pointer
variable.
int * Pnt = new int;
To give the memory back to the operating system you use the delete function. The following
returns the memory back to the operating system.
delete Pnt;
1
pf3
pf4
pf5

Partial preview of the text

Download Lab Manual for EEL 3801: Pointers, Dynamic Memory, Pass by Reference, and Arrays and more Lab Reports Computer Architecture and Organization in PDF only on Docsity!

EEL 3801 Lab # Laboratory Assignment # EEL 3801 Introduction to Computer Engineering Topic: Pointers, Dynamic Memory, Pass by Reference, and Arrays. Turn in: .CPP file with screen output. Pointers, Dynamic Memory, Pass by Reference, and Arrays A pointer variable has the capability of holding an address. A pointer is the same thing as an address you have already seen in assembly language. It refers to the number of bytes the data is offset from the beginning of the segment. It may also refer to the segment as well as the offset. It depends on the model that you are using. Using an *, the dereference operator, in front of a pointer means that you are referring to the contents pointed to by the pointer and not the pointer itself. The &, called the address operator, in front of a variable means that you are referring to the address of the variable not it's contents. This is much like the offset and seg directives in assembly language. Example int X, *P; // X is declared as an integer and P as a pointer // capable of holding the address of an integer. P = &X; // P points to X or it contains the address of X X = 5; // X gets 5. *P = 6; // X gets 6. P stays with the address of X P = 7; // P gets 7. X stays with 6 Pointers are used for a variety of functions. The next three sections are common examples of the many uses of pointers.

1. Dynamic Memory Some of the more popular computer platforms support dynamic memory allocation. This is where the program asks the operating system for a block of memory during execution. The following line of code allocates sufficient memory to implement an integer. Note the new function returns the address of the newly allocated memory so it must be received by a pointer variable. int * Pnt = new int; To give the memory back to the operating system you use the delete function. The following returns the memory back to the operating system. delete Pnt;

EEL 3801 Lab # a. To check this out, dynamically allocate an array of 10 characters. b. Fill the array with the letters A, B, C and so on. c. Finally print the contents of the array and delete the array. d. Turn in the .CPP file and screen output.

EEL 3801 Lab # void bill(int k) { printf("The value passed to bill is %d\n",k); k = 10; printf("The value passed to bill after being modified is %d\n",k); }; void main( ) { int x = 5; printf ("The value of x before bill is %d\n",x); bill (&x); printf ("The value of x after bill is %d\n",x); } The output is: The value of x before bill is 5 The value passed to bill is 5 The value passed to bill after being modified is 10 The value of x after bill is 10 TURN IN: .CPP file and screen output. Your task is to copy the program in example 3 and run it and explain the results in the comments of the program. The function bill will print the contents of x and y of which the contents of y is the address of x so use %X instead of %d. Example 3: void bill(int x, int *y) { printf ("The arguments passed to bill are x = %d y = %X\n",x,y); x = x + 5; *y = *y + 5; printf ("The arguments after being modified are x = %d y = %x\n",x,y); printf ("What got modified is x = %d y = %d\n",x,y); } void main( ) { int a = 5; int b = 10; printf ("The values before bill are a = %d b = %d\n",a,b); bill (a,&b); printf ("The values after bill are a = %d b = %d\n",a,b);

EEL 3801 Lab # } TURN IN: .CPP file and screen output.