重载新的返回错误的地址 [英] Overloading new returns wrong address

查看:40
本文介绍了重载新的返回错误的地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>
using namespace std;

class station{
    int n;
    float *p;
public:
    station(){};
    station(const station &ob){cout<<"Copy";}
    station operator=(station* a){cout<<"Nai";}
    ~station(){cout<<"Destructor";}
    static float counter;
    friend istream &operator>(istream  &stream, station &d);
    int &getN(){return n;};
    float *&getP(){return p;};
    void* operator new[](size_t size);
    void* operator new(size_t size){station *a;a=::new station;return a;};


};
void* station::operator new[](size_t size){
    station* a;
    a=(::new station[size]);
    int b;
    b=(size)/(sizeof(station));
    for(int i=0;i<b;i++){
        cin>a[i];
    }
   cout<<a;
    return a;
}



float station::counter;
istream &operator>( istream  &stream, station &d){
    cout<<"Dwse arithmo deigmatwn";
    int num;
    stream>>num;
    d.getP()=new float[num];
    d.getN()=num;
    for(int i=0;i<num;i++){
        stream>>d.getP()[i];
    }
    return stream;
}



int main(){
    station* a;
    a=new station[2];
    cout<<a;
    return 0;
}

大家好,这是我的第一篇文章,所以请原谅我的任何错误.

Hello everyone, This is my first post, so forgive me for any mistakes of mine.

我创建了一个新类,其中包含新运算符和提取程序的重载.我的问题是new返回的地址不同于重载运算符内部的地址,正如您在声明 cout<< a 的行中所看到的那样.但是,当我删除析构函数时,一切正常.有什么想法吗?

I have created a new class with an overload of new operator and an extractor. My problem is that the address that is returned by new is different from the one that is inside the overloaded operator as you can see in lines where cout<<a is stated. However, when i erase the destructor everything goes normal. Any ideas?

推荐答案

使用 new [] 运算符时,编译器可以为内部簿记分配一些额外的空间(以存储例如数组大小),因此在调用 delete [] 时,它将知道要销毁多少个对象).这就是您在原始"分配和最终数组分配的对象地址之间看到的差异.

When using the new[] operator, the compilers can allocate some extra space for the internal bookkeeping (to store e.g. the array size, so when calling delete[], it will know how many object to destroy). That is the difference you see between the "raw" allocation and the final array allocated object address.

这也是原因,为什么您不应该在通过 new [] 分配的内存上调用 delete ,反之亦然,因为这可能会导致内存泄漏(仅拳头对象被破坏),访问无效数据和/或释放错误的指针(技术上是所有UB).

That is also the reason, why you should not call delete on a memory allocated via new[] and vice versa, as it might result in memory leaks (only fist object destroyed), accessing invalid data and/or freeing a bad pointer (technically all UB).

编辑

对于对象内容问题,不应像这样在 operator new [] 中初始化对象

As for the object contents issues, you are not supposed to initialize the objects in the operator new[] like this

for(int i=0;i<b;i++){
    cin>a[i];
}

运算符new/new []只是分配原始"内存.

the operator new/new[] is just to allocate the "raw" memory.

只需删除它,然后在需要时读取构造函数中的对象(编译器会自动为数组中的每个对象调用构造函数),例如:

Simply remove that and read the object in the constructor if you wish to (the constructors are called by the compiler automatically for each object in the array), e.g.:

station(){cin >> *this}; // or '>' if you really want to keep that

但是通常,从流中读取通常是显式完成的,因此您可以执行以下操作:

But in general, the reading from stream is usually done explicitly, so you can do for example:

a=new station[2];
for(int i=0;i<2;i++){
    cin>a[i];
}

main()中.

这篇关于重载新的返回错误的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