【发布时间】:2014-02-25 10:06:33
【问题描述】:
所以我的代码如下。我正在拼接矩阵/或 pgm 图像。通过镶板,我的意思是在一定数量的列和行中重复。它看起来像一个窗户,其中的窗格都将每个玻璃部分分开。
一个例子:http://1.bp.blogspot.com/_OLskT-GO5VE/TGqgrSX_o_I/AAAAAAAAA-4/vcCdn6hA3fI/s320/2007_12_warhol_cambell_soup.jpg 这是一个 4x8 的汤罐矩阵(它们本身就是一个巨大的像素矩阵。
我相信段错误发生在涉及 index=k 的某个地方,但我找不到任何错误。我正在重新调整矩阵的大小,所以不应该这样。
编辑:固定示例。
void panel(vector <VecofInts> &p, int inputRow, int inputColumn)
{
int i, j, v = 1, g = 1, k = 0, row, col;
row = p.size();//obtaining the original rows
col = p[0].size();//obtaining the original columns
p.resize((r * row)); //sets up the new matrix so I can add new elements.
/* This is my first test loop for the columns; I know I can create a single loop
for rows and columns but this will help me find problems more easily */
while(v < c){
...
}
/* this is the loop I'm having trouble with */
v=1;
while(v < c){
k = row;
while(g < r){
for(i = 0; i < row; i++){
k = k + i;
for(j = 0; j < col; j++){
p[k].push_back(p[i][j]);
}
}
g++;
k++; //this allows the g<r loop to continue and k not repeat itself
//in the first i loop again.
}
v++;
}
}
【问题讨论】:
-
尝试使用标志 -g 编译它并使用 gdb 之类的调试器运行它,这为您提供了确切的代码行,其中发生了段错误。见unknownroad.com/rtfm/gdbtut/gdbsegfault.html
-
变量“c”在哪里定义?
-
我必须解决调试器问题。我会一路更新
-
您根本没有检查 p 是否为空。
col = p[0].size();。如果 p 为空,则这是未定义的行为。 -
vector<vector<int>> x(10,vector<int>(10)); x.push_back(x[9]);将整个第 10 行复制到新分配的第 11 行。无需循环遍历 x[9] 中的每个条目。这是你想要做的吗?
标签: c++ c vector matrix segmentation-fault