Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.

#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 << " "; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Machine Performance Modeling Methodologies And Evaluation Strategies Lncs 257

Authors: Francesca Cesarini ,Silvio Salza

1st Edition

3540179429, 978-3540179429

More Books

Students also viewed these Databases questions