T09_mt_sol - COMP 152 Spring 2010 Midterm Solutions 1. (a)...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
COMP 152 Spring 2010 Midterm Solutions 1. (a) 10 20 10 10 20 20 (b) x[3] should not be accessed as x is a dynamic array containing only 3 elements (x[0], x[1], and x[2]). So when i = 3, the access to x[i] is invalid in the loop. delete [] x; should be used instead of delete x as x is an inte- ger array. There is a memory leak problem - the reference to the dynamic integer array pointed by the ”original” x is lost in the line ”x=y;”; that integer array can never be freed, thus some memory is leaked 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2. (a) Copy constructor: Ring::Ring() { insert_head = NULL; } (b) Destructor: Ring::~Ring() { if (insert_head == NULL) return; RingNode* del, *del_next; while (insert_head != insert_head->next) { del = insert_head -> next; del_next = del -> next; delete del; insert_head->next = del_next; } delete insert_head; } 2
Background image of page 2
(c) Assignment operator: { // self-copy return *this; // destroy itself // another ring destruction method if( insert_head != NULL ){ // at least one node RingNode *head = insert_head -> next; // new head after ring breaking RingNode * del; // node to delete insert_head -> next = NULL; // break the ring to become linear list del = head;
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 9

T09_mt_sol - COMP 152 Spring 2010 Midterm Solutions 1. (a)...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online