Search code examples
c++allocationkarnaugh-map

Access violation writing location... Where is the bug? (Veitch diagram)


NOTE: I had two variables with the same name... Big thanks to Stefan Birladeanu and Henrik for noticing that!*

Recently I started writing code that helps me input the values of bool function to Veitch (Karnaugh) diagram with 4 variables. The code should write elements to matrix size 4x4 but with these indexes:

  1. element - index 3,3
  2. element - index 2,3
  3. element - index 3,2
  4. element - index 2,2
  5. element - index 0,3
  6. element - index 1,3
  7. element - index 0,2
  8. element - index 1,2
  9. element - index 3,0
  10. element - index 2,0
  11. element - index 3,1
  12. element - index 2,1
  13. element - index 0,0
  14. element - index 1,0
  15. element - index 0,1
  16. element - index 1,1 This is the code of main():

        void main()
        {
            int n;
    
        n=4;
    
        int **VeitchDiagram;
    
        //allocate memory for Veitch diagram
        VeitchDiagram = new int *[n];
        for(int i=0; i<n; i++)
            VeitchDiagram[i]=new int [n];
    
        //enter the elements
        for(int i=0; i<n; i++)
        {
            int j, k;
            if(i%2==1)
            {
                k=0;
                if(i<2)
                    j=4;
                else
                    j=-1;
                for(int k=0; k<2; k++)
                {
                    if(i<2)
                        j--;
                    else
                        j++;
                    cin >> VeitchDiagram[k][j];     //this part writes the input to elements with index (at least it should do that):
                    k++;                            //0,3     1,3     0,2     1,2     if i%2==1 and i<2
                    cin >> VeitchDiagram[k][j];     //0,0     1,0     0,1     1,1     if i%2==1 and i>=2
                    k--;
                }
            }
            else
            {
                k=3;
                if(i<2)
                    j=4;
                else
                    j=-1;
                for(int k=0; k<2; k++)
                {
                    if(i<2)
                        j--;
                    else
                        j++;
                    cin >> VeitchDiagram[k][j];     //this part writes the input to elements with index (at least it should do that):
                    k--;                            //3,3     2,3     3,2     2,2    if i%2==0 and i<2
                    cin >> VeitchDiagram[k][j];     //3,0     2,0     3,1     2,1    if i%2==0 and i>=2
                    k++;
                }
            }
        }
    
        //free memory allocated for VeitchDiagram
        for(int i=0; i<n; i++)
            delete [] VeitchDiagram[i];
        delete [] VeitchDiagram;
    }
    

Solution

  •         for(int k=0; k<2; k++)
            {
                if(i<2)
                    j--;
                else
                    j++;
                cin >> VeitchDiagram[k][j];     //this part writes the input to elements with index (at least it should do that):
                k--;                            //3,3     2,3     3,2     2,2    if i%2==0 and i<2
                cin >> VeitchDiagram[k][j];     //3,0     2,0     3,1     2,1    if i%2==0 and i>=2
                                     ^ k == -1
    

    But you really should learn how to use a debugger.