c ++中的堆栈溢出问题

时间:2014-05-28 12:10:42

标签: c++ image stack-overflow

当我编译以下内容时,我得到堆栈溢出...

我不知道为什么......我正在尝试将平面介入的图像转换成行干预,反之亦然......有一些指导会很棒......

部首:

#include <iostream>
#include<fstream>
#include<stdlib.h>
#include<vector>
extern "C"
{
#include "C:\Users\padmanab\Desktop\Marvin_To_UnsignedChar\multiplyImage\multiplyImage.h"
//#include "C:\Users\padmanab\Documents\Visual Studio 2013\Projects\Marvin_To_UnsignedChar\multiplyImage\multiplyImage.h"
}

using namespace std;

class Image_Conversion
{
public:
    int color = 3;
    int width = 888;
    int height = 799;
    //Marvin::img<U16>& out;
    //Marvin::img<U16>& in;
    //unsigned char Convert_Marvin_To_MatlabImage();
    //void Convert_To_Marvin(unsigned char MatlabImage[3*799*888])
    unsigned char Convert_Marvin_To_MatlabImage(double Marvin_Image[3*888*799]);
    double Convert_To_MarvinImage(unsigned char Matlab_Image[3*888*799]);
    unsigned char Matlab_Image[3*799*888];
    double Marvin_Image;
};

CPP:

unsigned char Image_Conversion::Convert_Marvin_To_MatlabImage(double Marvin_Image[3*799*888])
{
    for (int h = 0; h < height; ++h)
    {
        for (int w = 0; w < width; ++w)
        {
            for (int c = 0; c < color; ++c)
            {
                Matlab_Image[c*height*width + w*height + h] = unsigned char(Marvin_Image[c*h*w]);
    return Matlab_Image[c*height*width + w*height + h];

            }
        }
    }



}

double Image_Conversion::Convert_To_MarvinImage(unsigned char Matlab_Image[3*799*888])
        {
            for (int c = 0; c < color; c++) 
            { 
                for (int y = 0; y < height; y++) 
                { 
                    for (int x = 0; x < width; x++) 
                    { 
                        Marvin_Image = (double)Matlab_Image[c*height*width + x*height + y]; 
               return Marvin_Image;
                    } 
                } 
            }

}

主要功能:

#include "Image_Conversion.h"
#include <iostream>
#include<fstream>
#include<stdlib.h>
#include "C:\Users\padmanab\Desktop\Marvin_To_UnsignedChar\multiplyImage\multiplyImage.h"


int main()
{
    Image_Conversion Image_Conversion;
    return 0;
    double img[3 * 799 * 888];
    int parameter = 4;
    unsigned char Matlab_Image = Image_Conversion.Convert_Marvin_To_MatlabImage(img);
    const unsigned char Matlab_Const_Image = (const unsigned char)Matlab_Image;
    unsigned char Output_Image = multiplyImage(&Matlab_Const_Image, parameter, &Matlab_Image);
    std::cout << "Output =" << Output_Image;




}

2 个答案:

答案 0 :(得分:2)

你的筹码堆满了,因为你在筹码上堆得太多了。每个Image_Conversion对象大约2MB(包含数组),而double img堆栈帧中存储的main数组大约为17MB。 / p>

对于筹码来说,这显然太大了。您应该在堆上存储这样的大数组,最好使用std::vector。为此,您可以更改类:

class Image_Conversion
{
public:
    int color = 3;
    int width = 888;
    int height = 799;

    Image_Conversion() : Matlab_Image(color * width * height) {}

    unsigned char Convert_Marvin_To_MatlabImage(double Marvin_Image[]);
    double Convert_To_MarvinImage(unsigned char Matlab_Image[]);
    std::vector<unsigned char> Matlab_Image;
    double Marvin_Image;
};

main就像这样:

int main()
{
    Image_Conversion Image_Conversion;
    return 0;
    std::vector<double> img(3 * 799 * 888);
    int parameter = 4;
    unsigned char Matlab_Image = Image_Conversion.Convert_Marvin_To_MatlabImage(img.data());
    const unsigned char Matlab_Const_Image = (const unsigned char)Matlab_Image;
    unsigned char Output_Image = multiplyImage(&Matlab_Const_Image, parameter, &Matlab_Image);
    std::cout << "Output =" << Output_Image;
}

转换功能保持不变。但请注意,他们可能没有做他们应该做的事情。第一个返回一个越界值(它到达数组),第二个返回内部循环中的return,因此它永远不会实际循环。但这些是不同的问题。

答案 1 :(得分:0)

你在堆栈上分配的方式太多了(数百万个阵列,每个阵列都是3 * 799 * 888!)。尝试使用堆,使用std :: vector&lt;&gt;例如。

编辑: 使用 new运算符在堆上分配对象: auto v = new vector&lt;矢量&lt;矢量&lt;双&gt;&gt;&gt;

不要忘记删除您的分配以避免资源泄漏,或者更好,使用智能指针:

auto v = std :: unique_ptr(new vector&lt; ...&gt;)//使用make_unique(如果可用)