Question

Question 1: Pointers You are given the following C code and memory diagram. The “contents” column...

Question 1: Pointers

  1. You are given the following C code and memory diagram. The “contents” column of the memory diagram shows the value of the variable. Update the “contents” column of the memory diagram for each assignment in main().

long *pt;
long data;   
long buffer[4];
void main(void){
  pt = &buffer[1];
  *pt = 1234;
  data = *pt;
}


address      contents   variable
0x20000000 0x00000000    pt
0x20000004 0x00000000    data
0x20000008 0x00000000    buffer[0]
0x2000000C 0x00000000    buffer[1]
0x20000010 0x00000000    buffer[2]
0x20000014 0x00000000    buffer[3]

  1. Consider the following code.

The function GetFifo returns two parameters. The return parameter is a boolean specifying whether or not the request was successful, and the actual data removed from the queue is returned via the call-by-reference parameter. The calling program InChar passes the address of its local variable data. Normally GetFifo does not have the scope to access local variables of InChar, but in this case InChar explicitly granted that right by passing a pointer to GetFifo.

#define FifoSize 10    /* Number of 8-bit data in FIFO */
unsigned char PutI;    /* Index of where to put next */
unsigned char GetI;    /* Index of where to get next */
unsigned char Size;    /* Number currently in the FIFO */
char Fifo[FifoSize];   /* FIFO data */

int GetFifo (char *datapt) {
  if(Size == 0 )
    return(0);     /* empty if Size=0 */
  else{
    *datapt=Fifo[GetI++];

    Size--;
    if (GetI == FifoSize) GetI = 0;
    return(-1);
}
}

char InChar(void){

char data=0;
  if(GetFifo(&data)){};
  return (data);

}

Questions:

  1. Sketch a simple diagram that illustrates the different scope of variables in the code. Use any notation you want to show global versus local variables and pointer variables. You are not given information about actual memory addresses, so do not try to set up a memory diagram.

  1. What would happen if GetFifo is called instead with the following parameter GetFifo(data)? Will rate thanks!!
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer 1: The solution is given below in the form of a table

Line Explanation address contents variable
0x20000000 0x00000000 pt
0x20000004 0x00000000 data
0x20000008 0x00000000 buffer[0]
0x2000000C 0x00000000 buffer[1]
0x20000010 0x00000000 buffer[2]
0x20000014 0x00000000 buffer[3]
pt = &buffer[1]; The address of buffer[1] is stored as the content of pt 0x20000000 0x2000000C pt
*pt = 1234; 1234 is stored at the address stored as the content of pt 0x2000000C

1234 (base 10) =

0x000004D2

buffer[1]
data = *pt; Value at address stored as contents of pt is copied as content of data 0x20000004 0x000004D2 data

Answer 2 (i):

The scope of the different variables is given in the table given below alongwith explanatory comments where needed.

Variable name Scope/Type Comments
Putl global
Getl global
Size global
Fifo global
datapt local (pointer variable)

The scope of datapt is local, i.e. limited to the GetFifo() function.

But since it is a pointer variable, depending on the value passed as arguments, it may be used to modify the local variables of another function.

data local The scope is local, i.e. limited to InChar() function. But its address is passed as an argument to allow another function to modify its contents.

Answer 2(ii):

Calling GetFifo(data) implies passing a char as parameter while the prototype requires a pointer to char (char *). Many C-compilers will result in a compilation error as they do not support implicit cast from integral values to pointers.

For the compilers that allow this implicit type casting, the behaviour will be as follows. The value of data will be implicitly typecasted to char*. Therefore the GetFifo() function will start with dataptr=0. i.e. dataptr stores the address 0.

If Size is zero, it will return 0.

If Size != 0, the following line will result in a segmentation fault and cause the program to crash, since an unallocated memory address is accessed:

    *datapt=Fifo[GetI++];

This is because application of the dereferencing operator to datapt results in accessing the memory address 0x0 which was never allocated in the first place.

