P
pragtideep
Kindly help me explain the behaviour of defult copy constructor . Why
the destructor is freeing the SAME memory twice , though it was
allocated just once .
#include<iostream>
using namespace std;
class var_array
{
private:
int *data; // The data
const int size; // The size of the data
public:
var_array(const int _size):
size(_size)
{
data = new int[size];
memset(data, '\0',
size * sizeof(int));
}
// Destroy the var_array
~var_array(void) {
delete []data;
}
public:
// Get an item in the array
int &operator [] (
// Index into the array
const unsigned index
)
{
return (data[index]);
}
};
static void store_it(
var_array test_array
)
{
test_array[7] = 7;
}
int main()
{
var_array test_array(30);
store_it(test_array);
return (0);
}
the destructor is freeing the SAME memory twice , though it was
allocated just once .
#include<iostream>
using namespace std;
class var_array
{
private:
int *data; // The data
const int size; // The size of the data
public:
var_array(const int _size):
size(_size)
{
data = new int[size];
memset(data, '\0',
size * sizeof(int));
}
// Destroy the var_array
~var_array(void) {
delete []data;
}
public:
// Get an item in the array
int &operator [] (
// Index into the array
const unsigned index
)
{
return (data[index]);
}
};
static void store_it(
var_array test_array
)
{
test_array[7] = 7;
}
int main()
{
var_array test_array(30);
store_it(test_array);
return (0);
}