Destructor call in a class constructor with a vector - c++

I have the following class which simply wraps an array and adds with the constructor some elements to it:
class myArray {
public:
myArray();
myArray(int a, int b);
myArray(int a, int b, int c);
myArray(myArray&& emplace);
~myArray();
int& operator [](int id);
private:
int *data;
};
myArray::myArray() {
data = new int[1];
data[0] = 0;
}
myArray::myArray(int a, int b) {
data = new int[3];
data[0] = 0;
data[1] = a;
data[2] = b;
}
myArray::myArray(int a, int b, int c) {
data = new int[4];
data[0] = 0;
data[1] = a;
data[2] = b;
data[3] = c;
}
myArray::~myArray() {
std::cout << "Destructor"<<std::endl;
delete[] data;
}
int& myArray::operator [](int id) {
return data[id];
}
myArray::myArray(myArray&& emplace) : data(std::move(emplace.data)) {}
Furthermore I have a second class which contains a vector of elements of the first class (myArray).
class Queue {
public:
Queue();
private:
std::vector<myArray> _queue;
};
Queue::Queue {
_queue.reserve(1000);
for(int a = 0; a < 10; a++)
for(int b = 0; b < 10; b++)
for(int c = 0; c < 10; c++)
_queue.emplace_back(a,b,c);
}
My question here is: Why is the destructor called for the myArray elements at the end of the Queue constructor? The Queue object is still alive in my main program but the destructor of myArray frees the allocated memory and I consequently get a segmentation fault.
Is there a way to avoid the call of the destructor or rather call it not until at the end of the Queue objects lifetime?

Your move constructor doesn't set data to null on the moved from object so when the moved from object is destructed it will try to free data.
If you have c++14 you can use std::exchange to implement this:
myArray::myArray(myArray&& emplace)
: data{std::exchange(emplace.data, nullptr)})
{}
Otherwise you need to do:
myArray::myArray(myArray&& emplace)
: data{emplace.data)
{
emplace.data = nullptr;
}
The move constructor will be invoked by std::vector as it reallocates to increase its capacity when you call emplace_back. Something like the following steps are performed:
Allocate new memory to hold the elements
Move construct using placement new elements in the new memory from the elements in the previous memory
Destruct the elements in the previous memory
Deallocate the previous memory

Related

freeing the same memory with distructors

I try to free the memory correctly after the program ends, but I always encounter a problem.
In my code I want to have an array of all the numbers that I allow in my program, and have objects A and B (or more) that each one have some of the numbers that I allowed.
In the end I want to delete 'a' and 'b' only after "ints" getting out of the scope. But A and B calls their distructors to delete some of ints variables.
#define MAX_LEN 255
class IntArray
{
public:
int len;
void add(int* n) {
arr[len] = n; len++;
}
IntArray() : arr(new int* [MAX_LEN]), len(0) {}
~IntArray() {
for (int i = 0; i < len; i++)
delete arr[i];
delete[] arr;
}
private:
int** arr;
};
class Object
{
public:
void add(int* n) {
myIntArr.add(n);
}
private:
IntArray myIntArr;
};
int main(void)
{
int* a = new int(5);
int* b = new int(6);
IntArray ints;
ints.add(a);
ints.add(b);
Object A;
A.add(a);
Object B;
B.add(b);
return 0;
}
If you want to share dynamically allocated ints between multiple objects, use std::shared_ptr<int>.
Also, rather than writing a dynamic array type yourself, use std::vector to do it (correctly) for you.
using int_ptr = std::shared_ptr<int>;
class IntArray
{
public:
void add(int_ptr n) {
arr.push_back(n);
}
private:
std::vector<int_ptr> arr
};
class Object
{
public:
void add(int_ptr n) {
myIntArr.add(n);
}
private:
IntArray myIntArr;
};
int main(void)
{
int_ptr a = std::make_shared<int>(5);
int_ptr b = std::make_shared<int>(6);
IntArray ints;
ints.add(a);
ints.add(b);
Object A;
A.add(a);
Object B;
B.add(b);
return 0;
}
If you just want to have a copyable array of int, use std::vector<int>.
You're deleting a and b twice.
You should only delete something returned by new and exactly once.
But you add them both to IntArray ints; and then one each to Objects A and B and their destructors delete them also. Destructors are called in reverse order to it's when ints is destructed you'll be deleting them again - that's "Undefined Behaviour" but normally a catastrophic failure (crash) either immediately or later during executon.
The shortest fix is:
int* a = new int(5);
int* b = new int(6);
int *ac = new int(*a);//copy of *a
int *ab = new int(*b);//copy of *b
IntArray ints;
ints.add(a);
ints.add(b);
Object A;
A.add(ac);
Object B;
B.add(bc);
But it's not clear what our intention is. IntArray isn't an array of int as it stands, it's an array of pointers to int (which have been allocated by new).
My 'fix' will mean if you modify a (e.g. *a=20) you won't modify the copy (ac) added to the Object A.

Initializing an array of pointers to a defined structure with constructor

struct Mystruct
{
int x;
int y;
Mystruct(int x, int y);
}
------------------------
class Myclass
{
Mystruct** p;
Myclass(int n);
}
------------------------
Myclass::Myclass(int n)
{
this->p = new Mystruct*[n];
for ( int i = 0; i < n; i++ )
this->p[i] = new Mystruct[n];
}
This will not work. I know the problem lies somewhere with default constructor not being available, but I do not know how to move forward from here.
you want
Myclass::Myclass(int n)
{
this->p = new Mystruct*[n];
for ( int i = 0; i < n; i++ )
this->p[i] = new Mystruct[n];
}
because Mystruct** p;
You also need to save the dimension, and to add a destructor, very probably the constructor must be public.
As said in a remark to be able to allocate your array of Mystruct that one need a constructor without parameter

How to add data to an array of pointers?

I created an array of pointers.
I want to know how to prp=operly add data to my array of pointers.
I have attempted the code below, unfortunately it is giving me a memory error and I do not know why.
class dataClass {
int data;
public:
void setdata(int d) {data = d;}
int getdata() const {return data;}
};
int main() {
dataClass** ptr = new dataClass*s[5];
int num = 9;
ptr[0] -> setdata(num);
return 0;
}
You just allocate the memory for the pointers (not for the objects), so ptr[0] points to an unspecified address and you get a segfault.
You need to add something like this:
for(int i = 0; i < 5; i++) {
ptr[i] = new dataClass;
}
When using a pointer-to-pointer-to-type, you must allocate storage for the required number of pointers and then allocate storage for each class which you then assign to each pointer.
For example, using 5 pointers as you attempt in your questions, you would first need to allocate for the pointers, e.g.
#define NPTRS 5
...
dataClass** ptrs = new dataClass *[NPTRS];
Now you have storage allocated for 5 pointers to type dataClass with the address for the initial pointer assigned to ptrs (I added the plural form for clarity)
The storage for the pointers is now allocated, but each of the allocated pointers does not yet point to valid storage for dataClass, they are just pointers. Before you can use the pointers to reference an instance of the class, you must allocate storage for each each class instance and then assign the address of the new block of memory to each of your pointers, e.g.
for (int i = 0; i < NPTRS; i++) {
ptrs[i] = new dataClass;
ptrs[i]->setdata (i+1);
}
(above you allocate storage for each class instance assigning the result to ptrs[i] and then set the data value to i+1)
Putting it altogether, you could do something similar to:
#include <iostream>
#include <cstdlib>
#define NPTRS 5
class dataClass {
int data;
public:
void setdata(int d) {data = d;}
int getdata() const {return data;}
};
int main() {
dataClass** ptrs = new dataClass *[NPTRS];
for (int i = 0; i < NPTRS; i++) {
ptrs[i] = new dataClass;
ptrs[i]->setdata (i+1);
}
for (int i = 0; i < NPTRS; i++)
std::cout << "ptrs[" << i <<"]->getdata() : "
<< ptrs[i]->getdata() << '\n';
return 0;
}
(note: don't forget you are responsible for keeping track of what you allocated and calling delete to free each allocation when it is not longer needed)
Example Use/Output
$ ./bin/dblptr2class
ptrs[0]->getdata() : 1
ptrs[1]->getdata() : 2
ptrs[2]->getdata() : 3
ptrs[3]->getdata() : 4
ptrs[4]->getdata() : 5
As others have mentioned, the C++ containers like vector are for less error prone and make your job a lot easier. Still, you should know how to handle both new/delete as well as the use of the containers.
Look things over and let me know if you have further questions.
You could first create an array of objects
(constructors will be called automatically).
After that create a pointer to your pointer:
#include <stdio.h>
class dataClass {
int data;
public:
dataClass(){
printf(" new instance \n");
}
void setdata(int d) {
data = d;
printf(" setdata \n");
}
int getdata() const {return data;}
};
int main() {
dataClass* ptr = new dataClass[5];
dataClass** pptr = &ptr;
int num = 9;
pptr[0]->setdata(num);
return 0;
}
result:
new instance
new instance
new instance
new instance
new instance
setdata

How to delete dynamic allocated array in c++?

I got stuck with deleting an dynamically allocated array of int.
I've got a destructor, where I'm trying to use a loop for to delete all elements of array and finally delete it.
I have code on http://rextester.com/OTPPRQ8349
Thanks!
class MyClass
{
public:
int _a;
int* c;
int fRozmiar;
static int fIlosc;
MyClass() //default constructor
{
_a=0;
c = new int [9];
for(int i = 0; i<=9; i++)
{
c[i] = 1;
}
fIlosc++;
}
MyClass(int a1, int c1) // parametrized constructor
{
_a=a1;
c = new int [c1];
for(int i = 0; i<=c1; i++)
{
c[i] = rand();
}
fIlosc++;
}
MyClass(const MyClass &p2) // copy constructor
{
_a =p2._a;
c = p2.c;
fRozmiar = p2.fRozmiar;
fIlosc = fIlosc;
fIlosc++;
}
~MyClass(); // destructor
static int getCount() {
return fIlosc;
}
};
//Initialize static member of class
int MyClass::fIlosc = 0;
MyClass::~MyClass()
{
for(int i = 0; i<sizeof(c); ++i)
{
delete[] c[i];
}
delete[] c;
fIlosc--;
}
int main()
{
}
Remove the for-loop, but keep the delete[] c after it.
Each int doesn't need to be deleted because they're not dynamically allocated. If you needed to delete them, then the for-loop wouldn't work becuase: sizeof(c) is not the size of the array, and delete[] should have been delete instead.
There are several problems in the code.
First, that loop in the destructor must go. If you didn’t new it, don’t delete it.
Second, a loop through an array of N elements should be for (int i = 0; i < N; ++i). Note that the test is i < N, not i <= N. The loops as currently written go off the end of the array. That’s not good.
Third, the copy constructor copies the pointer. When the first object goes out of scope its destructor deletes the array; when the copy goes out of scope its destructor also deletes the array. Again, not good. The copy constructor has to make a copy of the array. In order to do that the class needs to also store the number of elements the array.

How to pass an Array into a Class in C++

When i create a class I would like to be able to store an array in that class. Is this possible?
For example. If i have a class called array to store an array from my main function
int main()
{
double nums[3] = {1 2 3}
array Vnums(nums)
return 0
}
class array
{
public
//constructor
array(double nums[])
{
double vector[] = nums;
}// end constructor
}// end array
Thank you!
use a std::array instead of a raw array. It's just like a raw array, but copiable, and has useful member functions.
class array
{
std::array<double, 3> classArray;
public:
//constructor
explicit array(const std::array<double, 3>& rhs)
:classArray(rhs)
{}// end constructor
}// end array
int main()
{
std::array<double, 3> nums = {{1 2 3}};
array Vnums(nums)
return 0
}
or maybe a std::vector if you want to be able to change the size at will
class array
{
std::vector<double> classArray;
public:
//constructor
explicit array(const std::vector<double>& rhs)
:classArray(rhs)
{}// end constructor
}// end array
int main()
{
std::vector<double> nums{1 2 3}; //C++11 feature
array Vnums(nums)
return 0
}
I'm not sure what you're doing, so it's hard to give solid advice. You can pass a raw array by reference, a pointer and a count, an iterator pair...
Yes, but you must either allocate the array dynamically upon class creation, or the array must always be the same size.
Option A:
class array{
private:
double* data;
unsigned size;
public:
array(double* d, unsigned s){
size = s;
data = new double[size];
for(unsigned i = 0; i < s; i++)
data[i]=d[i];
}
array(const array& copy){
double* temp = new double[copy.size];
delete [] data;
data = temp;
size = copy.size;
for(unsigned i = 0; i < size; i++)
temp[i]=copy.data[i];
}
array& operator= (const array& copy){
double* temp = new double[copy.size];
delete [] data;
data = temp;
size = copy.size;
for(unsigned i = 0; i < size; i++) data[i]=copy.data[i];
}
~array(){
delete[] data; // Don't forget the destructor!
}
};
This is probably the way you need, but note that you will almost certainly need the custom copy constructor and assignment operator so that you don't share any memory between multiple instances of this class. A better way might be to make a copy function that both can use.
Option B:
class array{
private:
double data[3];
public:
array(double* d){ //or "double(&d)[3]" to be safer, but less flexible
for(unsigned i = 0; i < 3; i++){
data[i] = d[i]; // If d is not at least size 3, your program will crash here (or later on, or maybe just act in an undefined way)
}
}
}
Haven't tested this, but it should be an ok starting point.