0

This program gives me segfault sometimes. I wanted to compare copy ctor with move ctor on speed. What is interesting it never throws segfault when running in gdb.

#include <iostream>
 #include <cstdlib>
 #include <ctime>
using namespace std;

 class Alma{
     protected:
        int* v;
        int size;
     public:
        void p_r(void);
        void fill_r(void);
        Alma(int size){
            this->size=size;
            v= (int*) malloc(size * sizeof(int));
        }
        ~Alma(){
            delete v ;
        } 
        Alma(const Alma& a){
            clock_t begin = clock();
            v= (int*) malloc(size * sizeof(int));
            size=a.size;
            for(int i =0; i< size;i++ ){
                 v[i]=a.v[i];
            }
            clock_t end = clock();
            double ms = double(end - begin) / CLOCKS_PER_SEC;
            cout << "copy / time:"<< ms << endl;
        }
};
class Korte:public Alma{
    public:
        Korte(int size):Alma(size){}

    Korte& operator=(Korte&& a){
            clock_t begin = clock();
             v=a.v;
             size=a.size;
             a.size=0;
             a.v=nullptr;
             clock_t end = clock();
             double ms = double(end - begin) / CLOCKS_PER_SEC;
             cout << "move / time:"<< ms << endl;
            return *this;
         }
};
void Alma::fill_r(){
    for(int i =0; i< size;i++ ){    
        v[i]=rand();
    }
}
int main(){
    Alma a(20000000);
    a.fill_r();
    Alma b = a;
    Korte k(20000000);
    k.fill_r();
    Korte k2(2);
    k2=move(k);
    return 0;
}

At first I wanted to sort vectors but vectors gave me segfault when I used too many elements.(tens of thousands) even with setting the initial size. So I just wrote this. What is the problem?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Mate
  • 3
  • 1
  • 2
    Is there any reason you're expecting the move operation to take more than 0ms to happen? You're just shuffling pointers. – Havenard Oct 24 '18 at 17:28
  • Your classes violate [the rule of three](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three), but that doesn't appear to be directly related to the problem you're seeing. – Mooing Duck Oct 24 '18 at 17:36
  • No it is to show it is a lot faster than copying. Yes it violates the rule of three and the rule of five. I just wanted to keep it short. This c++ thing is new for me because I only studied Java before. – Mate Oct 24 '18 at 18:38

1 Answers1

6

The problem is in below lines in copy constructor of Alma

v= (int*) malloc(size * sizeof(int));
size=a.size;

when you call malloc what is the value of size ? It is indeterminate, so this call leads to undefined behaviour. size=a.size should be called as first.

size=a.size;
v= (int*) malloc(size * sizeof(int));

The second issue, you allocate memory by malloc C-function, but why do you delete by delete ? It is not consistent, when you use malloc you should call free to delete memory.

free(v);

You may read about rule of three and consider using virtual destructor in base class.

rafix07
  • 20,001
  • 3
  • 20
  • 33
  • Thank you! I literally spent hours trying to find the solution. I don't know how could I miss that. I probably copied it in a bad order... I tried to use free like this: free v . It did't work so I just used delete. It is still interesting tough that sometimes it worked, and it always worked with gdb, and gcc did't give any warning. Maybe I'm just used to javac... – Mate Oct 24 '18 at 17:55