在Mac OS上遇到“分段错误:11”[C ++]

时间:2018-02-08 05:04:06

标签: c++

我是C ++的新手,不知道如何克服有关内存管理的问题。

我用一些方法在类中创建了一个数组。我正在为数据结构中的一个类做这个。

当我在main()中运行所有方法时,我收到错误“Segmentation fault: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();

}

2 个答案:

答案 0 :(得分:1)

index_position未初始化,内部有一些垃圾

答案 1 :(得分:1)

您的程序具有未定义的行为,因为在初始化之前使用了index_position

添加代码以将其初始化为合适的值。

作为良好的编码实践,确保在构造对象时使用合适的值初始化所有成员变量。您可以通过定义构造函数来实现。 E.g。

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() : value(nullptr),
               index_position(0),
               size(0),
               total(0),
               small_numb(0),
               big_numb(0) {}

      // ...
      // Rest of your class.

};