读取文件时出现访问冲突异常

时间:2014-11-09 21:19:28

标签: c++ access-violation yuv

我正在对原始视频YUV文件进行一些计算。我们的想法是逐个像素地移动整个帧的8x8窗口。这是我的代码:

    while ( (frameNumber < maxFrame) && 
            (fread_s(frame1, frameSize, 1, frameSize, file1)== frameSize) &&
            (fread_s(frame2, frameSize, 1, frameSize, file2)) == frameSize)
{
    unsigned char *p1 = frame1;     //pointer to indicate start of a window
    unsigned char *p2 = frame2;  
    unsigned char *p8_1 = NULL;     //pointer used to navigate accross 8x8window
    unsigned char *p8_2 = NULL;

    for (int i = 0; i < countY; i += stepSize)
    {
        p1 += i*width;                       //move to next line
        p2 += i*width;

        for (int j = 0; j < countX; j += stepSize)
        {



            meanLuma1 = 0;
            meanLuma2 = 0;

            //8x8 window loop

            for (int k = 0; k < windowSize; k++)
            {
                p8_1 = p1 + k*width;             //move to next line of a window
                p8_2 = p2 + k*width;

                for (int l = 0; l < windowSize; l++)
                {
                    meanLuma1 += *p8_1;       
                    meanLuma2 += *p8_2;    //Access violation here at i=60, others are 0
                    ++p8_1;
                    ++p8_2;

                }

            }
            meanLuma1 = meanLuma1 / (windowSize*windowSize);
            meanLuma2 = meanLuma2 / (windowSize*windowSize);

            ++p1;
            ++p2;

        }

我一直在p8_2上获得访问冲突异常(i = 60; j,k,l = 0)。我认为这很奇怪,p8_1成功读取其值,但p8_2没有,因为两个文件都有相同的大小和尺寸。可变状态如下:

width=352;
height=288;
stepSize=4;
windowSize=8;

我正在计算如下的最大步骤数:

    int countX = ((width - windowSize) / stepSize)+1;
    int countY = ((height - windowSize) / stepSize)+1;

第二个奇怪的是,如果我设置windowSize = 16stepSize = 8,它就会成功编译。

你没有看到任何明显的问题吗?我很难找到这个bug多年了。

1 个答案:

答案 0 :(得分:1)

这里似乎是行中的错误:

p1 += i*width;                     //move to next line
p2 += i*width;

你在每一步的算术进展中走得太远了。尝试:

p1 += width;                       //move to next line
p2 += width;


p1 += stepSize*width;              //move to next line
p2 += stepSize*width;

(取决于width和stepSize度量单位)。