Question: there are two programs: prob1.c and prob2.c and a shell script run.sh. To compile and run a program (e.g. prob1.c), you would issue the following

there are two programs: prob1.c and prob2.c and a shell script run.sh. To compile and run a program (e.g. prob1.c), you would issue the following commands:

cd lab1Problem3 (You only need this command if you are not already in the lab1Problem3 directory.)

chmod 755 run.sh

./run.sh (to compile the two programs)

questions:

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 in prob1.c (choose one from the options below)?

garbage memory

dangling reference

segmentation fault

bus error

null pointer dereference

6. What is the value (in hex) stored in f1 in prob2.c?

7. What are the byte values stored in f2? Please show each byte of the 18 bytes in f2 in hex.

8. What are the sizes in byte of struct x and struct y? Hint: modify the program to use sizeof operator

prob1.c:

#include

#include

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;

}

prob2.c:

#include

#include

#include

struct x{

int i1;

int i2;

char ch1[8];

char ch2[8];

};

struct y{

long long int f1;

char f2[18];

};

int main(void)

{

struct x * myX;

struct y * myY;

myX = malloc(sizeof(struct x));

myX->i1 = 4096;

myX->i2 = 4096;

strcpy(myX->ch1,"Witawas ");

strcpy(myX->ch2,"Srisaan");

myY = (struct y *) myX;

printf("myY->f1 = %llx ", myY->f1);

printf("myY->f2 = %s ", myY->f2);

}

run.sh:

gcc prob1.c -Wno-return-local-addr -o prob1; gcc prob2.c -Wno-return-local-addr -o prob2;

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!