0

In my header file for my PCBTable.cpp I have this:

class PCBTable {
private:
PCB* table[];
}

In my PCBTable.cpp I am trying to create a constructor that initializes the table to a blank table of PCB objects, with the size of my parameter and it looks like this

PCBTable::PCBTable(int size) {
    table = new PCB*[size];
}

However I get the error that table must be a modifiable value(E0137), and that array type PCB*[] is not assignable.

I've tried messing around with this but I can't get it to work.

2
  • 6
    Tip: Forget this new[] stuff and use std::vector. If you're slinging bare pointers around, you may want to wrap those as well. Commented Feb 6, 2023 at 22:22
  • 3
    PCB* table[]; is not valid. Commented Feb 6, 2023 at 22:22

2 Answers 2

4

new T[...] returns a T* pointer, where T in your case is PCB*, thus you are asking new[] to return a PCB** pointer, which cannot be assigned to a PCB*[] 1 fixed array, hence the error.

(1 you are not specifying the size of the array, which is invalid syntax)

You need to change your table declaration to match what new[] actually returns, eg:

class PCBTable {
private:
    PCB** table;
    int table_size;
public:
    PCBTable(int size);
    ~PCBTable();
};
PCBTable::PCBTable(int size) : table_size(size) {
    table = new PCB*[size];
    for (int i = 0; i < size; ++i) {
        table[i] = new PCB;
    }
}

PCBTable::~PCBTable() {
    for (int i = 0; i < table_size; ++i) {
        delete table[i];
    }
    delete[] table;
}

Alternatively, if you just need an array of objects, rather than an array of pointers to objects:

class PCBTable {
private:
    PCB* table;
    int table_size;
public:
    PCBTable(int size);
    ~PCBTable();
};
PCBTable::PCBTable(int size) {
    table_size = size;
    table = new PCB[size];
}

PCBTable::~PCBTable() {
    delete[] table;
}

That being said, you really should be using std::vector instead of new[]/delete[] manually. And if you are using C++11 or later, use std::unique_ptr or std::shared_ptr instead of raw pointers, eg:

#include <vector>
#include <memory>

class PCBTable {
private:
    std::vector<std::unique_ptr<PCB>> table;
public:
    PCBTable(int size);
};
PCBTable::PCBTable(int size) : table(size) {
    for (auto &elem : table) {
        elem = std::make_unique<PCB>();
    }
}

Or:

#include <vector>
#include <memory>

class PCBTable {
private:
    std::vector<PCB> table;
public:
    PCBTable(int size);
};
PCBTable::PCBTable(int size) : table(size) {
}
Sign up to request clarification or add additional context in comments.

Comments

0

There seems to be some confusion here. Arrays (declared like int foo[12]) are fixed-size at compile time and can be found on the stack. If you want a dynamically sized array, a list whose size is defined at runtime and isn't necessarily known at compile time, that needs to be a pointer. If the elements you want in your array are themselves pointers, it will have to be a pointer to pointers.

class PCBTable {
private:
    PCB** table;
}
PCBTable::PCBTable(int size){
    table = new PCB*[size];
}

Or, better yet, you could use std::vector, which gives you all the best of both worlds.

#include <vector>

class PCBTable {
private:
    std::vector<PCB*> table;
}
PCBTable::PCBTable(int size){
    table.resize(size);
}

You can use std::vector any way you would use a normal array. This also provides the dual benefit of not having to store the array size in an external variable, as table.size() takes care of that for you.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.