tags:

views:

68

answers:

3

I am trying to develop a C++ application. Part of the Application is meant to create and initiate some properties of an Object and then store the object in a multi-dimensional array. Problem is after Object creation and storing the objects in the array, retrieving the Object values gives me pointers to NULL.

Please see code below for exact implementation:

Cell** TestMain::convertToMatrix(){

//char[] lengthArr = arra[0];

//int[][] temp
int rowCount = getCurrentRowCount(); // Gives the row count of the multi-dimensional array
int colCount =  getCurrentColCount(); // Gives the column count of the multi-dimensional array

Cell** cellList;

cellList = new Cell*[rowCount];
for (int rowIter=rowCount-1;rowIter>=0; rowIter-- ){
    cellList[rowIter] = new Cell[colCount];
    for (int colIter=colCount-1;colIter>=0;colIter--) {
        Cell *currentCell = new Cell(arra[rowIter][colIter],rowIter,colIter);

        //Calculate weights
        if (0==currentCell->getValue()) currentCell->setWeight(0);
        if (1== currentCell->getValue()) {
            if (isEdge(rowIter,colIter)) {
                currentCell->setWeight(1);
            }
            else {
                //currentCell->setWeight(1 + getMinimumValue(cellList[rowIter+1][colIter]->getWeight(),cellList[rowIter+1][colIter+1]->getWeight(),cellList[rowIter][colIter+1]->getWeight() ) );
                currentCell->setWeight(1 + getMinimumValue(cellList[rowIter+1][colIter].getWeight(),cellList[rowIter+1][colIter+1].getWeight(),cellList[rowIter][colIter+1].getWeight() ) );
            }


        }

        cellList[rowIter][colIter] = *currentCell;
    }
}
return cellList;

} `

Here is the code that performs the checking later in the code:

void StrawberryMain::printField(Cell** arrayOfCells) {

int row=0;
int column=0;

int maxRowCount= getCurrentRowCount();
int maxColCount = getCurrentColCount();


for (;row<maxRowCount;row++) {
    Cell *cellArr = arrayOfCells[row];
    for (;column<maxColCount;column++) {
        Cell currentArrayCell = cellArr[column];

        /*if (currentArrayCell==NULL){   // This line throws an error ->No match for ‘operator==’ in ‘currentArrayCell == 0’. Why?  
        printf("Returned Pointer for Cell was NULL");
        }
        else { */

            printf("%s(%s)|", currentArrayCell.getWeight(),currentArrayCell.getValue());
        /


    //}

    printf("\n");


}

}

When I run the program I get a whole load of nulls printed on my screen as output.( One null for every object supposed stored in the array

I come from a Java background ( although I have dabbled in QT C++ before) so I am a bit miffed why this is happening. As much as I would appreciate an answer I would value an explanation as to why this happens ( or a link which explains why this happens) as I really want to understand the workings of the Language.

Thanks in anticipation.

A: 

just to give you an idea

template<typename T>
struct array {
    array(int m, int n) {
        size_[0] = m, size_[1] = n;
        data_.resize(m*n);
    }
    T* operator[](int i) {
        return &data_.front() + i*size_[1];
    }
private:
    stdvector<T> data_;
    size_t size_[2];
};
aaa
Thanks , even though your solution looks very interesting ( are you fcreating some sort of custom array?) but I would really want to know why the above code returned nulls. It would help my learning process. But Thanks again
Kris Ogirri
A: 

There are several issues in your code.

As already stated in comments, you have a memory leak issue.

if (currentArrayCell==NULL){ // This line throws an error ->No match for ‘operator==’ in ‘currentArrayCell == 0’. Why?

currentArrayCell as declared in your code is a Cell object. Not a pointer to one. So you aren't comparing if a pointed to Cell is NULL. That line is trying to compare if a Cell == 0. And since you apparently haven't defined an equality operator that could work with a Cell and 0 the compiler raises that error.

With that in mind, you should note that the line Cell currentArrayCell = cellArr[column]; is actually creating a copy of a Cell. It may not be important this time. But if you write similar code where you would modify currentArrayCell, then you would find that any changes are only made to the local copy and not to the element in cellArr.

This line:

printf("%s(%s)|", currentArrayCell.getWeight(),currentArrayCell.getValue());

is most likely not doing what you wanted. s% means you must pass a string (meaning something like a const char*). However, based on your other code I'm guessing that those member functions are returning integers. printf is a low level tool and does not have the ability to convert between data types in that manner. You either need to use the appropriate format specifier for the data type (such as %d for int) or convert the values before passing them to printf.

So what happens when you use the wrong format specifier is that printf tries to byte-wise interpret whatever you actually passed as whatever type the format specifier implies. In your case, it's trying to interpret integers as character pointers. I'm actually surprised this isn't causing a crash instead of just printing nulls.

TheUndeadFish
Most of your ideas seem quite sound, I will try them out and get back to you with the results.
Kris Ogirri
I have accepted the answer as this `printf` problem was partly the problem. Apparently passing an integer argument to printf when it is expecting a String will return a null as output. The main problem apparently stemmed from the 'for' loop and the lack of initializer in it. Once I added the initialisers into the `for` loop everything worked!
Kris Ogirri
A: 

It seems clear from your code that you're a C guy doing C++, so here are some classes that should be aware of in light of your goals.

Boost's ublas has a matrix implementation that would be a generally superior alternative to creating your own implementation.

Baring that, at bare minimum you should probably be working with vectors instead of dynamically created arrays to reduce the potential for memory leaks.

no one important