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

Please answer the questions using the following code. Thanks!! 1. What does the

ID: 3878677 • Letter: P

Question

Please answer the questions using the following code. Thanks!!

1. What does the keyword volatile mean?

2. What type is int * result1?

3. What are the values stored in result1 and *result1 after the third line in main has been executed? Indicate the actual values in your program.

4. Who incidentally change the value of * result1?

5. What is the term used to describe this type of problem exhibited?(choose one from the options below)?

•garbage memory•dangling reference•segmentation fault•bus error•null pointer dereference

#include<stdio.h>

#include<stdlib.h>

int * function1(int);

int * function2(int);

int main(void)

{

volatile int * result1,* result2;

int val = 1000;

result1 = function1(val);

fprintf(stderr,"result1 = %d ", *result1);

result2 = function2(val);

fprintf(stderr,"result2 = %d ", *result2);

fprintf(stderr,"result1 = %d ", *result1);

}

int * function1(int val)

{

int result = val + 1500;

return &result;

}

int * function2(int val)

{

int result = val - 1500;

return &result;

}

Explanation / Answer

Answers:

1. Volatile Keyword: Volatile actually came into existence for the purpose of not caching the values of the variable automatically. It will tell the machine not to cache the value of this variable. So it will take the value of the given volatile variable from the main memory every time it encounters it. This mechanism is used because at any time the value can be modified by the OS or any interrupt. So using volatile will help us accessing the value afresh every time.

Another use for volatile is signal handlers. If you have code like this:

The compiler is allowed to notice the loop body does not touch the quit variable and convert the loop to a while (true) loop. Even if the quit variable is set on the signal handler for SIGINT and SIGTERM; the compiler has no way to know that.

However, if the quit variable is declared volatile, the compiler is forced to load it every time, because it can be modified elsewhere. This is exactly what we want in this situation.

2. What type is int * result1?

result1 is a point to an integer. (*) before a variable name means that is a POINTER.

We have just seen that a variable which stores a reference to another variable is called a pointer. Pointers are said to "point to" the variable whose reference they store.

Using a pointer we can directly access the value stored in the variable which it points to. To do this, we simply have to precede the pointer's identifier with an asterisk (*), which acts as dereference operator and that can be literally translated to "value pointed by". Please see the explanation below with Example:

assigned 1 to variable x by using the dereference operator and a pointer to the variable x.

3. What are the values stored in result1 and *result1?

result1 stores the address of the local variable declared in the function1.

*result1 stores the actual value returned from address variable result i.e., 2500

4. Who incidentally changed the value of *result1?

The address of the variable "result" declared in the function1 has changed the result1 value.

5. What is the term used to describe this type of problem exhibited?

Null Pointer deference