【问题标题】:Pointer values not passing correctly to overloaded equals operator (C++)指针值未正确传递给重载的等于运算符 (C++)
【发布时间】:2013-08-03 02:54:15
【问题描述】:

我有一个重载的= 运算符,它正确传递了particle 类中的一些值,但没有传递其他值。我在网上进行了故障排除,找不到与此问题直接相关的任何内容。我在 C++ 方面相当胜任的同行无法提供帮助,并建议我在这里发帖。任何帮助将不胜感激。

widthheightwrapparticlenarray 的所有值都完美通过。但是,xposyposxvelyvel 的值传递的值不正确。对于xposypos,每11 个元素都正确通过,但所有其他元素都等于0;我希望这些值都不为零。在main 中,在立即进行gen1 = gen0; 的操作中,所有取消引用的gen0 值都是正确的。我估计他们没有正确通过?如果需要,我会很乐意发布更多代码/信息。同样,任何见解都将不胜感激。谢谢。

相关代码:

class particle{             
private:
    int* xpos;  
    int* ypos; 
    int* xvel;
    int* yvel;
    int* array;             
    int width;
    int height; 
    int wrap;
    int particlen;
public:
    void operator=(const particle&);
}

重载=运算符的相关部分:

void particle::operator=(const particle &current){
    int a,b,i,j;
    width = current.width;
    height = current.height;
    wrap = current.wrap;
    particlen = current.particlen;
    array = new int[width*height];
    xpos = new int[particlen];
    ypos = new int[particlen];
    xvel = new int[particlen];
    yvel = new int[particlen];

    for(a=0; a<height; a++){
        for(b=0; b< width; b++){
        this->array[a*width + b] = current.array[a*width + b];
        }
    } 

for(i = 0; i < particlen; i++){
    this->xpos[i] = current.xpos[i];
    this->ypos[i] = current.ypos[i];
    this->xvel[i] = current.xvel[i];
    this->yvel[i] = current.yvel[i];
}

main的相关部分:

int main(){
    particle gen0,gen1;
    gen1 = gen0;
}

在 Sehe 的建议下,我编辑了我的代码。但是,我现在使用最简单的命令遇到了内存分配问题。 array[i*width + j] = k; 得到一个分段错误 11。 GDB 打印输出:程序收到信号 EXC_BAD_ACCESS,无法访问内存。 原因:KERN_INVALID_ADDRESS 地址:0x0000000000000000 0x000000010001c4f4 inparticle::operator

我在网上查找了矢量库的正确术语,并认为我的语法是正确的。有什么想法吗?

更新(美国东部标准时间 8 月 2 日 9:00)

我仍然遇到 NULL 指针的问题。使用gdb调试时,收到消息:

KERN_INVALID_ADDRESS 地址:0x0000000000000000 0x000000010001d807 inparticle::operator

我对 NULL 指针没有什么经验;关于为什么会发生这种情况以及如何解决的任何建议?以下是相关代码(总代码约900行)

class particle{             
private:
    std::vector<int> xpos;  
    std::vector<int> ypos; 
    std::vector<int> xvel; 
    std::vector<int> yvel; 
    std::vector<int> array; 
    int width;      
    int height;     
    int wrap;       
    int particlen; // number of particles read in
public:
    void operator<<(particle);
    void Collision(int, int, particle); 
    void operator>>(char*);
    void operator<<(char*);
};

void particle::operator<<(char* file) // Reads initial input file
{
ifstream in_file;
in_file.open( file);    // open the file
int i,j,k;
}
in_file >> width >> height >> wrap >> particlen;
    for(i=0; i<height; i++){
        for(j=0; j< width; j++){
            in_file >> k;
            array[i*width + j] = k; // This is line 688 which GDB references
        }
    }
}

