-1

main.cpp:

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

int main() {
        array_list dynamic_list;
        dynamic_list.print();
        dynamic_list.append(2);
        dynamic_list.append(4);
        dynamic_list.print();

        return 0;
    }

array_list.cpp:

#include "array_list.h"
#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

//class constructor
array_list::array_list() {
    size = 0;
    capacity = 1;
    growth_factor = 2;
    data = new int[capacity];

}

array_list::~array_list() {
    delete[] list;
}

array_list::array_list(vector<int> initial) {
    size = 0;
    capacity = 1;
    list = new int[capacity];

    for (int x: initial) {
        append(x);
    }
}

int array_list::length(){
    return size;
}

void array_list::resize() {
    if (size > capacity) {
        capacity *= growth_factor;

        int *temp_list = new int[capacity];

        for (int i = 0; i < size; i++){
            temp_list[i] = list[i];
        }
        delete[] list;
        list = temp_list;
    }
}

void array_list::append(int n) {
    if (size >= capacity) {
        resize();
    }
    list[size] = n;
    size++;
}

void array_list::print() {
    {
        if (size == 0) {
            cout << "size is 0, empty list" << endl;
        } else if (size > 0) {
            cout << "[";
            for (int i = 0; i < size - 1; i++) {
                cout << list[i];
                cout << ", ";
            }
            cout << list[size - 1] << "]" << endl;
        }
    }
}

array_list.h:

#include "math.h"

#include <iostream>
#include <iomanip>
#include <vector>
#include <stdexcept>
#include <cmath>

using namespace std;


class array_list {

    private:
        int size;
        int growth_factor;
        int capacity;
        int *list;

    public:
        array_list();
        ~array_list();

        array_list(vector<int>initial);

        void resize();
        void append(int temp);
        void print();

        int length();
    };


    #endif

I am making a custom list class in c++. I want to test it, but when I do, I get no output to the terminal. I have been looking at the code for hours, and yes I have tried with a debugger. A fresh set of eyes is nice to have when stuck in situations like these.

When I run this code, I am supposed to get the following output to the terminal: [2, 4]

But I get nothing instead. What is wrong here?

Update: I found the issue. I had reinstantiated the variables within the constructor making them local variables. This ultimately ruined the entire structure of the object.

  • 5
    Your constructor doesn't initialize most of your members (only `list` is initialized), it creates local variables instead – UnholySheep Nov 02 '18 at 13:17
  • 6
    This seems like a great opportunity to [learn how to debug your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Some programmer dude Nov 02 '18 at 13:18
  • 1
    ***I have been looking at the code for hours, and can't spot the bug.*** You really need to spend some time (20 minutes or so) learning how to use your debugger. It will save you time in the long run. Instead of spending hours you should have already seen the problems. – drescherjm Nov 02 '18 at 13:19
  • 2
    Possible duplicate of [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Max Vollmer Nov 02 '18 at 13:19
  • Like @UnholySheep said `int size = 0;` declares a new local variable size. This does not have anything to do with the class variable of the same name. – drescherjm Nov 02 '18 at 13:21
  • resize needs to update to the new size as well – lostbard Nov 02 '18 at 13:26
  • No that is not true resize only changes capacity not size – Can H. Tartanoglu Nov 02 '18 at 13:31
  • To be honest I am not the biggest fan of that debugging advice page even though it does contain a lot of good advice. I think it should include information on how to use the debugger in Visual Studio and gdb, specifically how to debug line by line looking at the variables. I think a lot of people new to `c++` were not taught how to use their IDE effectively. Many of them think debugging means press `F5` key and or the Play button in the IDE. Although with this said the advice is programming language in-specific so I guess that is a reason for not talking about debuggers. – drescherjm Nov 02 '18 at 15:41

1 Answers1

3

When appending an element, if resizing is necessary, you forget to actually append the said number:

void array_list::append(int n) {
    if (size < capacity) {
        list[size] = n;
        size++;
    }
    else {
        resize();
    }
}

Fix:

void array_list::append(int n) {
    if (size >= capacity) {
        resize();
    }
    list[size] = n;
    size++;
}

This is the sort of mistakes everybody (except maybe Jon Skeet) does. You have maid several of that kind in your program. They are hard to find by yourself unless you use a debugger. It might be the time for you to learn how to use a debugger.

YSC
  • 38,212
  • 9
  • 96
  • 149