i was trying to create a deque, and when the function that resizes the array is excuted, it crashes with the error from the title, and when i delete the deallocate() it stops happening, what im doing wrong? code: https://pastebin.com/0yHHcLnj

  • ratatosk@feddit.de
    link
    fedilink
    arrow-up
    3
    arrow-down
    2
    ·
    edit-2
    10 months ago

    Double free corruption means you deallocate some memory twice.

    Let me add a question: do you really want to implement your own deque? Because there already exists one in the STL.

      • ratatosk@feddit.de
        link
        fedilink
        arrow-up
        2
        ·
        edit-2
        10 months ago

        I see. Let me know if my hint doesn’t take you further, then i take a closer look. It seems, that .destroy deallocates anyway and the later .deallocate deallocates the already deallocated memory again.

        • prettydarknwild@lemmy.worldOP
          link
          fedilink
          arrow-up
          1
          ·
          edit-2
          10 months ago

          that makes sense, although is kinda weird because i also did a string implementation before doing the same thing and it worked, also the c++ reference and the book that im using to learn c++ says that .destroy() only destroys the object it doesnt deallocate

          • ratatosk@feddit.de
            link
            fedilink
            arrow-up
            2
            ·
            10 months ago

            That’s btw. another error. You byte-copy the elements and Call the destructor on the original ones. This will work only, if your element’s type is trivially destructible.

            Given your T has an allocating constructor and therefore an deallocating destructor:

            Creating a T Calls T::T() that allocates memory.

            Resizing your deque Calls T::~T() by .destroy(), deallocating this memory.

            Destructing your deque should destroy the elements. Thus you call the constructor once and the destructor 1+number of deque resizes.

              • ratatosk@feddit.de
                link
                fedilink
                arrow-up
                2
                ·
                edit-2
                10 months ago

                If you develop or debug a container, it is useful to have a special test class for the elements that covers potential container specific errors.

                struct ContainerTester {
                	static int instances;
                	int counter{};
                	int val;
                	
                	ContainerTester() : ContainerTester(0) {}
                	ContainerTester(int val) : val(val)
                	{
                		++counter;
                		++instances;
                	}
                	~ContainerTester() {
                		--counter;
                		--instances;
                		if (counter < 0) std::cout << "multiple destructor calls on same element" << std::endl;
                		if (instances < 0) std::cout << "negative number of instances" << std::endl;
                	}
                };
                
                int ContainerTester::instances{};
                
                std::ostream& operator<<(std::ostream& o, const ContainerTester& c) {return o << c.val;}
                

                If I run your code with ContainerTester instead of int, i get:

                negative number of instances
                negative number of instances
                negative number of instances
                negative number of instances
                negative number of instances
                negative number of instances
                multiple destructor calls on same element
                negative number of instances
                double free or corruption (out)
                segmentation fault
                

                So it’s more obvious that very bad things do happen :)

                Oh and note, that allocator::destroy is deprecated in C++17 and was removed with C++20.

                • prettydarknwild@lemmy.worldOP
                  link
                  fedilink
                  arrow-up
                  1
                  ·
                  10 months ago

                  i used tthe tester class with my code removing the .deallocate(), and although it doesnt crash, it still runs the destructor multiple times on the same element, i think its because im just pushing i into the container, and because that constructor creates an implicit conversion between int and ContainerTester, it creates a temporary object that gets destroyed once it is pushed into the deque, am i right?