[英]A bug of using for loop to print out matrix in my program
I wrote the following code to test in int main()
我写了以下代码在
int main()
进行测试
int main() {
int *p1 = findmatrixsize("inputmatrix.txt","A");//A function gets matrix size
cout << p1[0] << endl << p1[1] << endl;
int *p2 = findmatrixsize("inputmatrix.txt","B");
cout << p2[0] << endl << p2[1] << endl;
double **C = normalmultiplication("inputmatrix.txt","A", "B");
cout << C[0][0] << endl;
ofstream fout;
const string s="outputmatrix.txt";
fout.open(s);
for(int i=0;i<p1[0];i++){
for(int j =0;j<p2[1];j++){
fout << C[i][j] << " ";
}
fout<< endl;
}
fout.close();
return 0;
}
Everything works except when I am executing the multiarray printing. 一切正常,除了执行多阵列打印时。 Here is the problem:
这是问题所在:
1.If I comment out the printing part, p1[0],p1[1],p2[0],p2[1],C[i][j]
gives me perfect and correct result that I want. 1.如果我注释掉打印部分,
p1[0],p1[1],p2[0],p2[1],C[i][j]
给我想要的完美和正确的结果。
2.However, when I print by using normal loops it doesn't work. 2.但是,当我使用普通循环打印时,它不起作用。 But, but,but, if I change the
p1[0],p2[1]
in the for loop to a concrete number like 3(when my matrix dimension is 3), it works again. 但是,但是,但是,如果我将for循环中的
p1[0],p2[1]
更改为3这样的具体数字(当我的矩阵维数为3时),它将再次起作用。
3.It compiles, but the for loop printing part is not written into the txt. 3.编译,但是for循环打印部分未写入txt。 When I changed it to the standard print cout(not only the single line, I mean the whole method), I couldn't see them in the terminal neither.
当我将其更改为标准打印指令(不仅是一行,而是整个方法)时,在终端上也看不到它们。 But as I mentioned above, when I change p[0] p[1] to 3, it works, which means I can see them either in the txt or terminal
但是如上所述,当我将p [0] p [1]更改为3时,它可以工作,这意味着我可以在txt或终端中看到它们
I have never met such weird situation in C++ before, so I hope someone can help me out! 我以前从未在C ++中遇到过这种奇怪的情况,所以我希望有人能帮助我!
FYI, I use Visual Studio 2010 as compiler. 仅供参考,我使用Visual Studio 2010作为编译器。
Thanks for your help! 谢谢你的帮助!
We don't know what findmatrixsize
and normalmultiplication
do. 我们不知道
findmatrixsize
和normalmultiplication
作用。
But if they do what I think they do (and I think they return pointers to their local data), the objects referred to by p1
, p2
and C
share storage. 但是,如果他们按照我的想法去做(我认为他们返回指向本地数据的指针),则由
p1
, p2
和C
引用的对象将共享存储空间。
Check if p1 == p2
. 检查
p1 == p2
。
The problem is in the way you get result from findmatrixsize and normalmultiplication functions: they keep the result of calculation in stack and return a pointer to it, but the stack is destroyed soon after these functions finish their work. 问题在于您从findmatrixsize和normalmultiplication函数获取结果的方式:它们将计算结果保存在堆栈中并返回指向它的指针,但是在这些函数完成工作之后,堆栈立即被破坏。 Reimplement these functions so that the place in memory is valid outside of these functions.
重新实现这些功能,以使存储器中的位置在这些功能之外有效。 For example,
例如,
int p1[2];
findmatrixsize("inputmatrix.txt","A", p1)
and put the result inside of findmatrixsize into the last argument, eg. 并将结果放入findmatrixsize的最后一个参数中,例如。
void findmatrixsize(const char* fileName, const char* matrixName, int* result)
{
// routine
result[0] = someValue1;
result[1] = someValue2;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.