Question

int c = 75; int d = 100; int& r1 = c; int& r2 = d;...

int c = 75;

int d = 100;

int& r1 = c;

int& r2 = d;

c++;

r2++;

Use the code above. Suppose the variable c lives at memory address 512, and d lives at the memory address 1024. The size of an integer is 4 bytes. After execution of the above code, draw labelled boxes for each of the 4 variables c, d, r1 and r2, and show their integer values. For r1 and r2, draw an additional arrow to where it points. Include addresses of boxes where applicable.

int w = 1;

int x = 2;

int* f = &w;

int* g = &x;

int y = 3;

int z = 4;

int& h = y;

int& i = z;

f = g;

h = i;

Draw labelled boxes for each of the 8 variables, and after execution of the above code show the contents of each box. Assume w lives at memory address 128, x lives at 256, y lives at 512, and z lives at 1024. In addition, draw arrows from f, g, h and i to where they point. Include addresses of boxes where applicable.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

A reference variable is an alias, that is, another name for an already existing variable. A reference, like a pointer, is also implemented by storing the address of an object.A reference can be thought of as a constant pointer (not to be confused with a pointer to a constant value!) with automatic indirection, i.e the compiler will apply the * operator for you. A reference shares the same memory address (with the original variable) but also takes up some space on the stack.

#include <iostream>
using namespace std;
int main()
{
int c = 75;
int d = 100;
int& r1 = c;
int& r2 = d;
c++;
r2++;
cout<<c<<"\n";
cout<<r2;
   return 0;
}

OUTPUT:

76

101

#include <iostream>
using namespace std;
int main()
{
int w = 1;
int x = 2;
int* f = &w;
int* g = &x;
int y = 3;
int z = 4;
int& h = y;
int& i = z;
f = g;
h = i;
cout<<f<<"\n";
cout<<g<<"\n";
cout<<h<<"\n";
cout<<i<<"\n";
return 0;
}

OUTPUT: 1

0x7fffe1f28f78
0x7fffe1f28f78
4
4

OUTPUT: 2

0x7ffeffa9e1c8
0x7ffeffa9e1c8
4
4

Kindly like my answer.

Add a comment
Know the answer?
Add Answer to:
int c = 75; int d = 100; int& r1 = c; int& r2 = d;...
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
  • 2. a)Write the ARM ALP conditional code snippet for the following statements written in C-language. Assume R1 to Rn as06 variables Let R1, R2, R3 contain the starting addresses of arrays X, Y and Z r...

    2. a)Write the ARM ALP conditional code snippet for the following statements written in C-language. Assume R1 to Rn as06 variables Let R1, R2, R3 contain the starting addresses of arrays X, Y and Z respectively Use Register R4 for variable i. Display appropriate messages. While (i+10) else Z[i] XiYi; b)i Write a program to display a message "This is an examination Question" on the screen using 06 a function sub program Note the following Address of the string to...

  • #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...

  • Suppose R is a one-to-one relationship from R1 = { a, b, c, d} to R2...

    Suppose R is a one-to-one relationship from R1 = { a, b, c, d} to R2 = {e, f, g, h}. Of the following sets, which one is NOT a valid instance of R? (2 points) Select one: о 1, the empty set O {(c, g)} {(a, e),(6,1),(6,9)} {(a,h), (0.9), (C, f), (d, e)}. All of the above. None of the above.

  • Suppose R is a one-to-one relationship from R1 = { a, b, c, d; to R2...

    Suppose R is a one-to-one relationship from R1 = { a, b, c, d; to R2 = {e, f, g, h} of the following sets, which one is NOT a valid instance of R? (2 points) Select one: f), the empty set. {(cg)} {(a, e) (b,f).(b,9)} {(ah), (b, g),c, f), (d, e)} All of the above None of the above.

  • 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...

  • Consider the topology below with 9 hosts (A, B, C, D, E, F, G, H, 1),...

    Consider the topology below with 9 hosts (A, B, C, D, E, F, G, H, 1), 4 switches (S1, S2, S3, S4) and 3 routers (R1, R2, R3). The numbers near links between routers R1, R2, R3 represent the cost of the respective link. В. С R 3 D E H b) Number the switch interfaces as you see fit (you can write the numbers on the figure), and assign MAC addresses to all of the adapters (you may simplify...

  • Consider the following in C int main() { float x = 3.14, *p = &x; int...

    Consider the following in C int main() { float x = 3.14, *p = &x; int r, a = 2, b[] = {5, 6, 7}; <more code here> r = Foo(x, p, &a, b) <more code here> } int Foo(float x,float y, int *z, int *w) { <Foo's code goes here> } statement in Foo result (assigned value) modify Foo's AF variables modify main's AF variables statement in Foo result (assigned value) modify Foo's AF variables modify main's AF variables...

  • Run the C program below and complete the table showing all the variables. Add printf function...

    Run the C program below and complete the table showing all the variables. Add printf function calls to obtain the addresses and values of all 13 variables. (Count the array (ca) as 3 variable/values and also include argc and argv in the table). The table must show the Address, Name, Datatype, Scope, and Value of each variable on the stack. (Hint: use the sizeof function to double check your addresses.) Explain how different the actual memory allocations are from what...

  • (a) Consider the following C++ function: 1 int g(int n) { 2 if (n == 0)...

    (a) Consider the following C++ function: 1 int g(int n) { 2 if (n == 0) return 0; 3 return (n-1 + g(n-1)); 4} (b) Consider the following C++ function: 1 bool Function (const vector <int >& a) { 2 for (int i = 0; i < a. size ()-1; i ++) { 3 for (int j = i +1; j < a. size (); j ++) { 4 if (a[i] == a[j]) return false; 5 6 } 7 return...

  • Memory Consider a process running the following program #include estdlib.h> #include #define const char* int int...

    Memory Consider a process running the following program #include estdlib.h> #include #define const char* int int <stdio.h> TO PRINT toPrintCPtr "Good luck!" TO PRINT; main for (i -0; i < sizeof (TO PRINT)-1; i++) printf("%c %c\n", toPrintCPt r [1], toupper(toPrintCPt r [i])); return(EXIT SUCCESS); Please tell where the following objects are stored in memory. Your choices are a. ROM BIOS b. kernal Memory (the OS) c. shared library memory (the glibc library) d. .text segment e. .rodata segment f. .data...

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