. aueshons 1-10 C13-277 Data structures and Algerithns Revi ew to Pointers and D
ID: 3594842 • Letter: #
Question
. aueshons 1-10 C13-277 Data structures and Algerithns Revi ew to Pointers and Dynamie Hemory Hanagement 1 What does a pointer variable store ? 2 Write the statements required toi declare a variable ot type double with a value of 2.71828, declare a pointer variable that can hold the addreas of a double, aasign the address of the double variable to the pointer variable, and print out the value of the double variable theu the use of the pointer variable. .What are the two legal operations that can be applied to pointer variables ? a. When is statio memory (atack memory) allocated/deallocated ? b. When is dynamie memory (heap memory) allocated/deallocated ? 5. The name of the dynamic memory management operator for a. dynamic memory allooation. b. dynamie memory deallocation 6. | what does the dynamie memory allocation operator return it insufficient memozy is available for the request ? 7. Write the statements required to: declare a pointer variable that can hold the address of a double, dynamically allocate memory hold a double and assign the address of this area to the pointer variable, store the value: 3.14159 in the to allocated memory area, print out the stored value, deallocate the memo ry 8· [write the statements required to: a variable of type float with a value of 3.14159, a pointer declare variable that can hold the address of a of the float variable to the pointer variable, and print out the value of the float variable thru the use of the pointer variable. float, assign the address 9· Given the decl a rat i ons : typedef int PT INT a, b, C int PT_INT x, y, z INTx, y. z Determine which of the following are VALID (are legal AND would produce a meaningful result or INVALID code s assume that any pointers already hold an address. Consider earh an INDEPENDENT problem segments. Do NOT
Explanation / Answer
2. Write the statement required to: declare a variable of type double with value of 2.71828,
declare a pointer variable that can hold the address of a double, assign the address of
the double variable to the pointer variable, and print out the value of the double
variable through the use of the pointer variable.
double value = 2.71828; //declare a variable of type double with value of 2.71828
double *ptr; //declare a pointer variable that can hold the address of a double
ptr = &value; //assign the address of the double variable to the pointer variable
cout << *ptr << endl; //print out the value of the double variable through the use of the pointer variable
7. Write the statements required to: declare a pointer variable that can hold the address
of a double, dynamically allocate memory to hold a double and assign the address of
this area to the pointer variable, store the value: 3.14159 in the allocated memory
area, print out the stored value, deallocate the memory.
double* ptr; //declare a pointer variable that can hold the address of a double
ptr = new double; //dynamically allocate memory to hold a double and assign the address of this area to the pointer variable
*ptr = 3.14159; //store the value: 3.14159 in the allocated memory area
cout << *ptr << endl; //print out the stored value
delete ptr; //deallocate the memory
8. Write the statements required to: declare a variable of type float with a value of
3.14159, a pointer variable that can hold the address of a float, assign the address
of the float variable to the pointer variable, and print out the value of the float
variable through the use of the pointer variable.
float value = 3.14159; //declare a variable of type float with a value of 3.14159
float* ptr; //a pointer variable that can hold the address of a float
ptr = &value; //assign the address of the float variable to the pointer variable
cout << *ptr << endl; //print out the value of the float variable through the use of the pointer variable
9. Given the declarations:
typedef int* PT_INT;
int a, b, c;
PT_INT x, y, z;
Determine which of the following are VALID (are legal AND would produce a meaningful
result) or INVALID code segments. Do NOT assume that any pointers already hold an address.
Consider each as an INDEPENDENT problem.
a. a = 20; //a is an integer variable and is assigned an integer.
b = 10; //b is an integer variable and is assigned an integer.
z = &a; //z is a pointer variable and is assigned an address of integer.
x = z; //x is a pointer variable and is assigned an address.
c = b + *x; //c is an integer, and the expression contains integer, and integer(accessed through pointer).
//2 integers added, and the result is stored in integer.
VALID
b. x = new int; //x is a pointer variable and is assigned an address of an integer.
*x = 20; //An integer (accessed through pointer) is assigned a value.
VALID
c. x = new int; //x is a pointer variable and is assigned an address of an integer.
a = x; //a is an integer and is being trying to assign an address.
INVALID.
d. y = z; //y is a pointer, and is being assigned another address(in z).
//Remember this may contain garbage values, and may generate a warning.
But still it is VALID.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.