Removing an element from an array
May 3, 2015 at 8:56am UTC
Hello, I am looking to implement my own Vector ADT and I am currently assigning "0" when the method push_back is called. How do I make it so that instead assigning "0", the index is actually freed up?
Below is my current push_back function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
void Vector_double::pop_back() {
array[currentIndex-1] = 0.0;
currentIndex--;
}
void Vector_double::push_back(double element_value) {
// out of bounds
if (currentIndex+1 > maxSize ) {
resize();
}
// not out of bounds
array[currentIndex] = element_value;
currentIndex++;
}
void Vector_double::resize() {
maxSize = maxSize *2;
double temp_array[maxSize];
// copy contents from old array to new array
for (int u = 0; u < currentIndex; u++) {
temp_array[u] = array[u];
}
array = (double *) calloc (maxSize,sizeof (double ));
// re-copy temporary array
for (int i = 0; i < maxSize; i++) {
array[i] = temp_array[i];
}
}
Last edited on May 3, 2015 at 9:13pm UTC
May 3, 2015 at 10:12am UTC
If you do by "free an index" actually mean "deallocate memory", then you should tell us how have you "allocated the index" in the first place?
May 3, 2015 at 9:14pm UTC
Hello keski,
I have modified the post to reflect your query.
Topic archived. No new replies allowed.