0

I create a program which enter a dynamic array of integer. I want to add a copy constructor, in order to become better acquainted with its functionality, but always when try to run program, Visual Studio open a new tab and get delete_scalar.cpp Error, so if anyone had a similar problem it would help me.

Here' s code:

DynamicArray.h

#pragma once
class DynamicArray
{
private:
    int* arr;
    int capacity;
    int number_of_elements;
public:
    DynamicArray(int capacity = 1);
    DynamicArray(const DynamicArray&);
    ~DynamicArray();
    void add(DynamicArray* arr, int value);
    void print(DynamicArray* arr);
};

DynamicArray.cpp

#include "DynamicArray.h"
#include <iostream>

DynamicArray::DynamicArray(int capacity): capacity(capacity),number_of_elements(0),arr(new int[capacity])
{}
DynamicArray::~DynamicArray()
{
    delete[] this->arr;
    this->arr = nullptr;
    this->number_of_elements = 0;
    this->capacity = 0;
}
DynamicArray::DynamicArray(const DynamicArray& other) : capacity(other.capacity),number_of_elements(other.number_of_elements),arr(new int[other.number_of_elements])
{
    
    for (int i = 0; i < number_of_elements; i++)
    {
        this->arr[i] = other.arr[i];
    }

}

main.cpp

int main()
{  
        DynamicArray obj(5);
    DynamicArray obj2(5);
    obj.add(&obj,22);
    obj.add(&obj,12);
    obj.add(&obj,18);
    obj.add(&obj,11);
    obj.print(&obj);
    obj2=obj;
    obj2.print(&obj2);
}

Error delete_scalar.cpp

_CRT_SECURITYCRITICAL_ATTRIBUTE
void __CRTDECL operator delete(void* const block) noexcept
{
    #ifdef _DEBUG
    _free_dbg(block, _UNKNOWN_BLOCK);
    #else
    free(block);
    #endif
}

Thanks in advance!

Best regards!

Wikallou
  • 11
  • 2
  • 1
    The problem is here `obj2=obj;`. That is not a copy constructor, it is an assignment operator. Since you have not written the assignment operator yet, you get a crash. If you want to test your copy constructor write something like this `DynamicArray obj3(obj);` – john Nov 10 '22 at 12:38
  • you are performing assignment not copy construction – Neil Butterworth Nov 10 '22 at 12:38
  • Also `this->` should not be needed in your code. And for memory sizes you should use std::size_t instead of int. This will avoid problems with allocating negative memory sizes as well as provide enough bits to store large amount of data. – Pepijn Kramer Nov 10 '22 at 12:39
  • OT: Note that resetting values of the member variables in the destructor is useless. Why to assign values to objects when their lifetime is going to end? – Daniel Langr Nov 10 '22 at 12:40
  • somewhat related: https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three/4172724#4172724 – 463035818_is_not_an_ai Nov 10 '22 at 12:40
  • why does `add` take a pointer to a `DynamicArray` as paramter? You didnt show the definition, but the parameter and its name (which is the same as the member) suggests that there are more issues present – 463035818_is_not_an_ai Nov 10 '22 at 12:43
  • Thanks I was able to test my copy constructor! @john – Wikallou Nov 10 '22 at 12:48
  • It is very odd to pass an object as an argument to its own member function. I think you have missed some important point about member functions. – molbdnilo Nov 10 '22 at 13:11

0 Answers0