Question
Consider the attached C++ program, hw2Mem.c (this is not necessarily good code, as its main purpose is educational). Run the program, commenting out the lines
Consider the attached C++ program, hw2Mem.c (this is not necessarily good code, as its main purpose is educational). Run the program, commenting out the lines with blanks (you may need to modify the header for your system). Make sure you understand why you get the outputs your program produces before proceeding that is after all the main point of this homework.
Based on the programs output, determine how many bits your system uses to store short, regular, and long integers.
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.
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[0]) 0xABCDABB4 8 (foo.z) 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.
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).
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.
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.
Fill in the third blank in the program to print 847.
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.
c This may not be posted or distributed anywhere either in part or whole
c ILLEGAL TO POST
Submission
Submit the following:
Your program, with appropriate modifications
A printout of the program output
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started