为什么"std::cin >> secondMatrix[i*twoColoumn + j];"给我分段错误
Why "std::cin >> secondMatrix[i*twoColoumn + j];" give me segmentation fault
语言:c++系统:Linux:编译:g++ prog.cpp -o prog
这就是问题所在。我有一个程序,让用户插入两个矩阵的大小(我保持矩阵 1D 而不是 2D 作为练习)。给我带来问题的代码部分通常很好,除非我输入:
2
1
1
1
当我这样做时,输出是printf("4")和printf("5")之间的分割错误。
#include <iostream>
int main(void)
{
int oneColoumn, oneRow, twoColoumn, twoRow;
std::cout << "nHow many coloumns do you want for the first matrix?" << std::endl;
std::cin >> oneColoumn;
std::cout << "nHow many rows do you want for the first matrix?" << std::endl;
std::cin >> oneRow;
std::cout << "nHow many coloumns do you want for the second matrix?" << std::endl;
std::cin >> twoColoumn;
std::cout << "nHow many rows do you want for the second matrix?" << std::endl;
std::cin >> twoRow;
int firstMatrix[oneColoumn*oneRow];
int secondMatrix[twoColoumn*twoRow];
for(int i=0; i < oneColoumn; i++)
{
for(int j=0; j < oneRow; j++)
{
std::cout << "Insert a number for the first matrix";
std::cin >> firstMatrix[i*oneColoumn + j];
}
}
printf("1");
for(int i=0; i < twoColoumn; i++)
{printf("2");
for(int j=0; j < twoRow; j++)
{printf("3");
std::cout << "Insert a number for the second matrix";
printf("4");
std::cin >> secondMatrix[i*twoColoumn + j];
printf("5");
}
}
int threeColoumn, threeRow;
if(oneColoumn>twoColoumn)
threeColoumn=twoColoumn;
if(oneRow>twoRow)
threeRow=twoRow;
int thirdMatrix[threeColoumn*threeRow];
char choice;
std::cout<<"Do you want to add or multiply the two matrices?(a/m)"<<std::endl;
std::cin>>choice;
if(choice=='a')
{
std::cout<<"The two matrices have been added"<<std::endl;
//Addition(firstMatrix,oneRow,oneColoumn,secondMatrix,twoRow,twoColoumn,thirdMatrix,threeRow,threeColoumn);
}
else if(choice=='m')
{
std::cout<<"The two matrices have been multiplied"<<std::endl;
//Multiplication(firstMatrix,oneRow,oneColoumn,secondMatrix,twoRow,twoColoumn,thirdMatrix,threeRow,threeColoumn);
}
}
您有一个数组索引问题:
for(int i=0; i < oneColoumn; i++)
{
for(int j=0; j < oneRow; j++)
{
std::cout << "Insert a number for the first matrix";
std::cin >> firstMatrix[i*oneColoumn + j];
}
}
完成一次内部循环后,将迭代 oneRow 时间。
完成两次内部循环后,您已经迭代了 2*oneRow 时间。
。等
你想要:
firstMatrix[i*oneRow + j]
此外,正如其他人指出的那样,以下两行将堆栈上的数组声明为 VLA(可变长度数组),因为oneColumn
和oneRow
的值由用户提供,直到运行时才知道。
int firstMatrix[oneColoumn*oneRow];
int secondMatrix[twoColoumn*twoRow];
这不一定受支持,但可能取决于您的编译器。 有关 gcc,请参阅 https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html。 还可以看到这个:VLA 和通过 malloc 进行动态内存分配有什么区别?
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 在某些循环内使用vector.push_back时出现分段错误
- 错误,包括&lt; ctype&gt;在原子上使用C 11
- 错误c++visual studio c2227左侧'->;Init';必须指向类/结构/联合/泛型类型
- opencv 2.4.7在iOS错误背景_segm.hpp #include&lt; list&gt;未找到
- 链接器(MinGW的ld v2.23.2)错误,格式:-L<SOMEDIR>-l<lib1-l<l