找到分段故障原因的麻烦

问题描述 投票:-2回答:1

代码不断给出分段错误(核心丢弃)错误

我觉得getNumberParticles()有问题;因为我只是返回0作为函数定义没有错误,但如果我返回除了0之外的任何其他错误

如果你告诉我使用gdb它的多个文件程序与一个主要我不知道如何调试它

particleList.cpp


    ParticleList::ParticleList() {
    numParticles = 0;
    particles[500] = {};
    }

//  ParticleList::ParticleList(int rows, int cols){
//  numParticles = 0;
//  particles[rows * cols * 4] = {};
//  } 

    ParticleList::ParticleList(const ParticleList& obj){

    }
    // Clean-up the particle list
    ParticleList::~ParticleList() {
        for(int i = 0 ; i < 500 ; i++){
            delete particles[i];
    }
    }

    // Number of particles in the ParticleList
    int ParticleList::getNumberParticles() {
        numParticles = 0;
        for(int i = 0 ; i < 500 ; i++){

            if(particles[i] != nullptr){
                numParticles++;
            }
     }
       return this->numParticles;
    }

    // Get a pointer to the i-th particle in the list
    ParticlePtr ParticleList::get(int i) {
       return particles[i-1];
    }

    // Add a particle (as a pointer) to the list
    //    This class now has control over the pointer
    //    And should delete the pointer if the particle is removed from the list
    void ParticleList::add_back(ParticlePtr particle) {
        int quantity = getNumberParticles();
        particles[quantity]= particle;
    }

    // Remove all particles from the list
    void ParticleList::clear() {
        for(int i = 0 ; i < 500 ; i++){
            particles[i]= nullptr;
        }
    }

ParticleList.h


#ifndef COSC_ASS_ONE_PARTICLE_LIST
#define COSC_ASS_ONE_PARTICLE_LIST

#include "Particle.h"
#include "Types.h"

class ParticleList {
public:

   /*                                           */
   /* DO NOT MOFIFY ANY CODE IN THIS SECTION    */
   /*                                           */


   // Create a New Empty List
   ParticleList();

   ParticleList(const ParticleList& obj); //Copy Construcor

   // Clean-up the particle list
   ~ParticleList();

   // Number of particles in the ParticleList
   int getNumberParticles();

   // Get a pointer to the i-th particle in the list
   ParticlePtr get(int i);

   // Add a particle (as a pointer) to the list
   //    This class now has control over the pointer
   //    And should delete the pointer if the particle is removed from the list
   void add_back(ParticlePtr particle);

   // Remove all particles from the list
   // Don't forget to clean-up the memory!
   void clear();

   /*                                           */
   /* YOU MAY ADD YOUR MODIFICATIONS HERE       */
   /*                                           */
   ParticleList(int rows, int cols);
   /* This is a suggestion of what you could use. */
   /* You can change this code.                   */
private:

    // particle* particles[300] 
    //Array of pointers to particle objects
   ParticlePtr    particles[500];
   int           numParticles;



};
c++ segmentation-fault g++
1个回答
0
投票

你的班级有一些问题:

  1. 我没有看到它提供的任何std::vector<ParticlePtr>也没有提供(正确)。
  2. 您尚未提供赋值运算符,因此违反了rule of 3
  3. 您的复制构造函数不正确,并且不会初始化您的任何成员。
  4. 你的get方法实现了基于1的索引,这很可能让每个人都感到困惑。

上述问题2或3很可能是导致崩溃的根本原因。

但是问题1应该足以丢弃这些代码并用std::vector替换它。

© www.soinside.com 2019 - 2024. All rights reserved.