Code that crashes

> could I bother you to write for me some short C++ code that crashes?
> I am just learning to use gdb and I don't have any code that would be appropriate to learn with.
> But am excited to learn.

Header:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#ifndef MY_LIST_H_INCLUDED
#define MY_LIST_H_INCLUDED

#include <cstddef>
#include <iostream>

namespace my
{
    template < typename T > struct list
    {
        // the only constructor we have is the explicitly defaulted default constructor
        list() = default ;

        // non-copyable and non-moveable
        list( const list& ) = delete ;
        list( list&& ) = delete ;
        list operator= ( const list& ) = delete ;
        list operator= ( list&& ) = delete ;

        ~list() { clear() ; }

        std::size_t size() const { return sz ; }
        bool empty() const { return size() == 0 ; }
        std::ostream& print( std::ostream& stm = std::cout ) const
        {
            for( node* n = first ; n != nullptr ; n = n->next ) stm << n->value << ' ' ;
            return stm << '\n' << std::flush ;
        }

        void clear() { while( !empty() ) pop_back() ; }

        void push_back( const T& v ) // *** buggy ***
        {
            last->next = new node( v, last ) ;
            last = last->next ;
            ++sz ;
        }

        void pop_back() // *** buggy ***
        {
            if( size() == 1 )
            {
                delete last ;
                first = last = nullptr ;
                --sz ;
            }
            else if( !empty() )
            {
                last = last->prev ;
                delete last->next ;
                // std::cout << "deleted node at " << last->next << std::flush ;
                --sz ;
            }
        }

        private:
            struct node
            {
                explicit node( const T& v, node* prev = nullptr, node* next = nullptr )
                    : value(v), prev(prev), next(next) {}

                T value ;
                node* prev ;
                node* next ;
            };

            node* first = nullptr ;
            node* last = nullptr ;
            std::size_t sz = 0 ;
    };
}

#endif // MY_LIST_H_INCLUDED 


Test frame:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "my_list.h"

template struct my::list<int> ;

int main()
{
    my::list<int> lst ;
    std::cout << "before push_back\n" << std::flush ;
    lst.push_back(23) ; // *** crash ****
    std::cout << "after push_back\n" << std::flush ;

    // after the above error has been fixed, fix the error in pop_back
    for( int v : { 1, 2, 3, 4, 5 } ) lst.push_back(v) ;
    lst.pop_back() ; // *** buggy ***
    lst.pop_back() ; // *** buggy ***
    lst.print() ; // *** error (possible crash / incorrect output) ****
}
Last edited on
Topic archived. No new replies allowed.