在 Mac OS 上遇到的"Segmentation fault: 11" [C++]
"Segmentation fault: 11" encountered on Mac OS [C++]
我是C++新手,不知道如何克服有关内存管理的这个问题。
我用一些方法在类中创建了一个数组。我正在为数据结构中的一个类执行此操作。
当我在main()中运行所有方法时,出现错误"分段错误:11">
相反,当我不运行所有方法时,我不会遇到此问题。
我试图尽可能地简化代码,但到目前为止它还没有解决问题。有人可以帮助我吗?提前谢谢你!!
这是代码:
#include <iostream>
using namespace std;
// CLASS "list" -------------------------------------------------------------
class list{
// VARIABLES: The variables for the methods:
float* values; // Array, the actual data structure
int index_position; //
int size; // Size of array
float total; // Sum of all the values
float small_numb; // smallest value
float big_numb; // largest value
// METHODS:
public:
void set(); // Prompts the User to define the array
void print(); // Prints all the values in the array
void tot(); // Returns the sum of all the values
void avrg(); // Calculates the average value
void small(); // Shows the smalles number
void big(); // Shows the largest number
};
// CLASS "list" -------------------------------------------------------------
// METHODS for "list" class -------------------------------------------------
// "set" Method ---------------
void list::set(){
// Definition of the size
cout << "nEnter size of array: " << endl;
cin >> size;
while(1 == 1){
if(size <= 0){
cout << "nSize has to be greater than zero" << endl;
cin >> size;
}
else{
values = new float[size];
break;
}
}
// Insertion of the values
while(index_position < size){
float elem_array;
cout << "nEnter value: " << endl;
cin >> elem_array;
values[index_position] = elem_array;
index_position++;
}
}
// "print" Method ---------------
void list::print(){
int a = 0;
while(a < size){
cout << values[a] << " | ";
a++;
}
cout << endl;
}
// "tot" Method ---------------
void list::tot(){
for(int i = 0; i < size; i++){
total += values[i];
}
cout << total << endl;
}
// "avrg" Method ---------------
void list::avrg(){
for(int i = 0; i < size; i++){
total += values[i];
}
float y = (float) size;
cout << total / y << endl;
}
// "small" Method ---------------
void list::small(){ // tested
int x = 0;
small_numb = values[x];
while(x < size){
if(small_numb > values[x]){
small_numb = values[x];
}
x++;
}
cout << small_numb << endl;
}
// "big" Method ---------------
void list::big(){ // tested
int z = 0;
big_numb = values[z];
while(z < size){
if(big_numb < values[z]){
big_numb = values[z];
}
z++;
}
cout << big_numb << endl;
}
// METHODS for "list" class -------------------------------------------------
int main(){
list array; // Object "array" of "list" class
array.set(); // Definition of size and elements in "array"
array.print();
cout << "nSmallest:" << endl;
array.small();
cout << "nLargest:" << endl;
array.big();
cout << "nTotal:" << endl;
array.tot();
cout << "nAverage:" << endl;
array.avrg();
}
index_position未初始化,里面有一些垃圾
程序具有未定义的行为,因为在初始化之前使用了index_position
。
添加代码以将其初始化为合适的值。
作为良好的编码实践,请确保在构造对象时使用合适的值初始化所有成员变量。您可以通过定义构造函数来做到这一点。例如
class list
{
private:
// VARIABLES: The variables for the methods:
float* values; // Array, the actual data structure
int index_position; //
int size; // Size of array
float total; // Sum of all the values
float small_numb; // smallest value
float big_numb; // largest value
public:
list() : values(nullptr),
index_position(0),
size(0),
total(0),
small_numb(0),
big_numb(0) {}
// ...
// Rest of your class.
};
相关文章:
- Seg Fault Issue C++ (file IO / getline)
- 示例外壳应用程序显示的 V8 "segmentation fault (core dumped)"错误
- 为什么我的C++代码中出现'Segmentation Fault: 11'行?
- 给定一个整数数组,需要在Max_Heap上运行操作。得到错误"segmentation fault",有什么想法吗?(C++)
- 循环时无法进入:"Segmentation fault"
- OpenGL "Segmentation fault"错误
- 如何修复访问动态数组中结构中的字符串变量时"segmentation fault (core dumped)"错误
- 如何修复 C++ 中的'Segmentation fault'错误
- unordered_map segment fault
- 创建一个简单的类及其变量和函数不断返回" segmentation fault (core dumped)"、C++
- 在 Mac OS 上遇到的"Segmentation fault: 11" [C++]
- Getter seg fault
- 在 Linux 上实施英特尔 MKL 的 DFT 时"Segmentation fault"
- 尝试读取包含 ints 并存储在 int vector 中的文件,但我不断收到错误"Segmentation fault (core dumped)"
- 在非空 std::list 上使用 std::list.back<int>() 时"Segmentation Fault"
- 我正在尝试通过说话传递一些字符串,它会读取它们,但我得到了"segmentation fault"
- 为什么当我更改此代码时会给我"segmentation fault"错误?
- 尝试将文本文件加载到动态分配的 2D 数组中时出现'Segmentation fault'错误
- 将double打印到std::cout会导致Segmentation fault(C++)
- C++中的简单服务器.Freeaddinfo (Segmenataion fault) MSDN 示例