c++ - Access violation writing location... bug在哪里? (维奇图)

标签 c++ allocation karnaugh-map

注意:我有两个同名变量...非常感谢 Stefan Birladeanu 和 Henrik 注意到这一点!*

最近我开始编写代码,帮助我将 bool 函数的值输入到具有 4 个变量的 Veitch(卡诺)图。代码应将元素写入 4x4 大小的矩阵,但具有以下索引:

  1. 元素 - 索引 3,3
  2. 元素 - 索引 2,3
  3. 元素 - 索引 3,2
  4. 元素 - 索引 2,2
  5. 元素 - 索引 0,3
  6. 元素 - 索引 1,3
  7. 元素 - 索引 0,2
  8. 元素 - 索引 1,2
  9. 元素 - 索引 3,0
  10. 元素 - 索引 2,0
  11. 元素 - 索引 3,1
  12. 元素 - 索引 2,1
  13. 元素 - 索引 0,0
  14. 元素 - 索引 1,0
  15. 元素 - 索引 0,1
  16. 元素 - 索引 1,1 这是 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;
    }
    

最佳答案

        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

但是你真的应该学习如何使用调试器。

关于c++ - Access violation writing location... bug在哪里? (维奇图),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9840647/

相关文章:

c++ - 我如何使用 opencv 取 100 张图像的平均值?

c++ - 确定正确的原点以围绕 QPolygonF 旋转

jpa - 如何调整 EclipseLink 预分配策略

java - 由socketRead0分配的字符串TLAB

circuit - 数字逻辑 - 卡诺图

c++ - 与 const std::T 的并发

减少 CPU 指令大小的 C++ 技术?

c++ - 计算 std::string 的分配内存(以及 std::vector 中字符串的使用)

analysis - 证明卡诺图的非最优性

boolean - K-Map 解决代数约简问题