1

I'm writing a class for a Deque in cpp, and have almost completed it entirely. I am running into a weird bug where I get trash values after the first insert_tail() function call.

Here is the following code I'm referring to. Header File:

//File: Deque.h

#ifndef DEQUE_H
#define DEQUE_H

#include <iostream>

const int CAPACITY = 5;
const int DEFAULT = -1;

class Deque
{
public:
    Deque()
    {
        front = 1;
        end = 0;
        size = 0;
    }

    int get_size() const;

    int operator[](int) const;

    void insert_head(int);
    int remove_head();

    void insert_tail(int);
    int remove_tail();

    bool is_empty() const;
    bool is_full() const;

private:
    int size;
    int x_[CAPACITY];
    int front;
    int end;
};
std::ostream & operator<<(std::ostream &, const Deque &);

#endif

Implementation File:

//File: Deque.cpp

#include <iostream>
#include "Deque.h"

std::ostream & operator<<(std::ostream & cout, const Deque & dq)
{
  cout << dq.get_size() << " [ ";
  for (int i = 0; i < dq.get_size(); ++i)
  {
    cout << dq[i] << " ";
  }
  cout << "]";
  return cout;
}

int Deque::operator[](int x) const
{
    return x_[x];    
}

int Deque::get_size() const
{
    return size;
}

void Deque::insert_head(int element)
{
    if (size != CAPACITY)
    {
        x_[front] = element;
        front = (front+1) % CAPACITY;
        size++;
    }
}

int Deque::remove_head()
{
    if(is_empty())
    {
        return DEFAULT;
    }
    else
    {
        front = (front-1) % CAPACITY;
        size--;
        return x_[front];        
    } 
}

void Deque::insert_tail(int element)
{
    if (!is_full())
    {
        x_[end] = element;
        end = (end-1) % CAPACITY;
        size++;
    }
}


int Deque::remove_tail()
{
    if(is_empty())
    {
        return DEFAULT;
    }
    else
    {
        end = (end+1) % CAPACITY;
        size--;
        return x_[end];
    } 
}

bool Deque::is_empty() const
{   
    return (size == 0);
}

bool Deque::is_full() const
{
    return (size == CAPACITY);
}

Test File:

//File: main.cpp

#include <iostream>
#include "Deque.h"

void print(const Deque & deque)
{
  static int i = 1;
  std::cout << i << ". " << deque  << ", empty: " << deque.is_empty() 
            << ", full: " << deque.is_full();
  i++;
}

void test_remove_head(Deque & deque)
{
  int x = deque.remove_head();
  print(deque); std::cout << ", x: " << x << "\n";
}

void test_remove_tail(Deque & deque)
{
  int x = deque.remove_tail();
  print(deque); std::cout << ", x: " << x << "\n";
}

void test_insert_tail(Deque & deque, int x)
{
  deque.insert_tail(x);
  print(deque); std::cout << "\n";
}

void test_insert_head(Deque & deque, int x)
{
  deque.insert_head(x);
  print(deque); std::cout << "\n";
}

void test1()
{
  Deque deque; print(deque); std::cout << "\n";
  test_remove_tail(deque);
  test_remove_head(deque);
  test_insert_tail(deque, 2);
  test_insert_tail(deque, 1);
  test_insert_tail(deque, 0);
  test_insert_head(deque, 5);
  test_insert_head(deque, 1);
  test_insert_tail(deque, 0);
  test_insert_head(deque, 2);
  test_remove_head(deque);
  test_remove_tail(deque);
  test_remove_tail(deque);
  test_remove_tail(deque);
  test_remove_head(deque);
  test_remove_head(deque); 
  test_remove_head(deque);
}

void test2()
{
  Deque deque;
  for (int i=0; i<15; i++) test_insert_head(deque, i);
  for (int i=0; i<15; i++) test_remove_tail(deque);
}

int main()
{
  test1(); test2();
  return 0;  
}

Output:

