简体   繁体   English

静态内存分配中的浅拷贝构造函数问题?

[英]Shallow Copy Constructor Problems in Static memory Allocation?

Shallow Copy : When we initalize one object with another then the compiler copies state of one object to the other using copy constructor by assigning data member values of previous object to newly created object.浅拷贝: When we initalize one object with another then the compiler copies state of one object to the other using copy constructor by assigning data member values of previous object to newly created object.

Problem in Shallow Copy浅拷贝问题

  • Dangling Pointer (Pointer pointing to incorrect memory location).悬空指针(指向错误内存位置的指针)。
  • Update data on that memory location.更新该内存位置上的数据。

In Copy constructor two objects points to the same memory location and then the above two problem can take place.在复制构造函数中, two objects指向相同的memory location ,然后就会发生上述两个问题。 I have studied that these two problem take place when memory is allocated dynamically .我研究过这两个问题发生在memory is allocated dynamically时。 if not dynamic memory is involved then it works fine.如果不涉及dynamic memory ,那么它工作正常。

Question: My question is if static memory is allocated to an object at compile time, and at run time on by performing some event i initialize another object from that object who have already assigned memory statically.问题:我的问题是静态内存是否在编译时分配给一个对象,并在运行时通过执行某个事件我从该对象初始化另一个对象,该对象已经静态分配了内存。 Then also same problem will come or not?那么同样的问题会不会出现? if not why and how?如果不是为什么以及如何? The memory location is same to which both objects are pointing.两个对象所指向的内存位置相同。 Then why Dangling Pointer problem will not come.那为什么Dangling Pointer问题就不会来了。

My question is if static memory is allocated to an object at compile time, and at run time on by performing some event i initialize another object from that object who have already assigned memory statically.我的问题是静态内存是否在编译时分配给一个对象,并在运行时通过执行一些事件我从已经静态分配内存的那个对象初始化另一个对象。 Then also same problem will come or not?那么同样的问题会不会出现? if not why and how?如果不是为什么以及如何?

you will have two objects with pointers pointing to the same instance, so both will modify the same object.您将有两个指针指向同一个实例的对象,因此两者都会修改同一个对象。 The problem with dynamic memory allocation is that it must be at some point released, so if one object releases it then the second will release already freed memory - which is Undefined Behaviour.动态内存分配的问题在于它必须在某个时刻被释放,所以如果一个对象释放它,那么第二个将释放已经释放的内存——这是未定义行为。 So with static object case, there is no problem with "dangling pointer", because you cannot release its memory (and should not try), so you will not have UB.所以在静态对象的情况下,“悬空指针”没有问题,因为你不能释放它的内存(也不应该尝试),所以你不会有UB。

In Copy constructor two objects points to the same memory location and then the above two problem can take place.在复制构造函数中,两个对象指向相同的内存位置,然后就会发生上述两个问题。 I have studied that these two problem take place when memory is allocated dynamically.我研究过这两个问题发生在动态分配内存时。 if not dynamic memory is involved then it works fine.如果不涉及动态内存,那么它工作正常。

This isn't strictly true (though it would be a rare problem):这并非严格正确(尽管这将是一个罕见的问题):

#include <iostream>

using namespace std;

class Example
{
public:
    Example(int * const in) : p{in} {}

    int value() const { return *p; }
    int value(const int v) const { *p = v; return *p; }

private:
    int * p;
};

static int a = 5;
static Example first{&a};
// `first` now holds a pointer to `a`
static Example second = first;
// Now `first` and `second` hold pointers to the same static memory

int main()
{
    // Outputs `true`
    cout << boolalpha << (first.value() == second.value()) << endl;

    first.value(7);

    // Because they point to the same memory, this will output `true`
    cout << boolalpha << (first.value() == second.value()) << endl;
}

My question is if static memory is allocated to an object at compile time, and at run time on by performing some event i initialize another object from that object who have already assigned memory statically.我的问题是静态内存是否在编译时分配给一个对象,并在运行时通过执行一些事件我从已经静态分配内存的那个对象初始化另一个对象。 Then also same problem will come or not?那么同样的问题会不会出现? if not why and how?如果不是为什么以及如何?

You'll still have the same problem of你仍然会遇到同样的问题

Update data on that memory location.更新该内存位置上的数据。

#include <iostream>

using namespace std;

class Example
{
public:
    Example(int * const in) : p{in} {}

    int value() const { return *p; }
    int value(const int v) const { *p = v; return *p; }

private:
    int * p;
};

static int a = 5;
static Example first{&a};
// `first` now holds a pointer to `a`

int main()
{
    Example * second = new Example(first); // implicit copy constructor
    // Now `first` and `second` hold pointers to the same static memory,
    // though `second` is allocated dynamically

    // Outputs `true`
    cout << boolalpha << (first.value() == second->value()) << endl;

    first.value(7);

    // Because they point to the same memory, this will still output `true`
    cout << boolalpha << (first.value() == second->value()) << endl;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM