Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Review: Introduction to Pointers and Dynamia Memory Management 1. What does a po

ID: 3596145 • Letter: R

Question

Review: Introduction to Pointers and Dynamia Memory Management 1. What does a pointer variable store 2 2. Write the statements required to: declare a variable ot type double with a value of 2.71828, declare a pointer variable that can hold the address of a double, aasign the address of the double variable to the pointer variable, and print out the value of the double variable thru the use of the pointer variable. 3. What are the two legal operations that can be applied to pointer variables ? 4. a. When is statio memory (stack memory) allocated/deallocated? b. When is dynamie memory (heap memory) allocated/deallocated ? S. The name of the dynamic memory management operator for a. dynamic memory allocation b. dynamie memory deallocation 6. what does the dynamie memory allocation operator return if insufficient memory is available for the request? the statements required to: declare a pointer variable that Write 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 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 thru the use of the pointer variable 9. Given the declarations: typedef int' PT INT int a, b, c PT INT Y Determine which of the following are VAlID t are legal AND would assume that any pointers already hold an address. Consider each as produce a meaningful result) r INVALID code segments. Do NoT an INDEPENDENT problem

Explanation / Answer

Answer 1 :

                A pointer variable stores the address which is pointing to the value of any variable.

Ex:         int a = 100;

int *ptr

ptr = &a;

                Above “ptr” is pointer variable which holds address of variable “a” which holds value 100.

ptr ----------->(points to ) 2000

                a =100

                2000                       2001                       2002

Answer 2 :

                double a = 2.71828 ; // declaration of variable of type double with value

                double *ptr; //declaration of pointer variable that can hold address of double

                ptr = &a;

                printf(“Value = %f”, *ptr) ; // printing double variable through pointer

Answer 3 :

                Two legal operations that can be performed on pointer variables are :

1)Increment :   Incrementing ptr variable moves ptr one variable ahead and replaces the previous address with new address.
Ex: if ptr points to 2000 (address) , then ptr++ points to 2001 (address)

2)Decrement : Decrementing ptr variable moves back ptr one address memory backward and replaces address with new address .
Ex : if ptr points to 2000 (address), then ptr—points to 1999 (address)

Answer 4 :

a)            Static memory is allocated when the program is compiled, before the program is executed and remains allocated “lifetime” of the program. Deallocates at the end of the programs (dead or killed).

b)            Dynamic memory (heap memory) is allocated at runtime by dynamically allocating heap memories to the variables at the declaration time using malloc() or calloc() function or using new operator . And memory can be deallocated by using free() function or delete operator.

Answer 5 :

a)The name of the dynamic memory allocation operator : new

b)The name of the dynamic memory deallocation operator : delete

Answer 6 :

If insufficient memory is available for requrest then dynamic memory allocation operator (new)
indicates failure by throwing an exception of type std::bad_alloc and returns a pointer.

Answer 7:

double *ptr; //pointer variable that can hold double variable

ptr= new double; //dynamic memory allocation to ptr

*ptr=3.14159 // storing value to that new memory area

printf(“value : ”, *ptr) //printing ptr variable value

delete ptr; // deallocate the variable pointer by ptr

  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote