0

I am new to C++ and am trying to write code for a multi-dimensional array using double pointers. This is my code:

Class Declaration:

class magicMat{

    private:
         int** ptrnum;

    public:
        void init(int);
        void Display(int);
        void set(int);
        void message();
        void errorhandling(int);    
};

Function definitions:

void magicMat::init(int input)
{       
    ptrnum=new int*[input];

    for (int row=0;row<input;row++)
        ptrnum[row]=new int[input]; 

    for(int x=0;x<input;x++)
    {
        for (int y=0;y<input;y++)
        {
            *(ptrnum[x]+y)=0;
        }
    }
}

void magicMat::set(int input)
{
    int row=1,col=input/2,otherdiag=0;

    for(int value=1;value<=input*input;value++)
    {
        if (*(ptrnum[row]+col)>0)
        {
            row=row+2;
            if(row>input)
                row=row-input;

            col--;
            if(col<1)
                col=input;
        }
        *(ptrnum[row]+col)+=value;
        *(ptrnum[0]+col)+=value;
        *(ptrnum[row]+0)+=value;

        if (row==col)
            *(ptrnum[0]+0)+=value;          

        if (row+col==input+1)
            otherdiag+=value;                 
/*                                                                        */
/*       Determine where new row and col are                              */
/*                                                                     */
         row--;
         if (row < 1)                       /* If row exceeds side then   */
            row = input;                    /*  goto other side.          */
         col++;
         if (col > input)                   /* If col exceeds side then   */
            col = 1; 
    }       
}

Main function:

int main()
{
    int num;
    magicMat newMat;
    newMat.message();
    while(1)
    {
        cin>>num;
        if (cin.good())
        {
            newMat.errorhandling(num);
        }
        else if (!isdigit(num))
        {
            cout<<"Please enter only digits"<<endl;
        }    
        newMat.init(num);
        newMat.set(num);
        newMat.Display(num);
    }
    cout<<"\nBye bye!\n"<<endl;
    return 0;
}

It works in the init function but when in the set function I try to check the value it breaks at the first if statement in the set data function.

Bart
  • 19,692
  • 7
  • 68
  • 77
SB26
  • 225
  • 1
  • 6
  • 17
  • 2
    Try just `ptrnum[x][y]`. If there's no real reason not to, use a vector of vectors instead. It's a lot safer. `vector > matrix;` – chris Apr 21 '12 at 03:45
  • well i have been asked to use double pointers.This is a homework assignment.Sorry did not tag it as HW.Done it now – SB26 Apr 21 '12 at 03:48
  • 1
    Incidentally, using an "init" function is generally against the spirit of C++. Objects should be initialized through constructors--that way it's not possible to accidentally *forget* to initialize them. http://en.wikipedia.org/wiki/Constructor_(object-oriented_programming) – HostileFork says dont trust SE Apr 21 '12 at 03:50
  • thnx for the comment.I will make that change.But any idea why the code breaks – SB26 Apr 21 '12 at 03:58
  • It seems like you have out-of-bounds errors. Things like `col = input;` and then using `col` as an index when the indices go from 0 to col - 1. – chris Apr 21 '12 at 04:03
  • I get a "Access violation reading location" at the first if loop in set – SB26 Apr 21 '12 at 04:10
  • I would go through the function and rewrite it while keeping the bounds in mind. I'm almost positive that's the problem. If you have a `matrix[5][5]`, saying `matrix[3][5]` is not correct since it's 0-4, not 1-5. Your code is doing that, it's just a bit harder to catch. – chris Apr 21 '12 at 04:16
  • its a C code which I am changing to C++.The logic is the same.It works in the C code. – SB26 Apr 21 '12 at 04:25
  • If it works in the C code, you're just getting lucky. You're indexing outside the bounds of the array and usually that will crash your program, but sometimes it won't. It just depends on what happens to be located next to the array in memory. – Brent Writes Code Apr 21 '12 at 05:12

1 Answers1

0

You are stepping outside the bounds of your array. Just as an example, if I run your code and enter 5 as my first digit which sets the value of input to 5, look at these lines at the end of your set function:

row--;
if (row < 1)
    row = input;

At the end of the first time through the loop in your set function, right before these lines, row is equal to 1. These three lines execute and as a result, row is set equal to 5.

At the beginning of your next loop in set, you do this:

if (*(ptrnum[row]+col)>0)

The problem is that ptrnum is a 5x5 array, which means valid indexes are from 0-4. At the end of the previous loop, however, you set row equal to 5 and as a result, you're indexing outside the bounds of ptrnum and thus crashing your program.

I highly recommend either stepping through your code in a debugger or if you're not sure how to do that, at least put in a bunch of cout statements in your set function so you can check that the variables are getting set to what you think they should get set to.

Good luck!

Brent Writes Code
  • 19,075
  • 7
  • 52
  • 56