由于内存问题,C ++代码立即崩溃

时间:2018-05-30 03:10:00

标签: c++ memory-leaks out-of-memory

此代码应读取.pnm文件。我尝试用2种分辨率运行它:

  • 200x200 px:这里一切正常。

  • 500x281 px:代码立即崩溃,引发SIGSEGV错误。

据我所知,SIGSEGV与内存问题有关。我有8GB的RAM,我认为这个数量足以运行它。我不知道它为什么会发生以及如何解决它。

代码

#define IO_ERROR                (5)
#define X_DIMENSION             (500)
#define Y_DIMENSION             (281)
#define C_DIMENSION             (3)
#define HEADER_SIZE             (3)
#define BODY_SIZE               (X_DIMENSION * Y_DIMENSION * C_DIMENSION)

#include <iostream>
#include <fstream>
#include <string>
using namespace std;


int readImage(string fileName, string *imgHeader, string *imgBody)
{
        ifstream inputFile(fileName);

        if (!inputFile.is_open())
                return IO_ERROR;

        string line;
        int count = 0;
        while (getline(inputFile, line)) {
                if (line.find("#") != string::npos)     
                        continue;
                if (count < 3)                          
                        imgHeader[count] = line;
                else                                    
                        imgBody[count - HEADER_SIZE] = line;
                count++;
        }
        inputFile.close();

        return 0;
}


void numericParser(const string *imgBody, unsigned char *numericBody)
{
        int i = 0;
        while(i < BODY_SIZE) {
                numericBody[i] = (unsigned) atoi(imgBody[i].c_str());
                i++;
        }
}

void rgbParser(const string *imgBody, unsigned char rgbMatrix[X_DIMENSION][Y_DIMENSION][C_DIMENSION])
{
        unsigned char numericBody[BODY_SIZE];
        numericParser(imgBody, numericBody);
        int k = 0;
        for (int i = 0; i < X_DIMENSION; i++) {
                for (int j = 0; j < Y_DIMENSION; j++) {
                        for (int c = 0; c < 3; c++) {
                                rgbMatrix[i][j][c] = numericBody[k];
                                k++;
                        }
                }
        }
}

void printInfo(const string *header, const string *body)
{
        cout << "#-*- Image Header -*-" << endl;
        for (int i = 0; i < HEADER_SIZE; i++) {
                cout << header[i] << endl;
        }

        cout << "#-*- Image Body -*-";
        for (int i = 0; i < 5 * C_DIMENSION; i++) {
                if (i % 3 == 0) cout << endl << "R: " << body[i];
                else if (i % 3 == 1) cout << " G: " << body[i];
                else cout << " B: " << body[i];
        }
        cout << endl << ". . ." << endl;
}


int main()
{
        string fileName;
        cout << "File name: ";
        cin >> fileName;

        string imgHeader[HEADER_SIZE];
        string imgBody[BODY_SIZE];
        if(readImage(fileName, imgHeader, imgBody) == IO_ERROR)
                return IO_ERROR;

//        printInfo(imageData);

        unsigned char rgbMatrix[X_DIMENSION][Y_DIMENSION][C_DIMENSION];
        rgbParser(imgBody, rgbMatrix);

        return 0;
}

其他信息

我在Arch Linux 64位上使用CLion作为IDE。

0 个答案:

没有答案