1. 0 [ ], empty: 1, full: 0
2. 0 [ ], empty: 1, full: 0, x: -1
3. 0 [ ], empty: 1, full: 0, x: -1
4. 1 [ 2 ], empty: 0, full: 0
5. 2 [ 2 0 ], empty: 0, full: 0
6. 3 [ 2 0 0 ], empty: 0, full: 0
7. 4 [ 2 5 0 4198128 ], empty: 0, full: 0
8. 5 [ 2 5 1 4198128 0 ], empty: 0, full: 1
9. 5 [ 2 5 1 4198128 0 ], empty: 0, full: 1
10. 5 [ 2 5 1 4198128 0 ], empty: 0, full: 1
11. 4 [ 2 5 1 4198128 ], empty: 0, full: 0, x: 1
12. 3 [ 2 5 1 ], empty: 0, full: 0, x: 0
13. 2 [ 2 5 ], empty: 0, full: 0, x: 2
14. 1 [ 2 ], empty: 0, full: 0, x: 2
15. 0 [ ], empty: 1, full: 0, x: 5
16. 0 [ ], empty: 1, full: 0, x: -1
17. 0 [ ], empty: 1, full: 0, x: -1
18. 1 [ 32764 ], empty: 0, full: 0
19. 2 [ 32764 0 ], empty: 0, full: 0
20. 3 [ 32764 0 1 ], empty: 0, full: 0
21. 4 [ 32764 0 1 2 ], empty: 0, full: 0
22. 5 [ 4 0 1 2 3 ], empty: 0, full: 1
23. 5 [ 4 0 1 2 3 ], empty: 0, full: 1
24. 5 [ 4 0 1 2 3 ], empty: 0, full: 1
25. 5 [ 4 0 1 2 3 ], empty: 0, full: 1
26. 5 [ 4 0 1 2 3 ], empty: 0, full: 1
27. 5 [ 4 0 1 2 3 ], empty: 0, full: 1
28. 5 [ 4 0 1 2 3 ], empty: 0, full: 1
29. 5 [ 4 0 1 2 3 ], empty: 0, full: 1
30. 5 [ 4 0 1 2 3 ], empty: 0, full: 1
31. 5 [ 4 0 1 2 3 ], empty: 0, full: 1
32. 5 [ 4 0 1 2 3 ], empty: 0, full: 1
33. 4 [ 4 0 1 2 ], empty: 0, full: 0, x: 0
34. 3 [ 4 0 1 ], empty: 0, full: 0, x: 1
35. 2 [ 4 0 ], empty: 0, full: 0, x: 2
36. 1 [ 4 ], empty: 0, full: 0, x: 3
37. 0 [ ], empty: 1, full: 0, x: 4
38. 0 [ ], empty: 1, full: 0, x: -1
39. 0 [ ], empty: 1, full: 0, x: -1
40. 0 [ ], empty: 1, full: 0, x: -1
41. 0 [ ], empty: 1, full: 0, x: -1
42. 0 [ ], empty: 1, full: 0, x: -1
43. 0 [ ], empty: 1, full: 0, x: -1
44. 0 [ ], empty: 1, full: 0, x: -1
45. 0 [ ], empty: 1, full: 0, x: -1
46. 0 [ ], empty: 1, full: 0, x: -1
47. 0 [ ], empty: 1, full: 0, x: -1

As you can see, after line 4. The proper insert statements are not completed, and also the x: from function "remove_head()" and "remove_tail()" are not receiving the correct return values.

Taffywall
  • 11
  • 1
  • Use your debugger and set a breakpoint on the test that fails. – PaulMcKenzie Oct 13 '18 at 23:44
  • You're getting a negative result from `%` when `end` is 0.. See e.g. [this question](https://stackoverflow.com/questions/7594508/modulo-operator-with-negative-values). – molbdnilo Oct 13 '18 at 23:59
  • Hey, I'm quite new to coding on the text editor xemacs on linux os fedora 25, so how would one go about debugging on xemacs? – Taffywall Oct 14 '18 at 03:31
  • @Taffywall install GDB and step through it that way. Better yet, install cgdb. – Sailanarmo Oct 14 '18 at 04:03

0 Answers0