Dynamic memory allocation causing SIGSEGV (Signal: segmentation violation), In attempt to work with malloc and free

138 views Asked by At

I am creating a mathematical data structure in c++20 (Mingw-w64 clion) which i can partially offload from memory to storage and vice versa, Due to its capacity and size. While learning how i could work with memory i tested a particular code which to my knowledge it should work. But the app crashes due to segmentation violation (SIGSEGV) and i could not deduce why so. I would really appreciate your help.

//Utility
template<typename C, typename T>
std::basic_ostream<C, T> &
operator<<(std::basic_ostream<C, T> &_out, const logics::atomic_logic &_s) {
    _out << _s.to_string();
    return _out;
}


  • The following is not working.

    int main() {
        using namespace std;
        using namespace logics;
        auto *pointer = (logic_sym *) malloc(sizeof(logic_sym));
        *pointer = logic_sym(26);
        std::cout << (*pointer) << std::endl;
        free(pointer);
        return 0;
    }
    

    yet the others below are working.
    this one:-

    int test2() {
        using namespace std;
        using namespace logics;
        auto *pointer = (logic_sym *) malloc(sizeof(logic_sym));
        *pointer = logic_sym(26);
        string s=pointer->to_string();
        std::cout << s<< std::endl;
        free(pointer);
        return 0;
    }
    

    and this:-

    int test3() {
        using namespace std;
        using namespace logics;
        auto *pointer = new logic_sym(26u);
        *pointer = logic_sym(26u);
        std::cout << *pointer << std::endl;
        free(pointer);
        return 0;
    }
    


    EDIT The atomic_logic is not quite important but below is the logic_sym.

    
        struct logic_sym  final : public atomic_logic {
            [[nodiscard]] constexpr bool cached() const override {
                return false;
            };
    
            [[nodiscard]] const logic_id &get_id() const override {
                return id;
            }
    
            [[nodiscard]] constexpr logic_type get_type() const override {
                return logic_type::sym_t;
            }
    
            constexpr int operator<=>(const logic_id &_id) const override {
                return id - _id;
            }
    
            [[nodiscard]] std::string to_string() const override {
                if (id > 26)
                    return (std::string("p") + std::to_string(id));
                return (std::string() + ((char) (id + 'a' - 1)));
            }
    
            [[maybe_unused]] explicit logic_sym(const logic_id &id) {
                std::cout << "Aha! creating: " << id << std::endl;
                this->id = id;
            }
    
            logic_sym(const logic_sym &orig)=delete;
    
        private:
            logic_id id;
        };
    
  • 1

    There are 1 answers

    2
    bmargulies On BEST ANSWER

    You may not simply cast a void * pointer to a pointer to logic_sym. You must use a placement new operator to initialize the memory.

    new(pointer) logic_sym(26)
    

    However, why are you fussing with malloc and free instead of just using new and delete?`