[使用映射到1D的2D数组的访问冲突读取位置

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[使用映射到1D的2D数组的访问冲突读取位置相关的知识,希望对你有一定的参考价值。

我正在尝试检查数组中的邻居,并且不检查边缘情况是否有限制,该程序将导致异常。我需要至少检查bottomLeft,bottomRight,topLeft topRight角。我正在DirectXTDK中工作,该功能用于平滑风景。

bool Terrain::SmoothenHeightMap(ID3D11Device* device)

    bool result;

    int index, nx, nz;
    float height = 0.0;
    int neighbours[8] = ; // array starts at 0, inclusive
    int n = 8;

    /* Initialise corner of height map */ // 1.
    int bottomLeftCorner = 0;
    int bottomRightCorner = (m_terrainHeight * (m_terrainHeight - 1));
    int topLeftCorner = (m_terrainWidth - 1);
    int topRightCorner = m_terrainHeight * (m_terrainHeight - 1) + (m_terrainWidth - 1);

    m_frequency = (6.283 / m_terrainHeight) / m_wavelength; //we want a wavelength of 1 to be a single wave over the whole terrain.  A single wave is 2 pi which is about 6.283
    // m_terrainHeight is actually the z axis

    for (int j = 0; j < m_terrainHeight; j++)
    
        for (int i = 0; i < m_terrainWidth; i++)
        
            index = (m_terrainHeight * j) + i; 

            float sum = m_heightMap[index].y;

            // with more than 128 square dimensions, initial neighbours on bottom row might not exist
            // can refractor this better if it works

            if (m_heightMap[(m_terrainHeight * (j - 1)) + (i - 1)].x != NULL) 
                neighbours[0] = m_heightMap[(m_terrainHeight * (j + 1)) + (i - 1)].y; // top left
                neighbours[1] = m_heightMap[(m_terrainHeight * (j + 1)) + (i)].y;     // top middle
                neighbours[2] = m_heightMap[(m_terrainHeight * (j + 1)) + (i + 1)].y; // top right
                neighbours[3] = m_heightMap[(m_terrainHeight * (j)) + (i - 1)].y;     // middle left
                neighbours[4] = m_heightMap[(m_terrainHeight * (j)) + (i + 1)].y;     // middle right
                neighbours[5] = m_heightMap[(m_terrainHeight * (j - 1)) + (i - 1)].y; // bottom left
                neighbours[6] = m_heightMap[(m_terrainHeight * (j - 1)) + (i)].y;     // bottom middle
                neighbours[7] = m_heightMap[(m_terrainHeight * (j - 1)) + (i + 1)].y; // bottom right
            




            for (int z = 0; z < n; z++)
            
                if (neighbours[z] < 0 || neighbours[z] >= m_terrainHeight * m_terrainWidth) // if out of map, take y of current index for sum
                
                    sum += m_heightMap[index].y;
                
                else
                
                    sum += neighbours[z]; // if exists, include in sum
                
            

            // smoothen based on neighbours
            m_heightMap[index].y = sum / 9.0f; // current point n is no. of neighbours +1 for current vertex point// total of 9 points in a 3*3 grid
        
    


    result = CalculateNormals();
    if (!result)
    
        return false;
    

    result = InitializeBuffers(device);
    if (!result)
    
        return false;
    

提到的代码用于使用高度图来平滑地形,以穿过存储在结构中的点。

答案

由于我没有完整的代码块,所以我可以告诉您查找错误的可能方法。

正如我在屏幕截图中看到的那样,错误线无法匹配与数组关联的值或位置。我建议您调试代码并逐步跳过每个for循环以获得m_terrainHeight * (j-1) + (i-1)的执行中值。上面的表达式可能正在评估一个意外的值。

对所有分配和执行重复相同的逐行执行过程,以检查您是否获得期望值。

[请共享整个代码块,以更好地理解问题。

以上是关于[使用映射到1D的2D数组的访问冲突读取位置的主要内容,如果未能解决你的问题,请参考以下文章

异常错误:访问冲突读取位置 0xDDDDDDDD

当我尝试删除数组时,C++ 访问冲突读取位置 0xDDDDDDCD 已更新

调试模式下内存映射向量的读取访问冲突

如何将 1d numpy 数组附加到 2d numpy 数组 python

使用2d数组写入访问冲突

将 2d 边界转移到其 1d 网格上