Questions: Using the page below, answer these questions.
1. Look at the following examples and write your answer in the column next to the example.
char * name1 = new char[ 6 ];strcpy( name1, “Henry” );char * name2 = name1;name1[0] = ‘D’; | char s1[] = “The rain in Spain falls mainly on the plain.”char s2[] = “Hello, World!”;s1[12]=s2[0];s2[7] = s1[12]; |
after executing the values of name1 and name2 will be:name1 = ____________________name2 = ____________________ | after executing the values of s1 and s2 will be:s1 = ____________________s2 = ____________________ |
2. A program includes the following variable declarations:int i1, i2;int *p1,*p2;List the output produced by the following code sample. In addition, using the examples from class, draw a memory diagram showing the 4 variables i1, i2, p1 and p2. Make sure the pointers have a link to the addresses they point to! Draw these diagrams to show the memory state as things change
Code Sample | Output Generated By The Program | Memory Diagram As Things Change |
i1 = 8;i2 = 9;p1 = &i1;p2 = &i2; cout << “*p1=” << *p1 << endl;cout << “*p2=” << *p2 << endl;*p1 = 10;*p2 = *p1;cout << “i1=” << i1 << endl;cout << “i2=” << i2 << endl;p2 = p1;*p2 = 11;cout << “*p1=” << *p1 << endl;cout << “*p2=” << *p2 << endl; | ![]()
|
3. A program includes the following variable declarations:double d1, d2;double *pd1,*pd2;List the output produced by the following code sample. In addition, using the examples from class, draw a memory diagram showing the 4 variables d1, d2, pd1 and pd2. Make sure the pointers have a link to the addresses they point to! Draw these diagrams to show the memory state as things change.
Code Sample | Output Generated By The Program | Memory Diagram As Things Change |
d1 = 12.4;d2 = 8.9;pd1 = &d1;pd2 = &d2; *pd1 = *pd2;*pd2 = 41.6;d2 = 13.4;cout << “d1=” << d1 << endl;cout << “d2=” << d2 << endl; | ![]()
|
4. For each variable listed on the left, determine whether that variable must deleted to prevent memory problems. If so, state the appropriate delete statement necessary to destroy the dynamic variable and return the memory it uses to the heap so that other variables may use this memory. Please assume that the following constant declarations have been made:const int SIZE = 100;
|
- Your submission should follow this organization:
- part_a
- responses.txt
- q2_step_diagram.pdf
- q3_step_diagram.pdf
- part_a
Reviews
There are no reviews yet.