void particle::operator<<(particle current){
int i,k,j,l;

for(k = 0; k < height*width; k++)
{
    array[k] = 0;
}

k = 0;

for(i=0; i < particlen; i++)
{
    cout << "Current x pos is" << current.xpos[i] << endl;
}

for(i=0; i < particlen; i++)
{

    if(array[ (current.ypos[i]-1)*width + (current.xpos[i] - 1) ] == 0 )
    {
    //cout << "Current X position[" << i << "] is" << current.xpos[i] << endl;
        xpos[i] = current.xpos[i] + (current.xvel[i])*timeinc;
        if(xpos[i] > width) xpos[i] = xpos[i] - width; 
        ypos[i] = current.ypos[i] + (current.yvel[i])*timeinc;
        if(ypos[i] > width) ypos[i] = ypos[i] - height;

    //cout << "Next X position[" << i << "] is" << xpos[i] << endl;
    }

    if(array[ (current.ypos[i]-1)*width + (current.xpos[i] - 1) ] > 1 && current.JC[i] != 1)
    {
        for(l=i+1; l < particlen; l++)
        {
            if(current.xpos[l] == current.xpos[i] && current.ypos[l] == current.ypos[i] && current.JC[l] != 1)
            {
            Collision(i,l,current);
            }
        }
    }
}

for(i=0; i < particlen; i++)
{
    array[ (xpos[i]-1)*width + (ypos[i]-1) ] = 1;
}
Display();}

int main()
{
    char filename[256];
    particle gen0, gen1;
    gen0 << filename;
    gen1 = gen0;
    gen0 << gen1; // Calculates the next state of gen0, based on the values in gen1.
}

我知道这很长,但我认为这是我能提供的最少信息。我认为问题可能出在 gen0 &lt;&lt; gen1 运算符上,所以我包含了该方法。

【问题讨论】:

  • 这是有保证的内存泄漏;您没有删除原始数组。
  • @OliCharlesworth 我怀疑相反:我怀疑缺少复制构造函数浅层复制指针,但它们在一个实例中被释放。
  • 对动态数组使用std::vector&lt;int&gt;,而不是指针和new。你只是自找麻烦。
  • @OliCharlesworth 抱歉,我不完全清楚这会有什么帮助。你能解释一下吗?谢谢。
  • @user2638374 你编辑 still 没有显示任何构造函数调整向量(或原始数组,就此而言)。我只能假设你从一开始就没有这个。请张贴SSCCE最小但完整

标签: c++ pointers pass-by-reference pass-by-value


【解决方案1】:

就像我在评论中暗示的那样,99% 的人说你违反了三法则

值得注意的是,您有赋值运算符,但看不到复制构造函数。这可能意味着如果复制,数组指针最终将“非法”共享。我还假设你有一个析构函数删除这些数组(在一个实例中),从而使 复制到另一个实例中的数组无效。

首先通过避免手动内存管理来修复它:

#include <vector>

class particle{             
    std::vector<int> xpos;  
    std::vector<int> ypos; 
    std::vector<int> xvel;
    std::vector<int> yvel;
    std::vector<int> array;             
    int width;
    int height; 
    int wrap;
    int particlen; // or use `xpos.size()` e.g.
};

int main()
{
    particle gen0,gen1;
    gen1 = gen0;
}

PS。您可能想在需要边界检查时考虑使用v.at(i) 而不是v[i]。这对调试问题有很大帮助。

【讨论】:

  • 关闭:您忘记删除手动赋值运算符并让编译器处理它。零法则!
  • 精彩;谢谢您的帮助。我将实现这一点,并希望它能够正常工作。如果我有任何进一步的问题,我会告诉你。
  • 你需要实现默认构造函数,否则你的POD成员是未初始化的。
  • @BenjaminLindley 感谢您的评论。我确实实现了一个默认构造函数;只是没有将其包含在发布的代码中。
  • @sehe 我仍然遇到内存分配问题。例如,每当我运行代码时: for(i=0; i> k;数组[i*width + j] = k;我得到一个分段错误,GDB 给了我 KERN_INVALID_ADDRESS 地址:0x0000000000000000。这可能是我的编译器的问题吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-27
  • 1970-01-01
相关资源
最近更新 更多