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

Consider the following C++ program: #include using namespace std; int main() { s

ID: 3752659 • Letter: C

Question

Consider the following C++ program:

#include
using namespace std;
int main() {
                                                                               
struct foo_t {
    int x[100];
    int var1;
    int y[10];
    } foo;
int var2;
long i;
int *p, *q;
short int *s;
long int *l;
struct foo_t bar[50];
                                                                               
for (i=0; i<100; i++) foo.x[i]=100+i;
for (i=0; i<10; i++) foo.y[i]=200+i;
foo.var1 = 250;
                                                                               
cout << sizeof(*s) << " ";
cout << sizeof(*p) << " ";
cout << sizeof(*l) << " ";
q = (int *) &foo;    cout << q << " ";
p=&(foo.x[5]);       cout << *p << " ";
// POINT 1
q = (int *) &var2;   cout << q << " ";
q = p+16;            cout << *q << " ";
i = ((long) p) + 16;
q = (int *) i;       cout << *q << " ";
  s = (short *) i;     cout << *s << " ";
l = (long *) i;      cout << *l << " ";
q = p+95;            cout << *q << " ";
q = p+98;            cout << *q << " ";
i = ((long) p) + 17;
q = (int *) i;       cout << *q << " ";
q = p + _______;     cout << *q << " ";
q = (int *) (((long) p) + ______); cout << *q << " ";
p = (int *) &bar;
*(p + _______) = 500; cout << bar[6].var1 << " ";
}

Part 0, Need to use output for the rest of the parts. But no need for answer of Part 0.

Part 0 Run the above program (commenting out the lines with blanks) on the computer/compiler you normally use. You may need to modify the header slightly for your system. If the program does not work on your computer, you can either modify it to work, use a compiler flag to generate working code, or run it on the G-lab computers where it works using g++. Note that accounts for you have been created on the CS department Linux Lab (1000G), and you can access it the same you did in CSCI 127/135 What output do you get? Make sure to mention which computer and compiler you are using. You will need to understand your output before you proceed to the rest of this homework. Part 1 Based on the program's output, determine how many bits your system uses to store short, regular, and long integers. The answer may differ depending on your compiler and processor. Part 2 You wish to determine how many bits your system uses to store pointers. Modify the program to determine this (label your modification "MOD" in comments), and state your conclusions

Explanation / Answer

As we are allowed to answer only 4 sub parts, please post a new question.

part1:

short int takes 2

int takes 4

long int takes 8.

Part2:

cout << "Size of int pointer: " << sizeof(int*) << endl;

Added this code in the main. It returns as below.

Size of int pointer: 8 So it takes 8 bytes.

part5:

Yes same. When you add pointer values(*p + *q), dereferences the pointer to get the value pointed by the address then adds them. When you add an integer to a polinter(*p + 10), it adds that number to the address which may cause segmentation error if you try to add larger value to a pointer. But you cannot add two pointers as ptr1 + ptr2.

part7:

In my compiler, address of bar stored at 0th memory. So adding 500 to p, gives me 500.

As a hint, by adding 99, you can get 203 for your first dash.

In case of any doubts, please comment.

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