I'm working on a dynamic array problem right now and have created a destructor for it. The code compiles just fine, but because I haven't been provided with a test file, I'm not actually sure if the destructor is fully functional. Would anyone be able to tell me if I'm on the right track? I've included my header in full, and .cpp with some irrelevant functions removed for the sake of readability. Destructor designated with // This is the function in question // in .cpp. Any help appreciated!
dynamic_array.h
using namespace std;
class Dynamic_array {
public:
Dynamic_array();
Dynamic_array(Dynamic_array &);
Dynamic_array &operator=(Dynamic_array &);
~Dynamic_array();
void print_state(void);
int get_size(void);
int& operator[](int);
void insert(int, int);
void insert(Dynamic_array &, int);
void remove(int);
void remove(int, int);
class Subscript_range_exception {
};
private:
enum {
BLOCK_SIZE = 5,
};
class Block {
public:
int size;
int a[BLOCK_SIZE];
Block* next_p;
};
class Block_position {
public:
Block* block_p;
Block* pre_block_p;
int i;
};
Block_position find_block(int i);
void insert_blocks(Block *, Block *);
void remove_blocks(Block *, Block *, Block *);
Block * copy_blocks(Block *);
void delete_blocks(Block *);
int size;
Block* head_p;
};
dynamic_array.cpp
#include <iostream>
#include <string.h>
#include "dynamic_array.h"
using namespace std;
// ********** public functions **********
Dynamic_array::Dynamic_array() {
head_p = NULL;
size = 0;
}
Dynamic_array::Dynamic_array(Dynamic_array & d) {
size = d.size;
head_p = copy_blocks(d.head_p);
}
Dynamic_array &Dynamic_array::operator=(Dynamic_array & d) {
if (this != &d){
this->head_p = copy_blocks(d.head_p);
this->size = d.size;
}
return *this;
}
Dynamic_array::~Dynamic_array() { // This is the function in question //
if(head_p != NULL){
while (1) {
Block * p = head_p->next_p;
delete head_p;
// advance
if (p == NULL) {
break;
} else {
head_p = p;
}
}
}
}
// ********** private functions **********
// purpose
// create a new linked list which is a copy of the list pointed to p
// return a pointer to the head of the new linked list
// preconditions
// p is the head of a possibly empty linked list of blocks
Dynamic_array::Block * Dynamic_array::copy_blocks(Block * p) {
Block * new_head_p = NULL;
Block * new_p;
while (p != NULL) {
// allocate and link in new block
if (new_head_p == NULL) {
new_p = new Block;
new_head_p = new_p;
} else {
new_p->next_p = new Block;
new_p = new_p->next_p;
}
// copy the elements
new_p->size = p->size;
for (int i = 0; i < p->size; i++) {
new_p->a[i] = p->a[i];
}
// advance
p = p->next_p;
}
// terminate new list
if (new_head_p != NULL) {
new_p->next_p = NULL;
}
return new_head_p;
}