0

そこで、Jetbrains Clion IDE を使用して C++ でレイトレーサーを作成しています。マルチサンプリング アンチエイリアシングを有効にして 600 * 600 のイメージを作成しようとすると、メモリが不足します。次のエラーが表示されます。

'std::bad_alloc'
what(): std::bad_allocのインスタンスをスローした後に呼ばれる終了

このアプリケーションは、異常な方法で終了するようランタイムに要求しました。詳細については、アプリケーションのサポート チームにお問い合わせください。

私のレンダリング機能のコード:

幅: 600 高さ: 600 numberOfSamples: 80

void Camera::render(const int width, const int height){
    int resolution = width * height;
    double scale = tan(Algebra::deg2rad(fov * 0.5));  //deg to rad
    ColorRGB *pixels = new ColorRGB[resolution];
    long loopCounter = 0;
    Vector3D camRayOrigin = getCameraPosition();
    for (int i = 0; i < width; ++i) {
        for (int j = 0; j < height; ++j) {
            double zCamDir = (height/2) / scale;
            ColorRGB finalColor = ColorRGB(0,0,0,0);
            int tempCount = 0;
            for (int k = 0 ; k < numberOfSamples; k++) {
                tempCount++;
                //If it is single sampled, then we want to cast ray in the middle of the pixel, otherwise we offset the ray by a random value between 0-1
                double randomNumber = Algebra::getRandomBetweenZeroAndOne();
                double xCamDir = (i - (width / 2)) + (numberOfSamples == 1 ? 0.5 : randomNumber);
                double yCamDir = ((height / 2) - j) + (numberOfSamples == 1 ? 0.5 : randomNumber);
                Vector3D camRayDirection = convertCameraToWorldCoordinates(Vector3D(xCamDir, yCamDir, zCamDir)).unitVector();
                Ray r(camRayOrigin, camRayDirection);
                finalColor = finalColor + getColorFromRay(r);
            }
            pixels[loopCounter] = finalColor / numberOfSamples;
            loopCounter++;
        }
    }
    CreateImage::createRasterImage(height, width, "RenderedImage.bmp", pixels);
    delete pixels;      //Release memory
}

C++初心者なので、よろしくお願いします。Microsoft Visual Studio の C# でも同じことを試みましたが、メモリ使用量は 200MB を超えませんでした。私は何か間違ったことをしているような気がします。ご協力いただける場合は、詳細をお知らせいたします。

4

1 に答える 1

2

を使用して割り当てられたメモリは、 を使用してnew []割り当てを解除する必要がありますdelete []

の使用により、プログラムに未定義の動作があります

delete pixels;      //Release memory

メモリの割り当てを解除します。次のようにする必要があります。

delete [] pixels;
于 2016-04-10T04:58:05.707 に答える