Search code examples
c++visual-c++delete-operator

c++ delete [] always hangs


I have implemented my own simple vector, all functions seems fine except on alloc_new(), when it tries to create new memory and copy the contents their and delete the old memory allocation. My programs always hangs and never execute after that.

                class myvector{
            private:
                int vsize, maxsize;
                int* arr;
                void alloc_new();
            public:
                myvector();
                myvector(int);
                myvector(const myvector&);
                ~myvector();

                void push_back(int);
                int size();
                int operator[](int);
                int at(int);
                void display();
            };

            myvector::myvector(){
                maxsize = 20;
                vsize = 0;
                arr = new int[maxsize];
            }

            void myvector::alloc_new(){
                // Allocate new space, double of current size
                maxsize = maxsize*2;
                int* arr_new = new int[maxsize];
                //copy the elements from the base location to new location
                for(int i=0; i < vsize ; i++)
                    arr_new[i] = arr[i];
                delete[] arr;  // MY PROGRAM ALWAYS HANGS HERE
                arr = arr_new;
            }

            void myvector::push_back(int val){
                if((vsize+1) > maxsize)
                    alloc_new();
                arr[++vsize] = val;
            }

            int main(){
                myvector vect;
                for(int i=0;i<25;i++){
                    vect.push_back(rand()%100+1);
                }
                getch();
            }

Solution

  • The problem is that your "have we reached max size" is wrong. You are comparing vsize + 1 > maxsize. This means that you are writing to arr[++vsize] when vsize + 1 == maxsize. This writes one element PAST your allocation [in other words, index 20 were 0..19 are valid indices], and thus goes wrong.

    Fix the code like this:

    void myvector::push_back(int val){
        if((vsize+1) >= maxsize)
        alloc_new();
        arr[++vsize] = val;
    }
    

    Using valgrind will tell you:

    $ valgrind ./a.out
    ==20918== Memcheck, a memory error detector
    ==20918== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
    ==20918== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
    ==20918== Command: ./a.out
    ==20918== 
    ==20918== Invalid write of size 4
    ==20918==    at 0x4009E9: myvector::push_back(int) (in /home/MatsP/src/junk/a.out)
    ==20918==    by 0x400AA1: main (in /home/MatsP/src/junk/a.out)
    ==20918==  Address 0x5a20090 is 0 bytes after a block of size 80 alloc'd
    ==20918==    at 0x4C2A77C: operator new[](unsigned long) (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==20918==    by 0x4008B4: myvector::myvector() (in /home/MatsP/src/junk/a.out)
    ==20918==    by 0x400A57: main (in /home/MatsP/src/junk/a.out)
    ==20918== 
    ==20918== 
    ==20918== HEAP SUMMARY:
    ==20918==     in use at exit: 0 bytes in 0 blocks
    ==20918==   total heap usage: 2 allocs, 2 frees, 240 bytes allocated
    ==20918== 
    ==20918== All heap blocks were freed -- no leaks are possible
    ==20918== 
    ==20918== For counts of detected and suppressed errors, rerun with: -v
    ==20918== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)