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

CAN ANYONE HELP ME SOLVE THIS QUESTION? THANK YOU! 2 Recall the sizeof ¯ functio

ID: 3587275 • Letter: C

Question

CAN ANYONE HELP ME SOLVE THIS QUESTION?

THANK YOU!

2 Recall the sizeof ¯ function returns the size of a variable (i.e., the number of bytes used by a variable). Write a C++ program that declares three variables and three pointers that pointing to them (as below), and add cout statements in order to find out the size, and address of the variables. Run your program and write down your answers in the blank. N ote when you cout a pointer variable or value, it will be shown in hexadecimal, as indicated by the 0X prefix of the number. You can cast the pointer to long if you want the address to be displayed in decimal.

int a=10; // the size of a is: _________

double d=3.1415; // the size of d is: _________

char c=’A’; // the size of c is: __________

int * p = NULL; // the size of p is: _________. // the value of p is: ______________

double * q=NULL; // the size of q is: __________ // the value of q is: _____________

char * t=NULL; // the size of t is: ________, the value of t is: ___________

p = &a; // the value of p is __________________ q = &d; // the value of q is ________________ t = &c; // the value of c is ________________ cout << *p << endl; // This displays ______. When accessing *p, the following ___ bytes are // accessed: ________________________________ (give a range of memory) cout << *q << endl; // This displays ______. When accessing *q, the following ___ bytes are // accessed: ________________________________ (give a range of memory) cout << *t << endl; // This displays ______. When accessing *t, the following ___ bytes are // accessed: ________________________________ (give a range of memory) cout << "p+1=" << p+1 << endl; // How much is p differs from p+1? ______ cout << "q+1=" << q+1 << endl; // How much is q differs from q+1? ______ 2 cout << "t+1=" << t+1 << endl; // How much is t differs from t+1? ______ // We observe that all pointer variables are of the same size, but we need to differentiate // a pointer to an int against a pointer to a double, because:

Explanation / Answer

Blanks are filled with correct answers

Memory addresses change at each runtime so the address will vary everytime you execute the program but the other things remain the same

prog.cpp

#include<iostream>
using namespace std;

int main(){
  
int a=10; // the size of a is: ___4______
double d=3.1415; // the size of d is: ___8______
char c='A'; // the size of c is: _____1_____
int * p = NULL; // the size of p is: ____8_____.
// the value of p is: _____0_________
double * q=NULL; // the size of q is: _____8_____
cout<<"dsa "<<q<<endl;// the value of q is: _____0________
char * t=NULL; // the size of t is: ___8_____, the value of t is: _____0______
p = &a; // the value of p is ____140721291415828______________
cout<<"asdf "<<(long)p<<endl;
q = &d; // the value of q is ____140721291415816____________
cout<<"asdf "<<(long)q<<endl;
t = &c; // the value of c is ____140721291415815____________
cout<<"asdf "<<(long)t<<endl;
cout<<sizeof(a)<<endl;
cout<<sizeof(d)<<endl;
cout<<sizeof(c)<<endl;
cout<<sizeof(p)<<endl;
cout<<sizeof(q)<<endl;
cout<<sizeof(t)<<endl;

cout << *p << endl; // This displays __10____. When accessing *p, the following _8__ bytes are // accessed: ____140721291415828________----_______140721291415836_____________ (give a range of memory)
cout << *q << endl; // This displays __3.14____. When accessing *q, the following _8__ bytes are // accessed: __140721291415816______--________140721291415824________________ (give a range of memory)
cout << *t << endl; // This displays __A____. When accessing *t, the following _8__ bytes are // accessed: ____140721291415815____--_______140721291415823_________________ (give a range of memory)

cout << "p+1=" << (long)(p+1) << endl; // How much is p differs from p+1? __4____
cout << "q+1=" << (long)q+1 << endl; // How much is q differs from q+1? ___1___  
cout << "t+1=" << (long)t+1 << endl; // How much is t differs from t+1? ___1___

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote