0

I wrote my implementation of Vector class but when my program is running, I'm getting CrtlsValidHeapPointer(block) error only when reallock function is called. Below is my code.

#include "IntVector.h"
#include <stdlib.h>
#include <iostream>

using namespace std;

IntVector::IntVector() {
    this->size = 0;
    this->arr = (int*)malloc(sizeof(int));
    this->arr[0] = 1;
}

int IntVector::getsize() {
    return this->size;
}

int IntVector::get(int index)
{
    if (index >= size) {
        exit(0);
    }
    
    return this->arr[index];
}

void IntVector::add(int n) {
    this->size++;
    int* p = (int*)realloc(this->arr, this->size * sizeof(int));

    this->arr = p;
    this->arr[size - 1] = n;
    
}

void IntVector::remove(int index) {
    
    if (index < this->size && size > 0) {
        this->size--;
        int* newarr = (int*)malloc((this->size) * sizeof(int));

        int j = 0;
        for (int i = 0; i < size + 1; i++) {
            if (i == index) continue;

            newarr[j] = this->arr[i];
            j++;
        }

        free(this->arr);
        this->arr = newarr;
        
    }
}

int IntVector::getindex(int item)
{
    for (int i = 0; i < this->size; i++) {
        if (this->arr[i] == item) {
            return i;
        }
    }

    return -1;
}

I analyzed rest of my code and I think that problem lies in this class. Values in my program are correct until error is thrown.

  • `int* newarr = (int*)malloc((this->size) * sizeof(int));` is malloc of 0 supported? I don't remember my `c` its been decades since I have used `malloc` – drescherjm Nov 22 '22 at 17:14
  • 1
    @drescherjm it's allowed and it might return NULL. Passing NULL to realloc or free is allowed. – user253751 Nov 22 '22 at 17:15
  • 2
    this probably means you wrote outside an array. It might be this array or a completely different array. – user253751 Nov 22 '22 at 17:16
  • Do you have a destructor for your class? This may be a rule of 3 violation. – drescherjm Nov 22 '22 at 17:24
  • 1
    Can you break into the function in your debugger? What value was `this->arr`? Was it something like `0xFEEEFEEE` or `0xFDFDFDFD` or `0xDDDDDDDD`? These and a few other values tell you the nature of the problem: [https://stackoverflow.com/questions/127386/what-are-the-debug-memory-fill-patterns-in-visual-studio-c-and-windows](https://stackoverflow.com/questions/127386/what-are-the-debug-memory-fill-patterns-in-visual-studio-c-and-windows) – drescherjm Nov 22 '22 at 17:25
  • I do not. I try add this. – Kuba Sławiński Nov 22 '22 at 17:26
  • @drescherjm value of `this->arr` is 0x0000024d190049a0. – Kuba Sławiński Nov 22 '22 at 17:28
  • It's probably what @user253751 said. Some type of out of bounds access to this or some other array. – drescherjm Nov 22 '22 at 17:38
  • Another common mistake is copying the pointer and freeing it more than once. If you write `IntVector a; IntVector b=a;` then now you have two IntVectors with the same data array and you might accidentally delete the data array twice – user253751 Nov 22 '22 at 17:48

0 Answers0