Question: NOTE:Unix Commands File: addressArithmetic1.c #include #include char * pressKey(char *msg) { char *dummy=readline(msg); return dummy; } int main(int argc, char * argv[],char * envp[]) {
NOTE:Unix Commands

File: addressArithmetic1.c #include#include char * pressKey(char *msg) { char *dummy=readline(msg); return dummy; } int main(int argc, char * argv[],char * envp[]) { int numbers[]={0,1,2,3,4,5}; fprintf(stdout,"The size of the array numbers in bytes is is: %lu ",sizeof numbers); fprintf(stdout,"The size of array numbers[0] in bytes is is: %lu ",sizeof numbers[0]); fprintf(stdout,"The number of elements in numbers is: %lu ",sizeof numbers/sizeof numbers[0]); pressKey("Press return to continue"); fprintf(stdout," "); fprintf(stdout,"All 4 syntax notations give the same result "); fprintf(stdout,"numbers[3]: %d ", numbers[3]); fprintf(stdout,"*(numbers+3)%d ", *(numbers+3)); fprintf(stdout,"*(numbers+3)%d ", *(3+numbers)); fprintf(stdout,"numbers[3]: %d ", 3[numbers]); 3[numbers]=3000; fprintf(stdout,"The new value of numbers[3]: %d ", numbers[3]); //Note: In the following 2 loops we go beyond the end of the array // There is no guarantee as to what is stored in that memory // and it could result in a segmentation fault! pressKey("Press return to see the addresses and contents of numbers: numbers[i]"); fprintf(stdout," The base address of numbers is: %p ",numbers); for(int i=0;i Part III: Address Arithmetic /8 Testing out of bound values Modify the example addressArithmetic3.c by creating an array of 5 values before numbers and a second array after numbers. Fill each with distinct values. Use #include to include the declaration of your datatype from Part I. (1 mark each except for g which is 2 marks) a. Display values from numbers using subscripts from 6 to 9. From which array (if either) do these values come from? b. Display values from numbers using subscripts from -1 to -5. From which array (if either) do these values come from? c. Modify your listing so that instead of using numbers[i] you use the pointer dereferencing notation * (numbers+i) to refer to elements of the array. Is there any change in output? d. Add the following declarations to your code after the 3 arrays: char *newstring1=(char *)alloca(100); char newstring2[100]; struct yourDataType *item1=(struct yourDataType *) calloc(sizeof (struct yourDataType),5); Print out the value of the pointers newstringi and newstring2 where in memory are they in relation to your arrays? Are they in the stack or the heap? e. Add values from 3 to +3 to both newstringi and newstring2 and print out the values. Explain the results in terms of address arithmetic. f. Print out the values item1 and &item1. Where are they in memory in relationship to your arrays? Explain the results in terms of address arithmetic? g. Set up your program so that it uses both the function and the script mtrace to trace memory allocation. Describe what you did. What does the mtrace shell command tell you about the allocation and freeing of memory in the program? (2 marks) Hand in your completed program (1 listing only) and your writeup Part III: Address Arithmetic /8 Testing out of bound values Modify the example addressArithmetic3.c by creating an array of 5 values before numbers and a second array after numbers. Fill each with distinct values. Use #include to include the declaration of your datatype from Part I. (1 mark each except for g which is 2 marks) a. Display values from numbers using subscripts from 6 to 9. From which array (if either) do these values come from? b. Display values from numbers using subscripts from -1 to -5. From which array (if either) do these values come from? c. Modify your listing so that instead of using numbers[i] you use the pointer dereferencing notation * (numbers+i) to refer to elements of the array. Is there any change in output? d. Add the following declarations to your code after the 3 arrays: char *newstring1=(char *)alloca(100); char newstring2[100]; struct yourDataType *item1=(struct yourDataType *) calloc(sizeof (struct yourDataType),5); Print out the value of the pointers newstringi and newstring2 where in memory are they in relation to your arrays? Are they in the stack or the heap? e. Add values from 3 to +3 to both newstringi and newstring2 and print out the values. Explain the results in terms of address arithmetic. f. Print out the values item1 and &item1. Where are they in memory in relationship to your arrays? Explain the results in terms of address arithmetic? g. Set up your program so that it uses both the function and the script mtrace to trace memory allocation. Describe what you did. What does the mtrace shell command tell you about the allocation and freeing of memory in the program? (2 marks) Hand in your completed program (1 listing only) and your writeup
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
