mercredi 3 août 2022

Wierd situtation is causing segmentation fault

Below is a very simple driver code for stack implementation I wrote:

#include <iostream>
#include <string>
#include "stack.h"
#include "vector.h"
 
void print(stack<std::string, vector<std::string>> stk)
{}
 
int main()
{
    vector<std::string>
        v1{"1","2","3","4"},
        v2{"Ɐ","B","Ɔ","D","Ǝ"};
 
    stack<std::string, vector<std::string>> s1{std::move(v1)};
    stack<std::string, vector<std::string>> s2{std::move(v2)};
 
    print(s1);
    print(s1);
    print(s2);
}

For some very weird reason, it crashes at runtime complaining about segmentation fault. For even more strange reason, the following works (and so does a few other orderings of print(sx) statements:

#include <iostream>
#include <string>
#include "stack.h"
#include "vector.h"
 
void print(stack<std::string, vector<std::string>> stk)
{}
 
int main()
{
    vector<std::string>
        v1{"1","2","3","4"},
        v2{"Ɐ","B","Ɔ","D","Ǝ"};
 
    stack<std::string, vector<std::string>> s1{std::move(v1)};
    stack<std::string, vector<std::string>> s2{std::move(v2)};
 
    print(s1);
    //print(s1);
    print(s2);
}

In case, you are wondering, why am I using vector instead of std::vector; I wrote my own vector class too. Thanks to Sam Varshavchik comment, the problem might be in the vector class. This is the link to it: vector

My machine specifications:

  • Ubuntu 22.04 LTS
  • GCC 11.2.0
  • -std = c++11
  • Compiled with g++ main.cc -o main.out (nothing fancy)

stack.h

#ifndef STACK_H
#define STACK_H

#include <utility>
#include <deque>

template<typename T, class Sequence = std::deque<T>>
class stack
{
public:
    typedef          Sequence                   container_type;
    typedef typename Sequence::value_type       value_type;
    typedef typename Sequence::reference        reference;
    typedef typename Sequence::const_reference  const_reference;
    typedef typename Sequence::size_type        size_type;

    stack() : c(Sequence()) {}
    explicit stack(const Sequence& __c) : c(__c) {}
    explicit stack(Sequence&& __c) : c(std::move(__c)) {}
    stack(const stack& __s) : c(__s.c) {}
    stack(stack&& __s) : c(std::move(__s.c)) {}
    ~stack() {}

    stack& operator=(const stack& __s)
    {
        c = __s.c;
        return *this;
    }
    stack& operator=(stack&& __s)
    {
        c = std::move(__s.c);
        return *this;
    }

    bool empty() const { return c.empty(); }
    size_type size() const { return c.size(); }
    reference top() { return c.back(); }
    const_reference top() const { return c.back(); }
    void push(const value_type& __x) { c.push_back(__x); }
    void push(value_type&& __x) { c.push_back(std::move(__x)); }
    void pop() { c.pop_back(); }
    void swap(stack& __s) { std::swap(c, __s.c); }

    inline bool operator==(const stack& __s) { return c == __s.c; }
    inline bool operator!=(const stack& __s) { return c != __s.c; }
    inline bool operator<(const stack& __s) { return c < __s.c; }
    inline bool operator<=(const stack& __s) { return c <= __s.c; }
    inline bool operator>(const stack& __s) { return c > __s.c; }
    inline bool operator>=(const stack& __s) { return c >= __s.c; }

protected:
    Sequence c;
};

#endif

Aucun commentaire:

Enregistrer un commentaire