Access Violation (Segmentation Fault) error
Access Violation (Segmentation Fault) error
当我运行简单的数组编码时,当我输入第7个数字时出现访问违反(分段错误)错误。如何克服这个问题?
#include <iostream>
using namespace std;
int main()
{
int size = 0;
double temp[size];
double sum = 0;
double avg;
cout<<"Enter how many number would you like to type: ";
cin>>size;
for (int i=0; i < size; ++i )
{
cout<<endl<<"Number "<< i + 1<<": ";
cin>>temp[i];
sum=sum+temp[i];
}
avg=sum/size;
cout <<endl<< "The sum of the array elements is: " << sum << endl;
cout << "The average of the array elements is: " << avg << endl<<endl;
system("pause");
return 0;
}
正如其他注释解释的那样,在您的情况下,tmp[]
的大小为0,因为它是用非const表达式size
初始化的。当你访问数组边界之外的内存时,就会出现段错误(c++不验证数组边界)。
为什么你需要数组呢?你可以重新分配变量…
int main()
{
int size = 0;
double temp;
double sum = 0;
double avg;
cout<<"Enter how many number would you like to type: ";
cin>>size;
for (int i=0; i < size; ++i )
{
cout<<endl<<"Number "<< i + 1<<": ";
cin>>temp;
sum=sum+temp;
}
或者你应该使用vector<double> tmp
int main() {
int size = 0;
double temp[size];
在c++中是有效的。如果您的编译器编译它,则无法根据c++语言标准预测结果程序的外观或行为。数组(严格地说:完整的数组类型)必须有一个由常量表达式给出的严格正边界。
当您分配这样一个数组时,它的大小是固定的,并且必须在编译时知道。一般来说,不能使用变量来表示大小,但是在本例中,由于size
是在前面用常量表达式初始化的,因此它的值在编译时是已知的。编译器有效地将double temp[size]
替换为double temp[0]
(也许这里有一个警告会很好)。显然,当您尝试访问一个(固定)大小为0的数组的元素时,它的行为不会很好。
您正在尝试动态分配数组大小,您的方法是不正确的,当您试图从内存访问无效地址时,会发生分段故障,这意味着数组的位置不存在。我希望这能解决你的问题:
#include <iostream>
using namespace std;
int main()
{
int size;
int sum = 0;
float avg;
std::cout << "Number of elements you want to input: ";
std::cin >> size;
int *array = new int[size]; //assign array size dynamically
for (int i = 0; i < size; i++) //find sum
{
std::cout << "Input val " << i << ": ";
std::cin >> array[i];
sum = sum + array[i];
}
avg = sum / size; //cal avg
cout <<endl<< "Sum: " << sum << endl; //display
cout << "Avg: " << avg << endl;
delete [] array; //free memory
return 0;
}
相关文章:
- "error: no matching function for call to"构造函数错误
- Seg Fault Issue C++ (file IO / getline)
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 使用 LuaBridge 将 LuaJIT 绑定到C++会导致"PANIC: unprotected error"
- OpenMP卸载说'fatal error: could not find accel/nvptx-none/mkoffload'
- C++错误:"error: int aaa::bbb is protected within this context"
- 尝试链接我的着色器时,我收到错误代码"error c5145 must write to gl_position"
- C++,在int数组中输入字符串或字符会输出0,而不是ascii或error
- 示例外壳应用程序显示的 V8 "segmentation fault (core dumped)"错误
- 为什么我的C++代码中出现'Segmentation Fault: 11'行?
- 使用 cmake 的 LLVM 构建在 tsan_libdispatch_mac.cc 期间失败; "Error: conflicting types for ..."
- C++ "error: invalid use of void expression"
- struct.error:解压缩 C++ 结构时,解包需要 288 字节的缓冲区
- 如何修复"error: ‘_1’ was not declared in this scope"?
- 从不同目录执行时"symbol lookup error:",否则从构建目录执行时按预期工作
- 对象声明中的模板"error: expected expression"
- 在C++中使用 Catch 测试框架编译错误"error: expected ';' at end of declaration list"
- 给定一个整数数组,需要在Max_Heap上运行操作。得到错误"segmentation fault",有什么想法吗?(C++)
- OpenCV 3.4.1 error readNetFromTensorflow 无法在 cv::d nn::ReadProtoFromBinaryFile 中打开 .pb
- Access Violation (Segmentation Fault) error