Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. Write a program (addressShort1.cpp) that declares two short integer variables si1 and si2, sets them to 1 and 2 respectively, and displays their names,

1. Write a program (addressShort1.cpp) that declares two short integer variables si1 and si2, sets them to 1 and 2 respectively, and displays their names, values, and addresses using the & operator. It should also output the size (in bytes) of a short int variable using the sizeof() function. Usage of sizeof(): sizeof(type) or sizeof(expression). It returns the number of bytes of a data type. For example: cout << sizeof(double) << endl; // will display the size of a double in bytes Remember, as shown in class, that you can display the address of a variable using: cout << &si1 << endl; The address will be displayed in hexadecimal. it will have the prefix 0x followed by 8 (32 bits address) or 16 (64 bits address) hexadecimal digits depending on the computer you are running the program. Answer the following questions: a) What is the address assigned to si1? To si2? What is the difference (in Bytes) between them? Read the hexadecimal notes in hexadecimal.pdf to figure out how to subtract or add in hexadecimal. Do calculations by hand first, then check with a calculator. b) What is the size of a short int? c) Draw the memory locations of the two variables si1 and si2 in relation to each other and write their memory addresses next to them.

2. Make a copy of the previous program and call it addressShort2.cpp. To this program add a declaration of a char variable (vChar) and initialize it with 'C'. Output its memory address using the & operator and output its size in bytes with sizeof(). If the address does not display correctly (in hexadecimal) use (void *)&vChar in the cout statement: cout << (void *)(&varChar); Answer the following questions: a) What is the address of vChar? What is the difference (in Bytes) between the address of vChar and the address of si1 and that of si2? b) Draw the memory locations of vChar, si1 and si2 and their memory addresses. Explain the relations between the memory locations of si1, si2 and vChar. (See note at end of this document to see what to draw).

3. Make a copy of addressShort2.cpp and call it addressShort3.cpp. To this program add statements that declare a pointer to a short integer variable (ptrInt) and a pointer to a character (ptrChar). Initialize ptrInt to point to si1 and ptrChar to point to vChar. Add code to do the following and answer the indicated questions: (a) Output the values of ptrInt and ptrChar. (b) Output the memory addresses of ptrInt and ptrChar. Use: cout << (void *)ptrChar; // for printing the address of ptrChar (c) Output the values pointed by ptrInt and ptrChar (use the dereferencing operator (*)). (d) Output the size of ptrInt and the size of ptrChar using the sizeof() operator. Compare them with the size of values they point to. Explain the differences. (e) Draw the memory locations of ptrInt, ptrChar, vChar, si1 and si2 and their memory addresses. Explain the relations between them.

4. Make a copy of addressShort3.cpp and call it addressShort4.cpp. To this program add a declaration of a pointer to a short int (ptrInt2). Allocate dynamic memory for one short integer value using the new operator. Add code to do the following and answer the indicated questions: Initialize the allocated memory with value -5. Example allocating dynamic memory for a short int: short *ptrInt2 = new short; (a) Output the value of ptrInt2. Is this value related to any other memory address you found before? Explain what you observe. (b) Output the value pointed by ptrInt2. (c) Change the value pointed by ptrInt2 to be -10. Output the new value pointed by ptrInt2. (d) Change ptrInt2 to be equal to ptrInt. Output the value of ptrInt2 and the value pointed by ptrInt2. (e) What happened to -10. Can you still find the memory location where -10 is stored? If not, explain why? (f) Draw the memory locations of all variables in your program and their memory locations. Note: When asked to draw the locations of variables, use the following format: ************* ************* * Si1 (n bytes) * ##### *Si2 (m bytes) * ##### ************* ************* 0x05 ^ 0x100 | where n and m represent the length in bytes of the variables and the hex numbers indicate their start address. Any intervening memory addresses are indicated by ####. If there are no intervening memory addresses, then there are no ####. Variables with higher addresses that cannot fit across page can be shown on subsequent lines using the above format.

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

More Books

Students also viewed these Databases questions

Question

Why does the book devote so much space to punitive damages?

Answered: 1 week ago