1

I have an array of objects that have a vector as a class member (actually it is a struct). The struct is:

struct Cell {
  vector<int> borderNodes;
  vector<int> helperNodes;

  int minNodePointer;
  int maxNodePointer;


  Cell() {
    minNodePointer = -1;
    maxNodePointer = -1;
  }

  Cell(int _minNodePointer, int _maxNodePointer) {
    minNodePointer = _minNodePointer;
    maxNodePointer = _maxNodePointer;
  }

  vector<int>& getHelperNodes() {
    return helperNodes;
  }

  vector<int>& getBorderNodes()  {
    return borderNodes;
  }

  void setHelperNodesArray() {
    sort(helperNodes.begin(), helperNodes.end());
  }

  void setBorderNodesArray() {
    sort(borderNodes.begin(), borderNodes.end());
  }
}; 

I have built an array of those objects as a global variable with :

Cell* cells = new Cell[maxNumberOfCells];

and I want to add some integers to the vectors inside the objects.

I tried this (inside a function):

cells[cellId].borderNodes.push_back(node_id);

or

cells[cellId].getBorderNodes().push_back(node_id);

and they compile fine, but nothing is added to the vector. How is the correct way to do this? Here is the function that reads from a db, and adds the integers. The query and reading from db is correct, so no mistakes there.

void loadBorderNodesPerCellBnOnly(bool shouldRearrangeNodes, int subtractor, int maxNumberOfCells, int** cellBorderNodes) {
    cellBorderNodes = new int*[maxNumberOfCells];
    try {
        work W(Conn);
        for (int rownum = 0; rownum < r.size(); ++rownum) {
            const result::tuple row = r[rownum];

            vector<string> s = split(row[1].as<string > (), ' ');
            const int mySize = s.size();
            int cellId = row[0].as<int> ();
            cellBorderNodes[cellId] = new int[mySize];
            for (int k = 0; k < mySize; k++) {
                int node_id = atoi(s[k].c_str());
                cellBorderNodes[cellId][k] = node_id;
                (cells[cellId].getBorderNodes()).push_back(node_id);
                try {
                    nodes[node_id - subtractor].cellId = cellId;
                } catch (const std::exception &e) {
                    std::cerr << e.what() << std::endl;
                }
                nodes[node_id - subtractor].isBorderNode = true;
            }
            s.clear();
        }
    } catch (const std::exception &e) {
        std::cerr << e.what() << std::endl;
    }


    for (int k = 0; k < maxNumberOfCells; k++) {
        cout << k << ": " << cells[k].borderNodes.size() << endl;
        cells[k].setBorderNodesArray();
        if (maxBorderNodesPerCell < cells[k].borderNodes.size()) {
            maxBorderNodesPerCell = cells[k].borderNodes.size();
        }
    }
}
4
  • 8
    How are you determining that nothing has been added? Commented Feb 1, 2013 at 22:51
  • 2
    How do you know nothing is added? Commented Feb 1, 2013 at 22:52
  • for (int k = 0; k < maxNumberOfCells; k++) { cout << k << ": " << cells[k].borderNodes.size() << endl; } Commented Feb 1, 2013 at 22:54
  • 2
    @AlexandrosE. Can you add to your question the function in which you attempt to do the push_back and then output the sizes? Commented Feb 1, 2013 at 22:56

1 Answer 1

2
void loadBorderNodesPerCellBnOnly([...], int** cellBorderNodes) {
    cellBorderNodes = new int*[maxNumberOfCells];

The function above takes an int** by value. The copy is called cellBorderNodes inside the function. In the next line you change the value of the copy and continue updating that. None of those changes will be visible outside of the function. (And you will leak the acquired memory)

If you want to pass an int** and modify it inside the function, consider passing it by reference as in int**&. But you might be better off using higher level constructs (pass a vector<vector<int>>&, create a type that represents the data and pass it by value...)

Sign up to request clarification or add additional context in comments.

2 Comments

You are correct but my problem is not with cellBorderNodes, is with the cells array. I will fix it but my problem still remains
The problem was the delimiter used for splitting the string in the db. So now my code works. Thanks everyone for your help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.