在 Mac OS 上遇到的"Segmentation fault: 11" [C++]

"Segmentation fault: 11" encountered on Mac OS [C++]

本文关键字:C++ fault Segmentation OS 遇到 Mac      更新时间:2023-10-16

我是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.
};