C ++,无法通过.size()函数获得有效的向量大小

时间:2016-11-27 12:38:35

标签: c++ vector

我不熟悉C ++。我所做的就是使用RLE(运行长度编码)压缩和解压缩图片。

现在,我不确定这是否有问题:

unsigned char *compressedData = new unsigned char[size];

因为每次我想要经历,它都会停留在这里。弹出一个窗口显示:

enter image description here

希望有人给出建议,非常感谢! 我整天都被困在这里。

这是我写的部分:

unsigned char *CAppCompress::Compress(int &cDataSize) {

// You can modify anything within this function, but you cannot change the function prototype.


int i, j, n ;

unsigned int r, g, b ;
std::vector<unsigned int> rgb24;
std::vector<unsigned int> compressedDataTemp;

for(j = 0; j < height; j++) {
    for(i = 0; i < width; i++) {
        b = pInput[(i + j * width) * 3 + 0] ;   // Blue Color Component
        g = pInput[(i + j * width) * 3 + 1] ;   // Red Color Component
        r = pInput[(i + j * width) * 3 + 2] ;   // Green COlor Component
        rgb24.push_back((r << 16) | (g << 8) | b ) ;
    }
}

unsigned int count=1;

compressedDataTemp.push_back(count && 0xFF);

compressedDataTemp.push_back(rgb24[0] & 0xFF);              //b   8 bits
compressedDataTemp.push_back((rgb24[0]>> 8) & 0xFF);        //g   8 bits
compressedDataTemp.push_back((rgb24[0]>> 16) & 0xFF);       //r   8 bits



for(n = 1; n < rgb24.size(); n++) {

    while(rgb24[n]==rgb24[n-1] && count<256)
    {
        count++;
        n++;
    }

    compressedDataTemp.push_back(count && 0xFF);

    compressedDataTemp.push_back(rgb24[n] & 0xFF);              //b   8 bits
    compressedDataTemp.push_back((rgb24[n]>> 8) & 0xFF);        //g   8 bits
    compressedDataTemp.push_back((rgb24[n]>> 16) & 0xFF);       //r   8 bits

    count=1;
}

int k=0;
//cDataSize = compressedDataTemp.size() ;
int size;

size = compressedDataTemp.size();

unsigned char *compressedData = new unsigned char[size];

for(k = 0; k < size; k++)
{
    compressedData[k] = compressedDataTemp[k] & 0xFF;
}

memcpy(compressedData, pInput, size) ;

return compressedData ;     // return the compressed data

}

1 个答案:

答案 0 :(得分:2)

当您在断点处停止时,程序在执行该行代码之前处于状态

因此,您在分配之前看到size,而不是之后。