Question: #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;

#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]=500+i; for (i=0; i<10; i++) foo.y[i]=700+i; foo.var1 = 640; 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 << " "; // EXPLAIN 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 + _______) = 847; cout << bar[7].var1 << " "; }
1. Based on the programs output, determine how many bits your system uses to store short, regular, and long integers.
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 conclusion.
3. Based on your output, make a diagram indicating the addresses in memory where each of the programs variables are stored and their values at POINT 1. A sample diagram (not for the given program) may look like:
0xABCDABF8 | 123 (x) |
0xABCDABF4 | 30 (foo.y[15]) |
| ....: |
| 2k (foo.y[k]) |
| ..... |
0xABCDABB8 | 0 (foo.y[k]) |
0xABCDABB4 | 8 (foo.z) | <- p (points to foo)
0xABCDABB8 | 9 (w) |
p (points to foo)
Of course, you dont need to specify every single element for large arrays, as long as you make it clear you understand whats going on. It is general practice to indicate the first and last elements, along with an arbitrary index k in the middle.
4. Consider the output produced on the line labeled EXPLAIN. Explain why you get this output (a brief explanation suffices as long as you show an understanding).
5. After understanding the program, can you make any conclusions about how C/C++ interpret addition on integers and pointers? Are they the same? Explain. Obviously, your explanation should point out the outputs that support your answer.
6. The program contains three blanks. Fill the first two with appropriate integer constants to print 704 for both new outputs. Depending on your platform, it is possible (though unlikely) that your original program will produce a strange error message (e.g., bus error, segmentation error) before you get to this point. If so, you should use the lab computers.
7. Fill in the third blank in the program to print 847.
8. Does your compiler allocate local scalar variables growing up or down in memory (i.e., higher or lower addresses)? Explain how you know this from your program output.
Submission
Submit the following:
1. Your program, with appropriate modifications
2. A printout of the program output
3. Answers to each of the parts, either as clearly labeled (by problem number) comments at the end of your program file, or on a separate page.
Your submission must be stapled.
Explanatory material
The program has a few constructs that you may have (but shouldnt have) forgotten from 135:
The & operator returns the address of a variable. Dont forget that arrays are pointers in C/C++.
Preceding an expression with a type surrounded by parentheses (eg, (int)) is called casting. This means that the expression is converted to the specified type. For example, x=(float)2; will convert the integer 2 to a float before storing it in x. Although I use casting in this example to illustrate some points, it is generally considered a bad though sometimes unavoidable practice.
The struct construct is used to group a number of variables together in C. In the above code, foo is the name of a variable containing a structure with fields x, var1, and y, and the type of foo is foo t. Some C++ programmers may avoid using structs since classes can be used for the same purpose; however, they are still used.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!