Implementing the insert array method for a hand-written dynamic array - C++ -
so code supposed take dynamic array , inserts smaller array , if there's not enough space in larger array makes new array , copies values old array new 1 smaller array can inserted. here's code allocates new size new array dynamic_array &a being smaller array , being position it's inserted to:
void dynamic_array::insert(dynamic_array &a, int i) { if (i < 0 or > size){ throw exception(subscript_range_exception); } int *new_array; int range = a.get_size(); //my size method return how many values in int blocks_needed = (size) / block_size; if (size % 5 > 0) { blocks_needed = blocks_needed + 1; //add block if needed } if (size + range >= allocated_size) { //new space needed //get more space try { new_array = new int[blocks_needed * block_size]; } catch (bad_alloc){ throw exception (memory_exception); }
then there's 3 different loops. 1 copies elements before i-1. second copies elements in array a. third 1 copies remaining elements old array new array while shifting them over:
//copy array[0..i-1] (int j = 0; j < i; j++) { new_array[j] = array[j]; } //copy (int m = i; m < range; m++){ new_array[m] = a[m]; } //copy array[i..size-1] (int k = i; k < size; k++) { new_array[k+range] = array[k]; }
afterwards update sizes:
size = size + range; allocated_size = blocks_needed * block_size;
now if there enough space shift array right , insert values via loop:
else { //no new space needed shift_right(i, size, range); (int z = i; z < range; z++){ array[z] = a[z]; } size = size + range; }
now output i'm getting quite close want it's not quite want. there's alot of times array values show things 0 3 0 2 4
when should show 0 1 3 2 4
. allocated size wrong when shows it's 5 when should 10. size looks fine tested arrays it's allocated size , array values that's problem. here's shift right function:
void dynamic_array::shift_right(int start, int end, int delta){ for(int i=end; i>=start; i--){ array[i+delta] = array[i]; } return; }
alright while don't have code method being discussed, nor dynamic_array::shift_right()
code, i've noticed 2 mistakes right off bat (thanks @thedark nitpicking on fix):
first one:
for (int m = i; m < range; m++) { new_array[m] = a[m]; }
should be
for (int m = i; m < range + i; m++) { new_array[m] = a[m - i]; } // ^^^ ^^^
another 1 of same kind in last, shifting loop:
for (int z = i; z < range; z++) { array[z] = a[z]; }
should be:
for (int z = i; z < range + i; z++) { array[z] = a[z - i]; } // ^^^ ^^^
in fact, these may causing crash on trying access a[]
out of bounds. on side note, that's 1 of reasons why it's better, namely safer, use iterators simple integer indices container access.
i didn't either:
int blocks_needed = (size) / block_size; if (size % 5 > 0) { blocks_needed = blocks_needed + 1; //add block if needed }
this looks hand-writing ceil()
function. other that, 5
apparently should equal block_size
, else makes little sense me. thus, replace 5
block_size
or better yet switch ceil()
.
now on allocation-related errors. here's one:
int blocks_needed = (size) / block_size; int blocks_needed = (size + range) / block_size; // ^^^^^^^
and here's another, while not error, it's suboptimal behavior nonetheless:
if (size + range >= allocated_size) { //new space needed if (size + range > allocated_size) { //new space needed // ^^^
Comments
Post a Comment