Add a comment
Know the answer?
Add Answer to:
Question 1: Pointers You are given the following C code and memory diagram. The “contents” column...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (C++ concept on local variable) On page 14 of the “Pointers and Memory” handout there is...

    (C++ concept on local variable) On page 14 of the “Pointers and Memory” handout there is a reference to the “Amperand (&)” Bug in the TAB() function. Draw a trace of the local variables of the functions Victim() and TAB() on the run-time stack as these functions are executed. Your diagram should be similar to the one provided on page 13. Your diagram should depict exactly three instances in program execution - T1: Right after the first statement of Victim(),...

  • #include <stdio.h> int main(int argc, char *argv[]) {      char a, *pc, c[9];      int i, *pk, k[9];      a='...

    #include <stdio.h> int main(int argc, char *argv[]) {      char a, *pc, c[9];      int i, *pk, k[9];      a='z';      pc=&(c[8]);      pk=&(k[0]);      for (i=0; i<9; i++)     {           *pc=a-(char)i;           pc--;           *pk=(int)a-i;           pk++;           }                     return 0; }//end of main Answer the below questions with the above code Write out the memory map for the above C code in the following table. For array variables, list the address range for the entire array. Assume the memory address starts from 100, that is, the address for a...

  • For each code fragment below, show a memory diagram that traces the execution of the calling...

    For each code fragment below, show a memory diagram that traces the execution of the calling method. d) //calling method intp data = {1, 3, 5); method(datal2 public static int methodliot size) nt source new intfsize: for (int i-0; issourclength:i) sourceli)-size; return source; Heap Identifier Address Contents Main Stack Frame 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 Identifier Address Contents 101 102 103 Method Stack Frame Identifier Address Contents 200 201 202

  • Homework Part 1 Write a C program that declares and initializes a double, an int, and a char. You...

    Homework Part 1 Write a C program that declares and initializes a double, an int, and a char. You can initialize the variables to any legal value of your choosing. Next, declare and initialize a pointer variable to each of the variables Using printf), output the following: The address of each of the first three variables. Use the "Ox%x" format specifier to print the addresses in hexadecimal notation The value of each variable. They should equal the value you gave...

  • (30 points; 6 points each part) For each code fragment below, show a memory diagram that traces t...

    (30 points; 6 points each part) For each code fragment below, show a memory diagram that traces the execution of the calling method. 2. a) //main method nt data-14,7,3; method(data, 0); public static void method(int[l array, int value) for (int index array,length/2; index

  • Consider the following snippet of code. (Assume that input strings are at most 255 characters long.)...

    Consider the following snippet of code. (Assume that input strings are at most 255 characters long.) char* to_upper_case(char* original) { char capstr[255]; unsigned int i; for (i = 0; original[i] != '\0'; ++i) { if (original[i] >= 'a' && original[i] <= 'z') { capstr[i] = original[i] - (char)'a' + (char)'A'; } else { capstr[i] = original[i]; } } capstr[i] = '\0'; return capstr; } void main() { printf("%s", to_upper_case("the c programming language")); } a) What will be the output? Trace...

  • C PROGRAM The following is code prints the current activation record number, the memory address of...

    C PROGRAM The following is code prints the current activation record number, the memory address of the current array, followed by the estimated size of the current activation record as a distance between the current array address and the array address from the previous activation record. I need it to run until a segmentation fault occurs and also it must print the estimated size of the runtime stack as a product of the size of current activation record and the...

  • Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory...

    Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int * ) named ptr_1. Use ptr_1 to assign the number 7 to that dynamically allocated integer, and in another line use printf to output the contents of that dynamically allocated integer variable. Write the code to dynamically allocate an integer array of length 5 using calloc or malloc and have it pointed...

  • The following C code keeps returning a segmentation fault! Please debug so that it compiles. Also...

    The following C code keeps returning a segmentation fault! Please debug so that it compiles. Also please explain why the seg fault is happening. Thank you #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> // @Name loadMusicFile // @Brief Load the music database // 'size' is the size of the database. char** loadMusicFile(const char* fileName, int size){ FILE *myFile = fopen(fileName,"r"); // Allocate memory for each character-string pointer char** database = malloc(sizeof(char*)*size); unsigned int song=0; for(song =0; song < size;...

  • Learn the example C program in Folio consisting of three files (a.c, a.h, main.c) and complete this exercise. You need t...

    Learn the example C program in Folio consisting of three files (a.c, a.h, main.c) and complete this exercise. You need to understand function, pointer and the selection sort algorithm. Write a C program that consists of three files mysort.h, mysort.c and myMain.c. Below is the mysort.h prototype #include <stdlib.h> #include <stdio.h> void generateNums(int *myarr, int len); void sortNums(int *myarr, int len); mysort.h must contain only the function declarations (prototypes) listed above generateNums function should generate len random integers in the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT