2次元配列を行列として使用し、boost :: ublas :: matrixを使用するという2つの異なる方法でコードを書きました。このオブジェクトを最初のケースに追加するときは機能していますが、2番目のケースではセグメンテーション・フォルトが発生します。私は第二の方法を使用したいので、なぜ誰かがsegfaultを取得していることを知っていれば、私は感謝します。Segfaultにオブジェクトを追加するとき<vector>
コード:
img.h
#include <Magick++.h>
#include <string>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>
using namespace boost::numeric::ublas;
using namespace std;
using namespace Magick;
class Img
{
public:
Img();
Img(const string path2file);
unsigned int width, height;
string filename;
private:
typedef struct pix
{
Quantum R;
Quantum G;
Quantum B;
} pix;
matrix<pix> p;
pix **pixels;
string format;
};
img.cpp
Img::Img(const string path2file)
{
Image file;
unsigned int i, j;
Color pixel;
file.read(path2file);
filename = path2file;
width = file.size().width();
height = file.size().height();
// begin of first way
pixels = (pix**)malloc(sizeof(pix*)*height);
for(i=0 ; i<height ; ++i)
pixels[i] = (pix*)malloc(sizeof(pix)*width);
for(i=0 ; i<height ; ++i)
{
for(j=0 ; j<width ; ++j)
{
pixel = file.pixelColor(j, i);
pixels[i][j].R = pixel.redQuantum();
pixels[i][j].G = pixel.greenQuantum();
pixels[i][j].B = pixel.blueQuantum();
}
}
// end of first way
// begin of second way
p.resize(height, width);
for(i=0 ; i<height ; ++i)
{
for(j=0 ; j<width ; ++j)
{
pixel = file.pixelColor(j, i);
p(i, j).R = pixel.redQuantum();
p(i, j).G = pixel.greenQuantum();
p(i, j).B = pixel.blueQuantum();
}
}*/
}
// end of second way
私は、このコードは、セグメンテーション違反の原因ではないことを確信しています。 しかし、私は、私は(最初に働いている、唯一の第二の方法のための)セグメンテーションフォールトを取得していますメインプログラムで使用する場合:
main.cppに
#include <iostream>
#include <stdio.h>
#include "img.h"
#include <vector>
using namespace std;
int main(void)
{
std::vector<Img> files;
files.push_back(Img("files/mini.bmp"));
return 0;
}
デバッガーを試しましたか?またはValgrind? – derobert
ベクターは実際には全く関連していますか? Imgオブジェクトをベクトルに直接追加するのではなく、それ自身で構築してみてください。これは、あなたが言ったように、ベクトルにオブジェクトを追加することが本当に問題かどうかを判断するのに役立ちます。 –
@chrisaycock、あなたはC++のproramを意味します:) –